acme_engine/sources/
manager.rs

1/*
2    appellation: manager <module>
3    authors: @FL03
4*/
5use super::SourceRegistry;
6use crate::pipes::PipeRouter;
7
8/// The [`SourceManager`] is responsible for managing the current data sources
9#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
10#[cfg_attr(
11    feature = "serde",
12    derive(serde::Serialize, serde::Deserialize),
13    serde(rename_all = "snake_case")
14)]
15#[repr(C)]
16pub struct SourceManager {
17    pub(crate) registry: SourceRegistry,
18    pub(crate) router: PipeRouter,
19}
20
21impl SourceManager {
22    /// create a new instance of the [`SourceManager`].
23    pub const fn new() -> Self {
24        Self {
25            registry: SourceRegistry::new(),
26            router: PipeRouter::new(),
27        }
28    }
29    /// create a new [`SourceManager`] with the provided [`SourceRegistry`] and the default
30    /// [`PipeRouter`].
31    pub fn from_registry(registry: SourceRegistry) -> Self {
32        Self {
33            registry,
34            router: PipeRouter::default(),
35        }
36    }
37    /// create a new [`SourceManager`] with the provided [`PipeRouter`] and the default
38    /// [`SourceRegistry`].
39    pub fn from_router(router: PipeRouter) -> Self {
40        Self {
41            registry: SourceRegistry::default(),
42            router,
43        }
44    }
45    /// returns an immutable reference to the current [`SourceRegistry`].
46    pub const fn registry(&self) -> &SourceRegistry {
47        &self.registry
48    }
49    /// returns a mutable reference to the current [`SourceRegistry`].
50    pub const fn registry_mut(&mut self) -> &mut SourceRegistry {
51        &mut self.registry
52    }
53    /// returns a reference to the [`PipeRouter`] used by this [`SourceManager`].
54    pub const fn router(&self) -> &PipeRouter {
55        &self.router
56    }
57    /// returns a mutable reference to the [`PipeRouter`] used by this [`SourceManager`].
58    pub fn router_mut(&mut self) -> &mut PipeRouter {
59        &mut self.router
60    }
61    /// overwrite the current [`SourceRegistry`] with the provided one and return a mutable
62    /// reference to self
63    pub fn set_registry(&mut self, registry: SourceRegistry) -> &mut Self {
64        self.registry = registry;
65        self
66    }
67    /// consumes the current instance to create another with the given registry.
68    pub const fn with_registry(self, registry: SourceRegistry) -> Self {
69        Self { registry, ..self }
70    }
71    /// consumes the current instance to create another with the given [`PipeRouter`].
72    pub const fn with_router(self, router: PipeRouter) -> Self {
73        Self { router, ..self }
74    }
75}