Skip to main content

nautilus_data/engine/
bar.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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::fmt::Debug;
17
18use nautilus_common::{
19    messages::data::{SubscribeBars, SubscribeCommand},
20    msgbus::{MStr, Topic, TypedHandler},
21};
22use nautilus_core::UUID4;
23use nautilus_model::data::{Bar, BarType, QuoteTick, TradeTick};
24
25#[derive(Clone, Debug)]
26pub(super) struct BarAggregationSubscription {
27    pub(super) command: SubscribeBars,
28    pub(super) source: Option<SubscribeCommand>,
29}
30
31/// Identifies a bar aggregator instance.
32///
33/// Live subscriptions key on `(bar_type.standard(), None)`. Request-scoped
34/// aggregators carrying a `request_id` key on `(bar_type.standard(), Some(id))`
35/// so they can run alongside a live aggregator on the same bar type.
36pub(crate) type BarAggregatorKey = (BarType, Option<UUID4>);
37
38#[inline]
39pub(crate) fn bar_aggregator_key(bar_type: BarType, request_id: Option<UUID4>) -> BarAggregatorKey {
40    (bar_type.standard(), request_id)
41}
42
43/// Typed subscription for bar aggregator handlers.
44///
45/// Stores the topic and handler for each data type so we can properly
46/// unsubscribe from the typed routers.
47#[derive(Clone)]
48pub enum BarAggregatorSubscription {
49    Bar {
50        topic: MStr<Topic>,
51        handler: TypedHandler<Bar>,
52    },
53    Trade {
54        topic: MStr<Topic>,
55        handler: TypedHandler<TradeTick>,
56    },
57    Quote {
58        topic: MStr<Topic>,
59        handler: TypedHandler<QuoteTick>,
60    },
61}
62
63impl Debug for BarAggregatorSubscription {
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        match self {
66            Self::Bar { topic, handler } => f
67                .debug_struct(stringify!(Bar))
68                .field("topic", topic)
69                .field("handler_id", &handler.id())
70                .finish(),
71            Self::Trade { topic, handler } => f
72                .debug_struct(stringify!(Trade))
73                .field("topic", topic)
74                .field("handler_id", &handler.id())
75                .finish(),
76            Self::Quote { topic, handler } => f
77                .debug_struct(stringify!(Quote))
78                .field("topic", topic)
79                .field("handler_id", &handler.id())
80                .finish(),
81        }
82    }
83}