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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
use crate::{Error, Result};
use std::collections::HashMap;
use std::ops::Deref;
use std::str::FromStr;

/// represent an entry in /proc/crypto
///
/// ```
/// use proc_getter::crypto::*;
///
/// let info = crypto().unwrap();
/// assert_eq!(info[0].name(), info[0].get("name").unwrap());
/// ```
#[derive(Debug)]
pub struct Crypto(HashMap<String, String>);

impl Crypto {
    pub fn name(&self) -> &str {
        self.get("name").unwrap()
    }

    pub fn driver(&self) -> &str {
        self.get("driver").unwrap()
    }

    pub fn module(&self) -> &str {
        self.get("module").unwrap()
    }

    pub fn selftest(&self) -> bool {
        self.get("selftest").unwrap() == "passed"
    }

    pub fn refcnt(&self) -> usize {
        self.get("refcnt").unwrap().parse::<usize>().unwrap()
    }

    pub fn priority(&self) -> usize {
        self.get("priority").unwrap().parse::<usize>().unwrap()
    }

    pub fn r#type(&self) -> &str {
        self.get("type").unwrap()
    }
}

impl Deref for Crypto {
    type Target = HashMap<String, String>;

    fn deref(&self) -> &HashMap<String, String> {
        &self.0
    }
}

impl FromStr for Crypto {
    type Err = Error;

    fn from_str(value: &str) -> Result<Self> {
        let mut ret = HashMap::new();
        for line in value.trim().lines() {
            let columns: Vec<&str> = line.split(':').collect();
            if columns.len() != 2 {
                return Err(Error::BadFormat);
            }
            ret.insert(columns[0].trim().to_string(), columns[1].trim().to_string());
        }
        Ok(Crypto(ret))
    }
}

#[inline(always)]
fn to_crypto(block: &str) -> Result<Crypto> {
    Crypto::from_str(block)
}

default_list! {
    crypto, "/proc/crypto", Crypto, to_crypto, "\n\n"
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn getter() {
        let source = r#"
name         : sha1
driver       : sha1-ssse3
module       : kernel
priority     : 150
refcnt       : 5
selftest     : passed
type         : shash
blocksize    : 64
digestsize   : 20
"#;
        let c = Crypto::from_str(source.trim()).unwrap();
        assert_eq!(c.get("name").unwrap(), c.name());
        assert_eq!(c.get("driver").unwrap(), c.driver());
        assert_eq!(c.get("module").unwrap(), c.module());
        assert_eq!(c.get("priority").unwrap(), &c.priority().to_string());
        assert_eq!(c.get("refcnt").unwrap(), &c.refcnt().to_string());
        assert_eq!(c.get("selftest").unwrap() == "passed", c.selftest());
        assert_eq!(c.get("type").unwrap(), c.r#type());
    }
}