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