use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[allow(clippy::enum_variant_names)] pub enum ProxyMode {
#[default]
ProxyOnce,
ProxyAlways,
ProxyTransparent,
}
impl ProxyMode {
const TABLE: [(&'static str, Self); 3] = [
("proxyOnce", Self::ProxyOnce),
("proxyAlways", Self::ProxyAlways),
("proxyTransparent", Self::ProxyTransparent),
];
pub const SPELLINGS: [&'static str; 3] = {
let mut spellings = [""; 3];
let mut i = 0;
while i < spellings.len() {
spellings[i] = Self::TABLE[i].0;
i += 1;
}
spellings
};
#[must_use]
pub fn parse(mode: &str) -> Option<Self> {
let mode = mode.trim();
if mode.is_empty() {
return Some(Self::ProxyOnce);
}
Self::TABLE
.iter()
.find(|(spelling, _)| spelling.eq_ignore_ascii_case(mode))
.map(|(_, parsed)| *parsed)
}
}
#[cfg(test)]
mod tests {
use super::ProxyMode;
#[test]
fn spellings_parse_to_their_mode() {
let modes: Vec<ProxyMode> = ProxyMode::SPELLINGS
.iter()
.map(|s| ProxyMode::parse(s).unwrap_or_else(|| panic!("{s} does not parse")))
.collect();
assert_eq!(
modes,
[
ProxyMode::ProxyOnce,
ProxyMode::ProxyAlways,
ProxyMode::ProxyTransparent
]
);
for (spelling, mode) in ProxyMode::SPELLINGS.iter().zip(modes) {
assert_eq!(
serde_json::to_value(mode).unwrap(),
serde_json::json!(spelling)
);
}
assert_eq!(ProxyMode::parse(""), Some(ProxyMode::ProxyOnce));
assert_eq!(
ProxyMode::parse(" PROXYALWAYS "),
Some(ProxyMode::ProxyAlways)
);
}
}