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