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