Skip to main content

krabka_security/
listener.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
4pub enum ListenerProtocol {
5    Plaintext,
6    Ssl,
7    SaslPlaintext,
8    SaslSsl,
9}
10
11impl ListenerProtocol {
12    #[must_use]
13    pub fn requires_tls(self) -> bool {
14        matches!(self, Self::Ssl | Self::SaslSsl)
15    }
16
17    #[must_use]
18    pub fn requires_sasl(self) -> bool {
19        matches!(self, Self::SaslPlaintext | Self::SaslSsl)
20    }
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    fn tls_and_sasl_flags_per_protocol() {
29        for (protocol, tls, sasl) in [
30            (ListenerProtocol::Plaintext, false, false),
31            (ListenerProtocol::Ssl, true, false),
32            (ListenerProtocol::SaslPlaintext, false, true),
33            (ListenerProtocol::SaslSsl, true, true),
34        ] {
35            assert2::assert!(protocol.requires_tls() == tls);
36            assert2::assert!(protocol.requires_sasl() == sasl);
37        }
38    }
39}