1use std::fmt;
7
8#[cfg(feature = "serde")]
9use serde::{Deserialize, Serialize};
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
28#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
29pub enum ComponentRole {
30 Source,
32 Processor,
34 Sink,
36 Filter,
38 Router,
40}
41
42impl ComponentRole {
43 #[inline]
47 pub const fn is_producer(&self) -> bool {
48 matches!(
49 self,
50 ComponentRole::Source | ComponentRole::Processor | ComponentRole::Router
51 )
52 }
53
54 #[inline]
58 pub const fn is_consumer(&self) -> bool {
59 matches!(
60 self,
61 ComponentRole::Processor
62 | ComponentRole::Sink
63 | ComponentRole::Filter
64 | ComponentRole::Router
65 )
66 }
67}
68
69impl fmt::Display for ComponentRole {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 match self {
72 ComponentRole::Source => write!(f, "Source"),
73 ComponentRole::Processor => write!(f, "Processor"),
74 ComponentRole::Sink => write!(f, "Sink"),
75 ComponentRole::Filter => write!(f, "Filter"),
76 ComponentRole::Router => write!(f, "Router"),
77 }
78 }
79}
80
81#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
98#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
99pub enum BackpressureSignal {
100 Ready,
102 Pause,
104}
105
106impl BackpressureSignal {
107 #[inline]
111 pub const fn is_paused(&self) -> bool {
112 matches!(self, BackpressureSignal::Pause)
113 }
114}
115
116impl fmt::Display for BackpressureSignal {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 match self {
119 BackpressureSignal::Ready => write!(f, "Ready"),
120 BackpressureSignal::Pause => write!(f, "Pause"),
121 }
122 }
123}
124
125#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
141#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
142pub enum BackpressurePolicy {
143 #[default]
146 BlockProducer,
147 DropOldest,
150 DropNewest,
153 Error,
156}
157
158impl BackpressurePolicy {
159 #[inline]
163 pub const fn may_lose_data(&self) -> bool {
164 matches!(
165 self,
166 BackpressurePolicy::DropOldest | BackpressurePolicy::DropNewest
167 )
168 }
169}
170
171impl fmt::Display for BackpressurePolicy {
174 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175 match self {
176 BackpressurePolicy::BlockProducer => write!(f, "BlockProducer"),
177 BackpressurePolicy::DropOldest => write!(f, "DropOldest"),
178 BackpressurePolicy::DropNewest => write!(f, "DropNewest"),
179 BackpressurePolicy::Error => write!(f, "Error"),
180 }
181 }
182}
183
184#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
202#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
203#[repr(u8)]
204pub enum ObservabilityLevel {
205 Off = 0,
207 #[default]
210 Production = 1,
211 Diagnostic = 2,
215}
216
217impl ObservabilityLevel {
218 #[inline]
222 pub const fn is_enabled(&self) -> bool {
223 !matches!(self, ObservabilityLevel::Off)
224 }
225
226 #[inline]
230 pub const fn is_diagnostic(&self) -> bool {
231 matches!(self, ObservabilityLevel::Diagnostic)
232 }
233
234 #[inline]
236 pub const fn from_u8(value: u8) -> Option<Self> {
237 match value {
238 0 => Some(ObservabilityLevel::Off),
239 1 => Some(ObservabilityLevel::Production),
240 2 => Some(ObservabilityLevel::Diagnostic),
241 _ => None,
242 }
243 }
244}
245
246impl fmt::Display for ObservabilityLevel {
249 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250 match self {
251 ObservabilityLevel::Off => write!(f, "Off"),
252 ObservabilityLevel::Production => write!(f, "Production"),
253 ObservabilityLevel::Diagnostic => write!(f, "Diagnostic"),
254 }
255 }
256}
257
258#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
272#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
273#[repr(u8)]
274pub enum Severity {
275 Trace = 0,
277 Debug = 1,
279 Info = 2,
281 Warn = 3,
283 Error = 4,
285}
286
287impl fmt::Display for Severity {
288 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
289 match self {
290 Severity::Trace => write!(f, "TRACE"),
291 Severity::Debug => write!(f, "DEBUG"),
292 Severity::Info => write!(f, "INFO"),
293 Severity::Warn => write!(f, "WARN"),
294 Severity::Error => write!(f, "ERROR"),
295 }
296 }
297}
298
299#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
317#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
318pub enum CopyReason {
319 HostToComponent,
321 ComponentToHost,
323 CrossComponent,
325 PoolReturn,
327}
328
329impl fmt::Display for CopyReason {
330 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
331 match self {
332 CopyReason::HostToComponent => write!(f, "HostToComponent"),
333 CopyReason::ComponentToHost => write!(f, "ComponentToHost"),
334 CopyReason::CrossComponent => write!(f, "CrossComponent"),
335 CopyReason::PoolReturn => write!(f, "PoolReturn"),
336 }
337 }
338}
339
340#[cfg(test)]
345mod tests {
346 use super::*;
347
348 #[test]
351 fn test_component_role_display() {
352 assert_eq!(format!("{}", ComponentRole::Source), "Source");
353 assert_eq!(format!("{}", ComponentRole::Processor), "Processor");
354 assert_eq!(format!("{}", ComponentRole::Sink), "Sink");
355 assert_eq!(format!("{}", ComponentRole::Filter), "Filter");
356 assert_eq!(format!("{}", ComponentRole::Router), "Router");
357 }
358
359 #[test]
360 fn test_component_role_producer_consumer() {
361 assert!(ComponentRole::Source.is_producer());
362 assert!(!ComponentRole::Source.is_consumer());
363 assert!(ComponentRole::Processor.is_producer());
364 assert!(ComponentRole::Processor.is_consumer());
365 assert!(!ComponentRole::Sink.is_producer());
366 assert!(ComponentRole::Sink.is_consumer());
367 assert!(!ComponentRole::Filter.is_producer());
368 assert!(ComponentRole::Filter.is_consumer());
369 assert!(ComponentRole::Router.is_producer());
370 assert!(ComponentRole::Router.is_consumer());
371 }
372
373 #[test]
376 fn test_backpressure_signal_is_paused() {
377 assert!(!BackpressureSignal::Ready.is_paused());
378 assert!(BackpressureSignal::Pause.is_paused());
379 }
380
381 #[test]
384 fn test_backpressure_policy_default() {
385 assert_eq!(
386 BackpressurePolicy::default(),
387 BackpressurePolicy::BlockProducer
388 );
389 }
390
391 #[test]
392 fn test_backpressure_policy_may_lose_data() {
393 assert!(!BackpressurePolicy::BlockProducer.may_lose_data());
394 assert!(BackpressurePolicy::DropOldest.may_lose_data());
395 assert!(BackpressurePolicy::DropNewest.may_lose_data());
396 assert!(!BackpressurePolicy::Error.may_lose_data());
397 }
398
399 #[test]
402 fn test_observability_level_ordering() {
403 assert!(ObservabilityLevel::Off < ObservabilityLevel::Production);
404 assert!(ObservabilityLevel::Production < ObservabilityLevel::Diagnostic);
405 }
406
407 #[test]
408 fn test_observability_level_is_enabled() {
409 assert!(!ObservabilityLevel::Off.is_enabled());
410 assert!(ObservabilityLevel::Production.is_enabled());
411 assert!(ObservabilityLevel::Diagnostic.is_enabled());
412 }
413
414 #[test]
415 fn test_observability_level_is_diagnostic() {
416 assert!(!ObservabilityLevel::Off.is_diagnostic());
417 assert!(!ObservabilityLevel::Production.is_diagnostic());
418 assert!(ObservabilityLevel::Diagnostic.is_diagnostic());
419 }
420
421 #[test]
422 fn test_observability_level_from_u8() {
423 assert_eq!(
424 ObservabilityLevel::from_u8(0),
425 Some(ObservabilityLevel::Off)
426 );
427 assert_eq!(
428 ObservabilityLevel::from_u8(1),
429 Some(ObservabilityLevel::Production)
430 );
431 assert_eq!(
432 ObservabilityLevel::from_u8(2),
433 Some(ObservabilityLevel::Diagnostic)
434 );
435 assert_eq!(ObservabilityLevel::from_u8(3), None);
436 }
437
438 #[test]
439 fn test_observability_level_default() {
440 assert_eq!(
441 ObservabilityLevel::default(),
442 ObservabilityLevel::Production
443 );
444 }
445
446 #[test]
447 fn test_observability_level_repr_u8() {
448 assert_eq!(ObservabilityLevel::Off as u8, 0);
449 assert_eq!(ObservabilityLevel::Production as u8, 1);
450 assert_eq!(ObservabilityLevel::Diagnostic as u8, 2);
451 }
452
453 #[test]
456 fn test_severity_ordering() {
457 assert!(Severity::Trace < Severity::Debug);
458 assert!(Severity::Debug < Severity::Info);
459 assert!(Severity::Info < Severity::Warn);
460 assert!(Severity::Warn < Severity::Error);
461 }
462
463 #[test]
464 fn test_severity_display() {
465 assert_eq!(format!("{}", Severity::Trace), "TRACE");
466 assert_eq!(format!("{}", Severity::Error), "ERROR");
467 }
468
469 #[test]
472 fn test_copy_reason_display() {
473 assert_eq!(
474 format!("{}", CopyReason::HostToComponent),
475 "HostToComponent"
476 );
477 assert_eq!(
478 format!("{}", CopyReason::ComponentToHost),
479 "ComponentToHost"
480 );
481 assert_eq!(format!("{}", CopyReason::CrossComponent), "CrossComponent");
482 assert_eq!(format!("{}", CopyReason::PoolReturn), "PoolReturn");
483 }
484}