impl Display for Mount

This commit is contained in:
Rowan 2025-03-05 19:58:33 -06:00
parent 489cbcc824
commit c2aeb5ac96
2 changed files with 40 additions and 26 deletions

View file

@ -160,18 +160,14 @@ impl FromStr for DeviceId {
impl Display for DeviceId { impl Display for DeviceId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( match self {
f, DeviceId::Label(label) => write!(f, "LABEL={label}"),
"{}", DeviceId::Uuid(uuid) => write!(f, "UUID={uuid}"),
match self { DeviceId::PartLabel(label) => write!(f, "PARTLABEL={label}"),
DeviceId::Label(label) => format!("LABEL={label}"), DeviceId::PartUuid(uuid) => write!(f, "PARTUUID={uuid}"),
DeviceId::Uuid(uuid) => format!("UUID={uuid}"), DeviceId::Id(id) => write!(f, "ID={id}"),
DeviceId::PartLabel(label) => format!("PARTLABEL={label}"), DeviceId::Other(other) => write!(f, "{other}"),
DeviceId::PartUuid(uuid) => format!("PARTUUID={uuid}"), }
DeviceId::Id(id) => format!("ID={id}"),
DeviceId::Other(other) => other.to_string(),
}
)
} }
} }
@ -841,11 +837,12 @@ impl Display for MountOperation {
} }
} }
pub type MountOptions = DelimitedList<' ', MountOption>;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Mount { pub struct Mount {
device_id: DeviceId, source: DeviceId,
mountpoint: PathBuf, mountpoint: PathBuf,
options: DelimitedList<',', MountOption>, options: MountOptions,
} }
impl Mount { impl Mount {
@ -855,22 +852,16 @@ impl Mount {
I: IntoIterator<Item = T>, I: IntoIterator<Item = T>,
{ {
Self { Self {
device_id: source.into(), source: source.into(),
mountpoint: target.into(), mountpoint: target.into(),
options: DelimitedList::<',', MountOption>::from_iter( options: MountOptions::from_iter(options.into_iter().map(Into::into)),
options.into_iter().map(Into::into),
),
} }
} }
} }
impl FileSystem for Mount { impl FileSystem for Mount {
fn mount(&mut self) -> std::io::Result<()> { fn mount(&mut self) -> std::io::Result<()> {
mount( mount(self.source.clone(), &self.mountpoint, self.options.clone())?;
self.device_id.clone(),
&self.mountpoint,
self.options.clone(),
)?;
Ok(()) Ok(())
} }
@ -880,6 +871,19 @@ impl FileSystem for Mount {
} }
} }
impl Display for Mount {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "mount")?;
if !self.options.is_empty() {
write!(f, " -o {}", self.options)?;
}
write!(f, " {}", self.source)?;
write!(f, " {}", self.mountpoint.display())
}
}
pub fn mount<T: Into<MountOption>, I: IntoIterator<Item = T>>( pub fn mount<T: Into<MountOption>, I: IntoIterator<Item = T>>(
source: impl Into<DeviceId>, source: impl Into<DeviceId>,
target: impl Into<PathBuf>, target: impl Into<PathBuf>,
@ -912,8 +916,8 @@ mod tests {
}; };
use super::{ use super::{
Access, DeviceId, KeyValuePair, MapUsers, MountOperation, MountOption, OptionsMode, Access, DeviceId, KeyValuePair, MapUsers, Mount, MountOperation, MountOption, MountOptions,
OptionsSource, ParseMountOptionError, Subtree, UncheckedOptions, OptionsMode, OptionsSource, ParseMountOptionError, Subtree, UncheckedOptions,
}; };
#[test] #[test]
@ -1253,4 +1257,15 @@ mod tests {
Subtree::Unbindable, Subtree::Unbindable,
))); )));
} }
#[test]
fn mount_display() {
let mnt = Mount::new(
DeviceId::Label("awawa".into()),
"/mnt",
MountOptions::default(),
);
assert_eq!(mnt.to_string(), "mount LABEL=awawa /mnt");
}
} }

View file

@ -154,7 +154,6 @@ impl<T: AsRef<OsStr>> From<T> for DisplayString {
Self(value.as_ref().into()) Self(value.as_ref().into())
} }
} }
impl Display for DisplayString { impl Display for DisplayString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.display()) write!(f, "{}", self.0.display())