saluki_core/components/
mod.rs

1//! Component basics.
2
3use std::fmt;
4
5use crate::topology::ComponentId;
6
7pub mod destinations;
8pub mod encoders;
9pub mod forwarders;
10pub mod sources;
11pub mod transforms;
12
13/// Component type.
14#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
15pub enum ComponentType {
16    /// Source.
17    Source,
18
19    /// Transform.
20    Transform,
21
22    /// Destination.
23    Destination,
24
25    /// Forwarder.
26    Forwarder,
27
28    /// Encoder.
29    Encoder,
30}
31
32impl ComponentType {
33    /// Returns the string representation of the component type.
34    pub fn as_str(&self) -> &'static str {
35        match self {
36            Self::Source => "source",
37            Self::Transform => "transform",
38            Self::Destination => "destination",
39            Self::Forwarder => "forwarder",
40            Self::Encoder => "encoder",
41        }
42    }
43}
44
45/// A component context.
46///
47/// Component contexts uniquely identify a component within a topology by coupling the component identifier (name) and
48/// component type (source, transform, destination, forwarder, or encoder).
49///
50/// Practically speaking, all components are required to have a unique identifier. However, identifiers may be opaque
51/// enough that without knowing the _type_ of component, the identifier doesn't provide enough information.
52#[derive(Clone)]
53pub struct ComponentContext {
54    component_id: ComponentId,
55    component_type: ComponentType,
56}
57
58impl ComponentContext {
59    /// Creates a new `ComponentContext` for a source component with the given identifier.
60    pub fn source(component_id: ComponentId) -> Self {
61        Self {
62            component_id,
63            component_type: ComponentType::Source,
64        }
65    }
66
67    /// Creates a new `ComponentContext` for a transform component with the given identifier.
68    pub fn transform(component_id: ComponentId) -> Self {
69        Self {
70            component_id,
71            component_type: ComponentType::Transform,
72        }
73    }
74
75    /// Creates a new `ComponentContext` for a destination component with the given identifier.
76    pub fn destination(component_id: ComponentId) -> Self {
77        Self {
78            component_id,
79            component_type: ComponentType::Destination,
80        }
81    }
82
83    /// Creates a new `ComponentContext` for a forwarder component with the given identifier.
84    pub fn forwarder(component_id: ComponentId) -> Self {
85        Self {
86            component_id,
87            component_type: ComponentType::Forwarder,
88        }
89    }
90
91    /// Creates a new `ComponentContext` for a encoder component with the given identifier.
92    pub fn encoder(component_id: ComponentId) -> Self {
93        Self {
94            component_id,
95            component_type: ComponentType::Encoder,
96        }
97    }
98
99    /// Creates a new `ComponentContext` for a source component with the given identifier.
100    #[cfg(test)]
101    pub fn test_source<S: AsRef<str>>(component_id: S) -> Self {
102        Self {
103            component_id: ComponentId::try_from(component_id.as_ref()).expect("invalid component ID"),
104            component_type: ComponentType::Source,
105        }
106    }
107
108    /// Creates a new `ComponentContext` for a transform component with the given identifier.
109    #[cfg(test)]
110    pub fn test_transform<S: AsRef<str>>(component_id: S) -> Self {
111        Self {
112            component_id: ComponentId::try_from(component_id.as_ref()).expect("invalid component ID"),
113            component_type: ComponentType::Transform,
114        }
115    }
116
117    /// Creates a new `ComponentContext` for a destination component with the given identifier.
118    #[cfg(test)]
119    pub fn test_destination<S: AsRef<str>>(component_id: S) -> Self {
120        Self {
121            component_id: ComponentId::try_from(component_id.as_ref()).expect("invalid component ID"),
122            component_type: ComponentType::Destination,
123        }
124    }
125
126    /// Creates a new `ComponentContext` for a forwarder component with the given identifier.
127    #[cfg(test)]
128    pub fn test_forwarder<S: AsRef<str>>(component_id: S) -> Self {
129        Self {
130            component_id: ComponentId::try_from(component_id.as_ref()).expect("invalid component ID"),
131            component_type: ComponentType::Forwarder,
132        }
133    }
134
135    /// Creates a new `ComponentContext` for a encoder component with the given identifier.
136    #[cfg(test)]
137    pub fn test_encoder<S: AsRef<str>>(component_id: S) -> Self {
138        Self {
139            component_id: ComponentId::try_from(component_id.as_ref()).expect("invalid component ID"),
140            component_type: ComponentType::Encoder,
141        }
142    }
143
144    /// Returns the component identifier.
145    pub fn component_id(&self) -> &ComponentId {
146        &self.component_id
147    }
148
149    /// Returns the component type.
150    pub fn component_type(&self) -> ComponentType {
151        self.component_type
152    }
153}
154
155impl fmt::Display for ComponentContext {
156    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
157        write!(f, "{}[{}]", self.component_type.as_str(), self.component_id)
158    }
159}