use std::path::PathBuf;
define_struct! {
pub struct Mount {
device: String,
mount_point: PathBuf,
fs_type: String,
mode: String,
dummy1: String,
dummy2: String,
}
}
use std::str::FromStr;
impl FromStr for Mount {
type Err = crate::ProcErr;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let columns: Vec<&str> = s.split_ascii_whitespace().collect();
if columns.len() != 6 {
return Err("require 6 fields to prase a Mount".into());
}
let device = columns[0].to_string();
let mount_point: PathBuf = columns[1].to_string().into();
let fs_type = columns[2].to_string();
let mode = columns[3].to_string();
let dummy1 = columns[4].to_string();
let dummy2 = columns[5].to_string();
Ok(Mount {
device, mount_point, fs_type,
mode, dummy1, dummy2,
})
}
}
list_impl! {
mounts, "/proc/mounts", Mount, '\n', 0
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_parse_mount() {
let source = "cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0";
let correct = Mount {
device: "cgroup".to_string(),
mount_point: "/sys/fs/cgroup/cpuset".to_string().into(),
fs_type: "cgroup".to_string(),
mode: "rw,nosuid,nodev,noexec,relatime,cpuset".to_string(),
dummy1: "0".to_string(),
dummy2: "0".to_string()
};
assert_eq!(correct, source.parse::<Mount>().unwrap());
}
}