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::msgbus::handler::MessageHandler;
19use nautilus_model::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
43    fn as_any(&self) -> &dyn Any {
44        self
45    }
46}
47
48pub struct OrderEmulatorOnEventHandler {
49    pub id: Ustr,
50    pub emulator: Rc<RefCell<OrderEmulator>>,
51}
52
53impl MessageHandler for OrderEmulatorOnEventHandler {
54    fn id(&self) -> Ustr {
55        self.id
56    }
57
58    fn handle(&self, msg: &dyn Any) {
59        self.emulator.borrow_mut().on_event(
60            msg.downcast_ref::<&OrderEventAny>()
61                .unwrap()
62                .to_owned()
63                .clone(),
64        );
65    }
66
67    fn as_any(&self) -> &dyn Any {
68        self
69    }
70}