nautilus_tardis/
factories.rs1use 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#[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 #[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}