proc_status/
entry.rs

1use {
2    crate::errors::ProcStatusError,
3};
4
5/// a key:value line in the proc/status pseudo-file
6///
7/// key and value fields can be read directly
8pub struct ProcEntry<'p> {
9    pub key: &'p str,
10    pub value: &'p str,
11}
12
13impl<'p> ProcEntry<'p> {
14    pub(crate) fn new(src: &'p str) -> Result<Self, ProcStatusError> {
15        src.find(':')
16            .map(|i| Self {
17                key: &src[..i],
18                value: src[i+1..].trim(),
19            })
20            .ok_or_else(|| ProcStatusError::NoColon(src.to_string()))
21    }
22    /// the parsed number before 'kB', meaning the number of KiB
23    /// for a memory related value
24    #[allow(non_snake_case)]
25    pub fn in_KiB(&self) -> Result<usize, ProcStatusError> {
26        if let Some(number) = self.value.strip_suffix(" kB") {
27            Ok(number.parse()?)
28        } else {
29            Err(ProcStatusError::NotInKib)
30        }
31    }
32}