nautilus_execution/order_emulator/
handlers.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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::execution::TradingCommand, msgbus::handler::MessageHandler};
19use nautilus_model::events::OrderEventAny;
20use ustr::Ustr;
21
22use super::emulator::OrderEmulator;
23
24#[derive(Debug)]
25pub struct OrderEmulatorExecuteHandler {
26    pub id: Ustr,
27    pub emulator: Rc<RefCell<OrderEmulator>>,
28}
29
30impl MessageHandler for OrderEmulatorExecuteHandler {
31    fn id(&self) -> Ustr {
32        self.id
33    }
34
35    fn handle(&self, msg: &dyn Any) {
36        self.emulator.borrow_mut().execute(
37            msg.downcast_ref::<&TradingCommand>()
38                .unwrap()
39                .to_owned()
40                .clone(),
41        );
42    }
43
44    fn as_any(&self) -> &dyn Any {
45        self
46    }
47}
48
49#[derive(Debug)]
50pub struct OrderEmulatorOnEventHandler {
51    pub id: Ustr,
52    pub emulator: Rc<RefCell<OrderEmulator>>,
53}
54
55impl MessageHandler for OrderEmulatorOnEventHandler {
56    fn id(&self) -> Ustr {
57        self.id
58    }
59
60    fn handle(&self, msg: &dyn Any) {
61        self.emulator.borrow_mut().on_event(
62            msg.downcast_ref::<&OrderEventAny>()
63                .unwrap()
64                .to_owned()
65                .clone(),
66        );
67    }
68
69    fn as_any(&self) -> &dyn Any {
70        self
71    }
72}