Skip to main content

nautilus_tardis/
factories.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
16//! Factory functions for creating Tardis clients.
17
18use std::{any::Any, cell::RefCell, rc::Rc};
19
20use nautilus_common::{cache::Cache, clients::DataClient, clock::Clock};
21use nautilus_model::identifiers::ClientId;
22use nautilus_system::factories::{ClientConfig, DataClientFactory};
23
24use crate::{common::consts::TARDIS, config::TardisDataClientConfig, data::TardisDataClient};
25
26impl ClientConfig for TardisDataClientConfig {
27    fn as_any(&self) -> &dyn Any {
28        self
29    }
30}
31
32/// Factory for creating Tardis data clients.
33#[derive(Debug)]
34pub struct TardisDataClientFactory;
35
36impl TardisDataClientFactory {
37    /// Creates a new [`TardisDataClientFactory`] instance.
38    #[must_use]
39    pub const fn new() -> Self {
40        Self
41    }
42}
43
44impl Default for TardisDataClientFactory {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50impl DataClientFactory for TardisDataClientFactory {
51    fn create(
52        &self,
53        name: &str,
54        config: &dyn ClientConfig,
55        _cache: Rc<RefCell<Cache>>,
56        _clock: Rc<RefCell<dyn Clock>>,
57    ) -> anyhow::Result<Box<dyn DataClient>> {
58        let tardis_config = config
59            .as_any()
60            .downcast_ref::<TardisDataClientConfig>()
61            .ok_or_else(|| {
62                anyhow::anyhow!(
63                    "Invalid config type for TardisDataClientFactory. \
64                     Expected TardisDataClientConfig, was {config:?}",
65                )
66            })?
67            .clone();
68
69        let client_id = ClientId::from(name);
70        let client = TardisDataClient::new(client_id, tardis_config)?;
71        Ok(Box::new(client))
72    }
73
74    fn name(&self) -> &'static str {
75        TARDIS
76    }
77
78    fn config_type(&self) -> &'static str {
79        "TardisDataClientConfig"
80    }
81}