use std::path::PathBuf;
use std::str::FromStr;
use unescape::unescape;
use crate::{read_file, Error, Result};
use crate::disk::{FileSystem, Partition};
const PROC_MOUNTS: &str = "/proc/mounts";
impl FromStr for Partition {
type Err = Error;
fn from_str(line: &str) -> Result<Partition> {
match line.split_whitespace().collect::<Vec<_>>() {
fields if fields.len() >= 4 => {
Ok(Partition {
device: String::from(fields[0]),
mountpoint: PathBuf::from(unescape(fields[1]).unwrap()), filesystem: FileSystem::from_str(fields[2]).unwrap(), mount_options: String::from(fields[3]),
})
}
_ => Err(Error::MissingData {
path: PROC_MOUNTS.into(),
contents: line.to_string(),
}),
}
}
}
pub fn partitions() -> Result<Vec<Partition>> {
read_file(PROC_MOUNTS)?
.lines()
.map(Partition::from_str)
.collect()
}