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
use std::io::Result;
use std::path::{Path, PathBuf};
use SysClass;

#[derive(Clone)]
pub struct Net {
    path: PathBuf
}

impl SysClass for Net {
    fn class() -> &'static str {
        "net"
    }

    unsafe fn from_path_unchecked(path: PathBuf) -> Self {
        Self { path }
    }

    fn path(&self) -> &Path {
        &self.path
    }
}

impl Net {
    pub fn statistics(&self) -> NetStatistics {
        NetStatistics { parent: self }
    }

    method!(addr_assign_type parse_file u8);
    method!(addr_len parse_file u16);
    method!(address trim_file String);
    method!(broadcast trim_file String);
    method!(carrier parse_file u16);
    method!(carrier_changes parse_file u16);
    method!(carrier_down_count parse_file u16);
    method!(carrier_up_count parse_file u16);
    method!(dev_id trim_file String);
    method!(dev_port parse_file u16);
    method!(dormant parse_file u8);
    method!(duplex trim_file String);
    method!(mtu parse_file u32);
    method!(operstate trim_file String);
    method!(speed parse_file u32);
    method!(tx_queue_len parse_file u32);
}

pub struct NetStatistics<'a> {
    parent: &'a Net,
}

impl<'a> NetStatistics<'a> {
    const DIR: &'static str = "statistics";

    pub fn rx_bytes(&self) -> Result<u64> {
        self.parent.parse_file(&[Self::DIR, "/rx_bytes"].concat())
    }

    pub fn rx_packets(&self) -> Result<u64> {
        self.parent.parse_file(&[Self::DIR, "/rx_packets"].concat())
    }

    pub fn tx_bytes(&self) -> Result<u64> {
        self.parent.parse_file(&[Self::DIR, "/tx_bytes"].concat())
    }

    pub fn tx_packets(&self) -> Result<u64> {
        self.parent.parse_file(&[Self::DIR, "/tx_packets"].concat())
    }
}