1use std::fmt::{Display, Formatter};
16
17#[derive(Debug, PartialEq, Default, Clone, Copy, strum::FromRepr)]
19#[repr(u8)]
20pub enum Channel {
21 #[default]
22 Unknown = 0,
23
24 Mysql = 1,
25 Postgres = 2,
26 HttpSql = 3,
27 Prometheus = 4,
28 Otlp = 5,
29 Grpc = 6,
30 Influx = 7,
31 Opentsdb = 8,
32 Loki = 9,
33 Elasticsearch = 10,
34 Jaeger = 11,
35 Log = 12,
36 Promql = 13,
37 Splunk = 14,
38 Internal = 255,
40}
41
42impl From<u32> for Channel {
43 fn from(value: u32) -> Self {
44 u8::try_from(value)
45 .ok()
46 .and_then(Self::from_repr)
47 .unwrap_or_default()
48 }
49}
50
51impl Display for Channel {
52 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53 write!(f, "{}", self.as_ref())
54 }
55}
56
57impl AsRef<str> for Channel {
58 fn as_ref(&self) -> &str {
59 match self {
60 Self::Unknown => "unknown",
61 Self::Mysql => "mysql",
62 Self::Postgres => "postgres",
63 Self::HttpSql => "httpsql",
64 Self::Prometheus => "prometheus",
65 Self::Otlp => "otlp",
66 Self::Grpc => "grpc",
67 Self::Influx => "influx",
68 Self::Opentsdb => "opentsdb",
69 Self::Loki => "loki",
70 Self::Elasticsearch => "elasticsearch",
71 Self::Jaeger => "jaeger",
72 Self::Log => "log",
73 Self::Promql => "promql",
74 Self::Splunk => "splunk",
75 Self::Internal => "internal",
76 }
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::Channel;
83
84 #[test]
85 fn test_channel_name() {
86 let expected = [
87 (1, "mysql"),
88 (2, "postgres"),
89 (3, "httpsql"),
90 (4, "prometheus"),
91 (5, "otlp"),
92 (6, "grpc"),
93 (7, "influx"),
94 (8, "opentsdb"),
95 (9, "loki"),
96 (10, "elasticsearch"),
97 (11, "jaeger"),
98 (12, "log"),
99 (13, "promql"),
100 (14, "splunk"),
101 (255, "internal"),
102 ];
103
104 for (value, name) in expected {
105 assert_eq!(name, Channel::from(value).as_ref());
106 }
107 assert_eq!("unknown", Channel::from(0).as_ref());
108 assert_eq!("unknown", Channel::from(15).as_ref());
109 assert_eq!("unknown", Channel::from(256).as_ref());
110 }
111}