saluki_core/components/
mod.rs1use 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#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
16pub enum ComponentType {
17 Source,
19
20 Transform,
22
23 Destination,
25
26 Forwarder,
28
29 Encoder,
31
32 Relay,
34}
35
36impl ComponentType {
37 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#[derive(Clone)]
58pub struct ComponentContext {
59 component_id: ComponentId,
60 component_type: ComponentType,
61}
62
63impl ComponentContext {
64 pub fn source(component_id: ComponentId) -> Self {
66 Self {
67 component_id,
68 component_type: ComponentType::Source,
69 }
70 }
71
72 pub fn transform(component_id: ComponentId) -> Self {
74 Self {
75 component_id,
76 component_type: ComponentType::Transform,
77 }
78 }
79
80 pub fn destination(component_id: ComponentId) -> Self {
82 Self {
83 component_id,
84 component_type: ComponentType::Destination,
85 }
86 }
87
88 pub fn forwarder(component_id: ComponentId) -> Self {
90 Self {
91 component_id,
92 component_type: ComponentType::Forwarder,
93 }
94 }
95
96 pub fn encoder(component_id: ComponentId) -> Self {
98 Self {
99 component_id,
100 component_type: ComponentType::Encoder,
101 }
102 }
103
104 pub fn relay(component_id: ComponentId) -> Self {
106 Self {
107 component_id,
108 component_type: ComponentType::Relay,
109 }
110 }
111
112 #[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 #[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 #[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 #[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 #[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 #[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 pub fn component_id(&self) -> &ComponentId {
168 &self.component_id
169 }
170
171 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}