1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7#[serde(rename_all = "lowercase")]
8pub enum Protocol {
9 Http,
10 Https,
11 WebSocket,
12 Wss,
13 Grpc,
14 Ssh,
15 Ftp,
16 Sftp,
17 Smtp,
18 Imap,
19 Dns,
20 Mqtt,
21 Amqp,
22 Redis,
23 Postgres,
24 Mysql,
25 Tcp,
26 Udp,
27}
28
29impl Protocol {
30 pub fn default_port(&self) -> Option<u16> {
32 match self {
33 Protocol::Http => Some(80),
34 Protocol::Https => Some(443),
35 Protocol::WebSocket => Some(80),
36 Protocol::Wss => Some(443),
37 Protocol::Grpc => Some(443),
38 Protocol::Ssh => Some(22),
39 Protocol::Ftp => Some(21),
40 Protocol::Sftp => Some(22),
41 Protocol::Smtp => Some(587),
42 Protocol::Imap => Some(993),
43 Protocol::Dns => Some(53),
44 Protocol::Mqtt => Some(1883),
45 Protocol::Amqp => Some(5672),
46 Protocol::Redis => Some(6379),
47 Protocol::Postgres => Some(5432),
48 Protocol::Mysql => Some(3306),
49 Protocol::Tcp | Protocol::Udp => None,
50 }
51 }
52
53 pub fn supports_tls(&self) -> bool {
55 matches!(
56 self,
57 Protocol::Https
58 | Protocol::Wss
59 | Protocol::Grpc
60 | Protocol::Sftp
61 | Protocol::Imap
62 )
63 }
64
65 pub fn from_scheme(scheme: &str) -> Option<Self> {
67 match scheme.to_lowercase().as_str() {
68 "http" => Some(Protocol::Http),
69 "https" => Some(Protocol::Https),
70 "ws" => Some(Protocol::WebSocket),
71 "wss" => Some(Protocol::Wss),
72 "grpc" | "grpcs" => Some(Protocol::Grpc),
73 "ssh" => Some(Protocol::Ssh),
74 "ftp" => Some(Protocol::Ftp),
75 "sftp" => Some(Protocol::Sftp),
76 "smtp" | "smtps" => Some(Protocol::Smtp),
77 "imap" | "imaps" => Some(Protocol::Imap),
78 "dns" => Some(Protocol::Dns),
79 "mqtt" | "mqtts" => Some(Protocol::Mqtt),
80 "amqp" | "amqps" => Some(Protocol::Amqp),
81 "redis" | "rediss" => Some(Protocol::Redis),
82 "postgres" | "postgresql" => Some(Protocol::Postgres),
83 "mysql" => Some(Protocol::Mysql),
84 _ => None,
85 }
86 }
87
88 pub fn name(&self) -> &'static str {
90 match self {
91 Protocol::Http => "HTTP",
92 Protocol::Https => "HTTPS",
93 Protocol::WebSocket => "WebSocket",
94 Protocol::Wss => "WebSocket Secure",
95 Protocol::Grpc => "gRPC",
96 Protocol::Ssh => "SSH",
97 Protocol::Ftp => "FTP",
98 Protocol::Sftp => "SFTP",
99 Protocol::Smtp => "SMTP",
100 Protocol::Imap => "IMAP",
101 Protocol::Dns => "DNS",
102 Protocol::Mqtt => "MQTT",
103 Protocol::Amqp => "AMQP",
104 Protocol::Redis => "Redis",
105 Protocol::Postgres => "PostgreSQL",
106 Protocol::Mysql => "MySQL",
107 Protocol::Tcp => "TCP",
108 Protocol::Udp => "UDP",
109 }
110 }
111}
112
113#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct ProtocolCapabilities {
116 pub request_response: bool,
117 pub streaming: bool,
118 pub pub_sub: bool,
119 pub bidirectional: bool,
120 pub supports_tls: bool,
121 pub supports_compression: bool,
122}
123
124impl Protocol {
125 pub fn capabilities(&self) -> ProtocolCapabilities {
127 match self {
128 Protocol::Http | Protocol::Https => ProtocolCapabilities {
129 request_response: true,
130 streaming: false,
131 pub_sub: false,
132 bidirectional: false,
133 supports_tls: matches!(self, Protocol::Https),
134 supports_compression: true,
135 },
136 Protocol::WebSocket | Protocol::Wss => ProtocolCapabilities {
137 request_response: true,
138 streaming: true,
139 pub_sub: true,
140 bidirectional: true,
141 supports_tls: matches!(self, Protocol::Wss),
142 supports_compression: true,
143 },
144 Protocol::Grpc => ProtocolCapabilities {
145 request_response: true,
146 streaming: true,
147 pub_sub: false,
148 bidirectional: true,
149 supports_tls: true,
150 supports_compression: true,
151 },
152 Protocol::Mqtt | Protocol::Amqp => ProtocolCapabilities {
153 request_response: false,
154 streaming: true,
155 pub_sub: true,
156 bidirectional: true,
157 supports_tls: false,
158 supports_compression: false,
159 },
160 _ => ProtocolCapabilities {
161 request_response: true,
162 streaming: false,
163 pub_sub: false,
164 bidirectional: matches!(self, Protocol::Ssh),
165 supports_tls: self.supports_tls(),
166 supports_compression: false,
167 },
168 }
169 }
170}