confluent_kafka/common/config/
ssl_client_auth.rs1use std::fmt;
20use std::str::FromStr;
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
24#[non_exhaustive]
25#[doc(alias = "org.apache.kafka.common.config.SslClientAuth")]
26pub enum SslClientAuth {
27 Required,
29 Requested,
31 #[default]
33 None,
34}
35
36impl SslClientAuth {
37 const ALL: [SslClientAuth; 3] = [SslClientAuth::Required, SslClientAuth::Requested, SslClientAuth::None];
39
40 fn name(self) -> &'static str {
42 match self {
43 SslClientAuth::Required => "REQUIRED",
44 SslClientAuth::Requested => "REQUESTED",
45 SslClientAuth::None => "NONE",
46 }
47 }
48
49 #[doc(alias = "org.apache.kafka.common.config.SslClientAuth#forConfig")]
60 pub fn for_config(key: Option<&str>) -> Option<Self> {
61 match key {
62 Option::None => Some(SslClientAuth::None),
63 Some(k) => {
64 let upper = k.to_uppercase();
65 Self::ALL.iter().find(|a| a.name() == upper).copied()
66 },
67 }
68 }
69}
70
71impl fmt::Display for SslClientAuth {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 write!(f, "{}", self.name().to_lowercase())
75 }
76}
77
78impl FromStr for SslClientAuth {
79 type Err = String;
80
81 fn from_str(s: &str) -> Result<Self, Self::Err> {
83 let upper = s.to_uppercase();
84 SslClientAuth::ALL
85 .iter()
86 .find(|a| a.name() == upper)
87 .copied()
88 .ok_or_else(|| format!("No enum constant SslClientAuth.{}", s))
89 }
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95
96 #[test]
97 fn test_default() {
98 assert_eq!(SslClientAuth::default(), SslClientAuth::None);
99 }
100
101 #[test]
102 fn test_for_config_none_key() {
103 assert_eq!(SslClientAuth::for_config(Option::None), Some(SslClientAuth::None));
104 }
105
106 #[test]
107 fn test_for_config_required() {
108 assert_eq!(SslClientAuth::for_config(Some("REQUIRED")), Some(SslClientAuth::Required));
109 }
110
111 #[test]
112 fn test_for_config_requested() {
113 assert_eq!(SslClientAuth::for_config(Some("REQUESTED")), Some(SslClientAuth::Requested));
114 }
115
116 #[test]
117 fn test_for_config_none_variant() {
118 assert_eq!(SslClientAuth::for_config(Some("NONE")), Some(SslClientAuth::None));
119 }
120
121 #[test]
122 fn test_for_config_case_insensitive() {
123 assert_eq!(SslClientAuth::for_config(Some("required")), Some(SslClientAuth::Required));
124 assert_eq!(SslClientAuth::for_config(Some("Requested")), Some(SslClientAuth::Requested));
125 assert_eq!(SslClientAuth::for_config(Some("none")), Some(SslClientAuth::None));
126 }
127
128 #[test]
129 fn test_for_config_invalid() {
130 assert_eq!(SslClientAuth::for_config(Some("INVALID")), Option::None);
131 assert_eq!(SslClientAuth::for_config(Some("")), Option::None);
132 }
133
134 #[test]
135 fn test_display() {
136 assert_eq!(format!("{}", SslClientAuth::Required), "required");
137 assert_eq!(format!("{}", SslClientAuth::Requested), "requested");
138 assert_eq!(format!("{}", SslClientAuth::None), "none");
139 }
140
141 #[test]
142 fn test_from_str_roundtrip() {
143 for variant in &SslClientAuth::ALL {
144 let name = variant.name();
145 let parsed: SslClientAuth = name.parse().unwrap();
146 assert_eq!(*variant, parsed);
147 }
148 }
149
150 #[test]
151 fn test_from_str_case_insensitive() {
152 assert_eq!("required".parse::<SslClientAuth>().unwrap(), SslClientAuth::Required);
153 assert_eq!("None".parse::<SslClientAuth>().unwrap(), SslClientAuth::None);
154 }
155
156 #[test]
157 fn test_from_str_invalid() {
158 assert!("INVALID".parse::<SslClientAuth>().is_err());
159 assert!("".parse::<SslClientAuth>().is_err());
160 }
161
162 #[test]
163 fn test_clone_and_copy() {
164 let a = SslClientAuth::Required;
165 let b = a;
166 assert_eq!(a, b);
167 }
168}