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
use std::fmt;
use response;
#[derive(Deserialize, Debug, Clone)]
pub struct Network {
pub ip_address: String,
pub netmask: String,
pub gateway: String,
#[serde(rename = "type")]
pub network_type: String,
}
#[derive(Deserialize, Debug)]
pub struct Networks {
pub v4: Vec<Option<Network>>,
pub v6: Vec<Option<Network>>,
}
impl response::NotArray for Network {}
impl fmt::Display for Network {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"IP Address: {}\n\
Netmask: {}\n\
Gateway:{}\n\
Type: {}",
self.ip_address,
self.netmask,
self.gateway,
self.network_type)
}
}
impl fmt::Display for Networks {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "IPv4 Networks: {}\n\
IPv6 Networks: {}",
{
let v4 = self.v4
.iter()
.filter_map(|n| if n.is_some() {
Some(n.clone().unwrap().to_string())
}else{
None
})
.fold(String::new(),|acc, n| {
acc + &n.replace("\n", "\n\t")
});
if !v4.is_empty() {
format!("\n\t{}", v4)
} else {
"None".to_owned()
}
},
{
let v6 = self.v6
.iter()
.filter_map(|n| if n.is_some() {
Some(n.clone().unwrap().to_string())
}else{
None
})
.fold(String::new(),|acc, n| {
acc + &n.replace("\n", "\n\t")
});
if !v6.is_empty() {
format!("\n\t{}", v6)
} else {
"None".to_owned()
}
},
)
}
}