#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum RequestedProtocol {
#[default]
V0,
V1,
V2,
}
impl RequestedProtocol {
pub fn from_config(value: &[u8]) -> Option<Self> {
match value {
b"0" => Some(Self::V0),
b"1" => Some(Self::V1),
b"2" => Some(Self::V2),
_ => None,
}
}
#[cfg(any(
test,
feature = "http",
all(feature = "ssh", any(target_os = "macos", target_os = "linux"))
))]
pub(crate) fn parameter(self) -> Option<&'static str> {
match self {
Self::V0 => None,
Self::V1 => Some("version=1"),
Self::V2 => Some("version=2"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_only_exact_config_values() {
assert_eq!(
RequestedProtocol::from_config(b"2"),
Some(RequestedProtocol::V2)
);
assert_eq!(
RequestedProtocol::from_config(b"0"),
Some(RequestedProtocol::V0)
);
assert_eq!(RequestedProtocol::from_config(b"3"), None);
assert_eq!(RequestedProtocol::from_config(b" 2"), None);
assert_eq!(RequestedProtocol::V0.parameter(), None);
assert_eq!(RequestedProtocol::V2.parameter(), Some("version=2"));
}
}