saluki_core/components/
mod.rs

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