1use std::str::FromStr;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4pub enum Transport {
5 Tcp,
6 Unix,
7}
8
9impl FromStr for Transport {
10 type Err = &'static str;
11
12 fn from_str(value: &str) -> Result<Self, Self::Err> {
13 match value {
14 "tcp" => Ok(Self::Tcp),
15 "unix" => Ok(Self::Unix),
16 _ => Err("transport must be tcp or unix"),
17 }
18 }
19}
20
21#[cfg(test)]
22mod tests {
23 use super::*;
24
25 #[test]
26 fn parses_only_supported_transport_names() {
27 assert_eq!("tcp".parse(), Ok(Transport::Tcp));
28 assert_eq!("unix".parse(), Ok(Transport::Unix));
29 for value in ["", "TCP", "Unix", "udp", " tcp", "unix\n"] {
30 assert_eq!(
31 value.parse::<Transport>(),
32 Err("transport must be tcp or unix"),
33 "{value:?}"
34 );
35 }
36 }
37}