saluki_core/components/
mod.rs1use 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#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
17pub enum ComponentType {
18 Source,
20
21 Relay,
23
24 Decoder,
26
27 Transform,
29
30 Encoder,
32
33 Forwarder,
35
36 Destination,
38}
39
40impl ComponentType {
41 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#[derive(Clone)]
63pub struct ComponentContext {
64 component_id: ComponentId,
65 component_type: ComponentType,
66}
67
68impl ComponentContext {
69 pub fn source(component_id: ComponentId) -> Self {
71 Self {
72 component_id,
73 component_type: ComponentType::Source,
74 }
75 }
76
77 pub fn relay(component_id: ComponentId) -> Self {
79 Self {
80 component_id,
81 component_type: ComponentType::Relay,
82 }
83 }
84
85 pub fn decoder(component_id: ComponentId) -> Self {
87 Self {
88 component_id,
89 component_type: ComponentType::Decoder,
90 }
91 }
92
93 pub fn transform(component_id: ComponentId) -> Self {
95 Self {
96 component_id,
97 component_type: ComponentType::Transform,
98 }
99 }
100
101 pub fn encoder(component_id: ComponentId) -> Self {
103 Self {
104 component_id,
105 component_type: ComponentType::Encoder,
106 }
107 }
108
109 pub fn forwarder(component_id: ComponentId) -> Self {
111 Self {
112 component_id,
113 component_type: ComponentType::Forwarder,
114 }
115 }
116
117 pub fn destination(component_id: ComponentId) -> Self {
119 Self {
120 component_id,
121 component_type: ComponentType::Destination,
122 }
123 }
124
125 #[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 #[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 #[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 #[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 #[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 #[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 #[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 pub fn component_id(&self) -> &ComponentId {
190 &self.component_id
191 }
192
193 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}