define_struct! {
pub struct FileSystem {
nodev: bool,
fs_type: String,
}
}
use std::str::FromStr;
impl FromStr for FileSystem {
type Err = crate::ProcErr;
fn from_str(value: &str) -> Result<Self, crate::ProcErr> {
let nodev = value.starts_with("nodev");
let fs_type = if nodev {
value.trim_start_matches("nodev").trim().to_string()
}else {
value.trim().to_string()
};
Ok(FileSystem{
nodev, fs_type
})
}
}
list_impl! {
filesystems, "/proc/filesystems", FileSystem, '\n', 0
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_parse() {
let source = "nodev sysfs";
let correct = FileSystem {
nodev: true,
fs_type: "sysfs".to_string()
};
assert_eq!(correct, source.parse().unwrap());
let source = "sysfs";
let correct = FileSystem {
nodev: false,
fs_type: "sysfs".to_string()
};
assert_eq!(correct, source.parse().unwrap());
let source = " sysfs";
let correct = FileSystem {
nodev: false,
fs_type: "sysfs".to_string()
};
assert_eq!(correct, source.parse().unwrap());
}
}