1use std::fmt;
2use std::str::FromStr;
3
4use crate::Error;
5
6#[derive(Debug, PartialEq)]
7pub enum Protocol {
8 Ethernet,
9 UDP
10}
11
12impl fmt::Display for Protocol {
13 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14 let s = match self {
15 Protocol::Ethernet => "ethernet",
16 Protocol::UDP => "udp"
17 };
18 write!(f, "{}", s)
19 }
20}
21
22impl FromStr for Protocol {
23 type Err = Error;
24
25 fn from_str(s: &str) -> Result<Self, Self::Err> {
26 match s {
27 "ethernet" => Ok(Protocol::Ethernet),
28 "udp" => Ok(Protocol::UDP),
29 _ => Err(Error::BadInput(format!("Unknown ddlnk::Protocol '{}'", s)))
30 }
31 }
32}
33
34
35#[derive(Debug, PartialEq)]
36pub enum ProtImpl {
37 Pcap,
38 Generic
39}
40
41impl fmt::Display for ProtImpl {
42 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43 let s = match self {
44 ProtImpl::Pcap => "pcap",
45 ProtImpl::Generic => "generic"
46 };
47 write!(f, "{}", s)
48 }
49}
50
51impl FromStr for ProtImpl {
52 type Err = Error;
53
54 fn from_str(s: &str) -> Result<Self, Self::Err> {
55 match s {
56 "pcap" => Ok(ProtImpl::Pcap),
57 "generic" => Ok(ProtImpl::Generic),
58 _ => Err(Error::BadInput(format!("Unknown ddlnk::ProtImpl '{}'", s)))
59 }
60 }
61}
62
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn protocol_to_string() {
70 let t = Protocol::Ethernet;
71 let s = format!("{}", t);
72 assert_eq!(s, "ethernet");
73
74 let t = Protocol::UDP;
75 let s = format!("{}", t);
76 assert_eq!(s, "udp");
77 }
78
79 #[test]
80 fn protimpl_to_string() {
81 let t = ProtImpl::Pcap;
82 let s = format!("{}", t);
83 assert_eq!(s, "pcap");
84
85 let t = ProtImpl::Generic;
86 let s = format!("{}", t);
87 assert_eq!(s, "generic");
88 }
89
90 #[test]
91 fn string_to_protocol() {
92 let t = "ethernet".parse::<Protocol>().unwrap();
93 assert_eq!(t, Protocol::Ethernet);
94
95 let t = "udp".parse::<Protocol>().unwrap();
96 assert_eq!(t, Protocol::UDP);
97 }
98
99 #[test]
100 fn string_to_protimpl() {
101 let t = "pcap".parse::<ProtImpl>().unwrap();
102 assert_eq!(t, ProtImpl::Pcap);
103
104 let t = "generic".parse::<ProtImpl>().unwrap();
105 assert_eq!(t, ProtImpl::Generic);
106 }
107}
108
109