Skip to main content

common_base/
protocol.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::{Display, Formatter};
16
17/// The protocol through which a query is received.
18#[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}
39
40impl From<u32> for Channel {
41    fn from(value: u32) -> Self {
42        u8::try_from(value)
43            .ok()
44            .and_then(Self::from_repr)
45            .unwrap_or_default()
46    }
47}
48
49impl Display for Channel {
50    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
51        write!(f, "{}", self.as_ref())
52    }
53}
54
55impl AsRef<str> for Channel {
56    fn as_ref(&self) -> &str {
57        match self {
58            Self::Unknown => "unknown",
59            Self::Mysql => "mysql",
60            Self::Postgres => "postgres",
61            Self::HttpSql => "httpsql",
62            Self::Prometheus => "prometheus",
63            Self::Otlp => "otlp",
64            Self::Grpc => "grpc",
65            Self::Influx => "influx",
66            Self::Opentsdb => "opentsdb",
67            Self::Loki => "loki",
68            Self::Elasticsearch => "elasticsearch",
69            Self::Jaeger => "jaeger",
70            Self::Log => "log",
71            Self::Promql => "promql",
72            Self::Splunk => "splunk",
73        }
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::Channel;
80
81    #[test]
82    fn test_channel_name() {
83        let expected = [
84            (1, "mysql"),
85            (2, "postgres"),
86            (3, "httpsql"),
87            (4, "prometheus"),
88            (5, "otlp"),
89            (6, "grpc"),
90            (7, "influx"),
91            (8, "opentsdb"),
92            (9, "loki"),
93            (10, "elasticsearch"),
94            (11, "jaeger"),
95            (12, "log"),
96            (13, "promql"),
97            (14, "splunk"),
98        ];
99
100        for (value, name) in expected {
101            assert_eq!(name, Channel::from(value).as_ref());
102        }
103        assert_eq!("unknown", Channel::from(0).as_ref());
104        assert_eq!("unknown", Channel::from(15).as_ref());
105    }
106}