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