Skip to main content

kellnr_settings/
protocol.rs

1use clap::ValueEnum;
2use serde::{Deserialize, Deserializer, Serialize};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, ValueEnum)]
5pub enum Protocol {
6    #[default]
7    Http,
8    Https,
9}
10
11impl std::fmt::Display for Protocol {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        match self {
14            Protocol::Http => write!(f, "http"),
15            Protocol::Https => write!(f, "https"),
16        }
17    }
18}
19
20impl<'de> Deserialize<'de> for Protocol {
21    fn deserialize<D>(de: D) -> Result<Self, D::Error>
22    where
23        D: Deserializer<'de>,
24    {
25        match String::deserialize(de)?.to_lowercase().as_str() {
26            "http" => Ok(Self::Http),
27            "https" => Ok(Self::Https),
28            other => Err(serde::de::Error::custom(format!(
29                "unknown protocol {other:?}; expected \"http\" or \"https\""
30            ))),
31        }
32    }
33}
34
35impl Serialize for Protocol {
36    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
37    where
38        S: serde::Serializer,
39    {
40        serializer.serialize_str(&self.to_string())
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use serde::Deserialize;
47
48    use super::*;
49
50    #[test]
51    fn test_protocol_display() {
52        assert_eq!(Protocol::Http.to_string(), "http");
53        assert_eq!(Protocol::Https.to_string(), "https");
54    }
55
56    #[derive(Debug, Deserialize)]
57    struct Settings {
58        protocol: Protocol,
59    }
60
61    #[test]
62    fn test_deserialize_protocol_https() {
63        let toml = r#"
64            protocol = "https"
65        "#;
66
67        let settings: Settings = toml::from_str(toml).unwrap();
68        assert_eq!(settings.protocol, Protocol::Https);
69    }
70
71    #[test]
72    fn test_deserialize_protocol_http() {
73        let toml = r#"
74            protocol = "http"
75        "#;
76
77        let settings: Settings = toml::from_str(toml).unwrap();
78        assert_eq!(settings.protocol, Protocol::Http);
79    }
80
81    #[test]
82    fn test_deserialize_protocol_uppercase() {
83        let toml = r#"
84            protocol = "HTTPS"
85        "#;
86
87        let settings: Settings = toml::from_str(toml).unwrap();
88        assert_eq!(settings.protocol, Protocol::Https);
89    }
90
91    #[test]
92    fn test_deserialize_protocol_error() {
93        let toml = r#"
94            protocol = "ftp"
95        "#;
96
97        let settings: Result<Settings, toml::de::Error> = toml::from_str(toml);
98        assert!(settings.is_err());
99    }
100
101    #[test]
102    fn test_serialize_protocol_http() {
103        let protocol = Protocol::Http;
104        let json = serde_json::to_string(&protocol).unwrap();
105        assert_eq!(json, r#""http""#);
106    }
107
108    #[test]
109    fn test_serialize_protocol_https() {
110        let protocol = Protocol::Https;
111        let json = serde_json::to_string(&protocol).unwrap();
112        assert_eq!(json, r#""https""#);
113    }
114}