ddmw_client/types/node/
ddlnk.rs

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