nautilus_execution/order_emulator/
handlers.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{any::Any, cell::RefCell, rc::Rc};
17
18use nautilus_common::{messages::data::DataResponse, msgbus::handler::MessageHandler};
19use nautilus_model::{data::Data, events::OrderEventAny};
20use ustr::Ustr;
21
22use crate::{messages::TradingCommand, order_emulator::emulator::OrderEmulator};
23
24pub struct OrderEmulatorExecuteHandler {
25    pub id: Ustr,
26    pub emulator: Rc<RefCell<OrderEmulator>>,
27}
28
29impl MessageHandler for OrderEmulatorExecuteHandler {
30    fn id(&self) -> Ustr {
31        self.id
32    }
33
34    fn handle(&self, msg: &dyn Any) {
35        self.emulator.borrow_mut().execute(
36            msg.downcast_ref::<&TradingCommand>()
37                .unwrap()
38                .to_owned()
39                .clone(),
40        );
41    }
42    fn handle_response(&self, _resp: DataResponse) {}
43    fn handle_data(&self, _data: Data) {}
44    fn as_any(&self) -> &dyn Any {
45        self
46    }
47}
48
49pub struct OrderEmulatorOnEventHandler {
50    pub id: Ustr,
51    pub emulator: Rc<RefCell<OrderEmulator>>,
52}
53
54impl MessageHandler for OrderEmulatorOnEventHandler {
55    fn id(&self) -> Ustr {
56        self.id
57    }
58
59    fn handle(&self, msg: &dyn Any) {
60        self.emulator.borrow_mut().on_event(
61            msg.downcast_ref::<&OrderEventAny>()
62                .unwrap()
63                .to_owned()
64                .clone(),
65        );
66    }
67    fn handle_response(&self, _resp: DataResponse) {}
68    fn handle_data(&self, _data: Data) {}
69    fn as_any(&self) -> &dyn Any {
70        self
71    }
72}