proc_getter/proc/
swaps.rs

1use crate::{Error, Result};
2use std::str::FromStr;
3
4/// represent an entry in /proc/swaps
5#[derive(Debug)]
6pub struct Swap {
7    filename: String,
8    r#type: String,
9    size: usize,
10    used: usize,
11    priority: isize,
12}
13
14impl Swap {
15    getter_gen! {
16        filename: String,
17        r#type: String,
18        size: usize,
19        used: usize,
20        priority: isize
21    }
22}
23
24impl FromStr for Swap {
25    type Err = Error;
26
27    fn from_str(value: &str) -> Result<Self> {
28        let columns: Vec<&str> = value.split_ascii_whitespace().collect();
29        if columns.len() != 5 {
30            return Err(Error::BadFormat);
31        }
32        let filename = columns[0].to_string();
33        let r#type = columns[1].to_string();
34        let size = columns[2].parse::<usize>()?;
35        let used = columns[3].parse::<usize>()?;
36        let priority = columns[4].parse::<isize>()?;
37        Ok(Swap {
38            filename,
39            r#type,
40            size,
41            used,
42            priority,
43        })
44    }
45}
46
47#[inline(always)]
48fn to_swaps(line: &str) -> Result<Swap> {
49    Swap::from_str(line)
50}
51
52default_list! {
53    swaps, "/proc/swaps", Swap, to_swaps, '\n', 1
54}