sysfs_class/
net.rs

1use crate::SysClass;
2use std::io::Result;
3use std::path::{Path, PathBuf};
4
5#[derive(Clone)]
6pub struct Net {
7    path: PathBuf,
8}
9
10impl SysClass for Net {
11    fn class() -> &'static str {
12        "net"
13    }
14
15    unsafe fn from_path_unchecked(path: PathBuf) -> Self {
16        Self { path }
17    }
18
19    fn path(&self) -> &Path {
20        &self.path
21    }
22}
23
24impl Net {
25    pub fn statistics(&self) -> NetStatistics {
26        NetStatistics { parent: self }
27    }
28
29    method!(addr_assign_type parse_file u8);
30    method!(addr_len parse_file u16);
31    method!(address trim_file String);
32    method!(broadcast trim_file String);
33    method!(carrier parse_file u16);
34    method!(carrier_changes parse_file u16);
35    method!(carrier_down_count parse_file u16);
36    method!(carrier_up_count parse_file u16);
37    method!(dev_id trim_file String);
38    method!(dev_port parse_file u16);
39    method!(dormant parse_file u8);
40    method!(duplex trim_file String);
41    method!(mtu parse_file u32);
42    method!(operstate trim_file String);
43    method!(speed parse_file u32);
44    method!(tx_queue_len parse_file u32);
45}
46
47pub struct NetStatistics<'a> {
48    parent: &'a Net,
49}
50
51impl<'a> NetStatistics<'a> {
52    const DIR: &'static str = "statistics";
53
54    pub fn rx_bytes(&self) -> Result<u64> {
55        self.parent.parse_file(&[Self::DIR, "/rx_bytes"].concat())
56    }
57
58    pub fn rx_packets(&self) -> Result<u64> {
59        self.parent.parse_file(&[Self::DIR, "/rx_packets"].concat())
60    }
61
62    pub fn tx_bytes(&self) -> Result<u64> {
63        self.parent.parse_file(&[Self::DIR, "/tx_bytes"].concat())
64    }
65
66    pub fn tx_packets(&self) -> Result<u64> {
67        self.parent.parse_file(&[Self::DIR, "/tx_packets"].concat())
68    }
69}