use std::fmt;
use std::str::FromStr;
#[derive(Debug, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "lowercase")
)]
pub enum FsType {
Proc,
Overlay,
Tmpfs,
Sysfs,
Btrfs,
Ext2,
Ext3,
Ext4,
Devtmpfs,
#[cfg_attr(feature = "serde", serde(untagged))]
Other(String),
}
impl FromStr for FsType {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"proc" => Ok(FsType::Proc),
"tmpfs" => Ok(FsType::Tmpfs),
"overlay" => Ok(FsType::Overlay),
"sysfs" => Ok(FsType::Sysfs),
"btrfs" => Ok(FsType::Btrfs),
"ext2" => Ok(FsType::Ext2),
"ext3" => Ok(FsType::Ext3),
"ext4" => Ok(FsType::Ext4),
"devtmpfs" => Ok(FsType::Devtmpfs),
_ => Ok(FsType::Other(s.to_string())),
}
}
}
impl fmt::Display for FsType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fsname = match self {
FsType::Proc => "proc",
FsType::Overlay => "overlay",
FsType::Tmpfs => "tmpfs",
FsType::Sysfs => "sysfs",
FsType::Btrfs => "btrfs",
FsType::Ext2 => "ext2",
FsType::Ext3 => "ext3",
FsType::Ext4 => "ext4",
FsType::Devtmpfs => "devtmpfs",
FsType::Other(fsname) => fsname,
};
write!(f, "{}", fsname)
}
}
#[cfg(test)]
mod tests {
#[test]
#[cfg(feature = "serde")]
fn test_serde_fstype_ext4() {
use crate::FsType;
use serde_json::Value;
assert_eq!(
Value::String("ext4".into()),
serde_json::to_value(FsType::Ext4).unwrap()
);
assert_eq!(
FsType::Ext4,
serde_json::from_str::<FsType>("\"ext4\"").unwrap()
);
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_fstype_other() {
use crate::FsType;
use serde_json::Value;
assert_eq!(
Value::String("testexmaplefs".into()),
serde_json::to_value(FsType::Other("testexmaplefs".into())).unwrap()
);
assert_eq!(
FsType::Other("testexmaplefs".into()),
serde_json::from_str::<FsType>("\"testexmaplefs\"").unwrap()
);
}
}