nautilus_common/messages/
data.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, collections::HashMap, sync::Arc};
17
18use nautilus_core::{UUID4, UnixNanos};
19use nautilus_model::{
20    data::{Data, DataType},
21    identifiers::{ClientId, Venue},
22};
23
24// TODO: redesign data messages for a tighter model
25#[derive(Debug)]
26pub struct DataRequest {
27    pub correlation_id: UUID4,
28    pub client_id: ClientId,
29    pub venue: Venue,
30    pub data_type: DataType,
31    pub ts_init: UnixNanos,
32    pub params: Option<HashMap<String, String>>,
33}
34
35pub type Payload = Arc<dyn Any + Send + Sync>;
36
37#[derive(Debug)]
38pub struct DataResponse {
39    pub correlation_id: UUID4,
40    pub client_id: ClientId,
41    pub venue: Venue,
42    pub data_type: DataType,
43    pub data: Payload,
44    pub ts_init: UnixNanos,
45    pub params: Option<HashMap<String, String>>,
46}
47
48impl DataResponse {
49    pub fn new<T: Any + Send + Sync>(
50        correlation_id: UUID4,
51        client_id: ClientId,
52        venue: Venue,
53        data_type: DataType,
54        data: T,
55        ts_init: UnixNanos,
56        params: Option<HashMap<String, String>>,
57    ) -> Self {
58        Self {
59            correlation_id,
60            client_id,
61            venue,
62            data_type,
63            data: Arc::new(data),
64            ts_init,
65            params,
66        }
67    }
68}
69
70#[derive(Debug, Clone, Copy)]
71pub enum Action {
72    Subscribe,
73    Unsubscribe,
74}
75
76#[derive(Debug, Clone)]
77pub struct SubscriptionCommand {
78    pub client_id: ClientId,
79    pub venue: Venue,
80    pub data_type: DataType,
81    pub action: Action,
82    pub command_id: UUID4,
83    pub ts_init: UnixNanos,
84    pub params: Option<HashMap<String, String>>,
85}
86
87impl SubscriptionCommand {
88    /// Creates a new [`SubscriptionCommand`] instance.
89    #[must_use]
90    pub const fn new(
91        client_id: ClientId,
92        venue: Venue,
93        data_type: DataType,
94        action: Action,
95        command_id: UUID4,
96        ts_init: UnixNanos,
97        params: Option<HashMap<String, String>>,
98    ) -> Self {
99        Self {
100            client_id,
101            venue,
102            data_type,
103            action,
104            command_id,
105            ts_init,
106            params,
107        }
108    }
109}
110
111pub enum DataCommand {
112    Request(DataRequest),
113    Subscribe(SubscriptionCommand),
114}
115
116// TODO: Refine this to reduce disparity between enum sizes
117#[allow(clippy::large_enum_variant)]
118pub enum DataEvent {
119    Response(DataResponse),
120    Data(Data),
121}