openvpn_mgmt_codec/
stream_mode.rs1use std::fmt;
2use std::str::FromStr;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum StreamMode {
9 On,
11
12 Off,
14
15 All,
17
18 OnAll,
22
23 Recent(u32),
25}
26
27impl fmt::Display for StreamMode {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 match self {
30 Self::On => f.write_str("on"),
31 Self::Off => f.write_str("off"),
32 Self::All => f.write_str("all"),
33 Self::OnAll => f.write_str("on all"),
34 Self::Recent(n) => write!(f, "{n}"),
35 }
36 }
37}
38
39impl FromStr for StreamMode {
40 type Err = String;
41
42 fn from_str(s: &str) -> Result<Self, Self::Err> {
44 match s {
45 "on" => Ok(Self::On),
46 "off" => Ok(Self::Off),
47 "all" => Ok(Self::All),
48 "on all" => Ok(Self::OnAll),
49 n => n
50 .parse::<u32>()
51 .map(Self::Recent)
52 .map_err(|_| format!("invalid stream mode: {s} (use on/off/all/on all/N)")),
53 }
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn parse_roundtrip() {
63 for mode in [
64 StreamMode::On,
65 StreamMode::Off,
66 StreamMode::All,
67 StreamMode::OnAll,
68 StreamMode::Recent(42),
69 ] {
70 let s = mode.to_string();
71 assert_eq!(s.parse::<StreamMode>().unwrap(), mode);
72 }
73 }
74
75 #[test]
76 fn parse_invalid() {
77 assert!("bogus".parse::<StreamMode>().is_err());
78 }
79}