diff --git a/src/fs/mount.rs b/src/fs/mount.rs index 875ada1..d351476 100644 --- a/src/fs/mount.rs +++ b/src/fs/mount.rs @@ -160,18 +160,14 @@ impl FromStr for DeviceId { impl Display for DeviceId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - match self { - DeviceId::Label(label) => format!("LABEL={label}"), - DeviceId::Uuid(uuid) => format!("UUID={uuid}"), - DeviceId::PartLabel(label) => format!("PARTLABEL={label}"), - DeviceId::PartUuid(uuid) => format!("PARTUUID={uuid}"), - DeviceId::Id(id) => format!("ID={id}"), - DeviceId::Other(other) => other.to_string(), - } - ) + match self { + DeviceId::Label(label) => write!(f, "LABEL={label}"), + DeviceId::Uuid(uuid) => write!(f, "UUID={uuid}"), + DeviceId::PartLabel(label) => write!(f, "PARTLABEL={label}"), + DeviceId::PartUuid(uuid) => write!(f, "PARTUUID={uuid}"), + DeviceId::Id(id) => write!(f, "ID={id}"), + DeviceId::Other(other) => write!(f, "{other}"), + } } } @@ -841,11 +837,12 @@ impl Display for MountOperation { } } +pub type MountOptions = DelimitedList<' ', MountOption>; #[derive(Clone, Debug, PartialEq, Eq)] pub struct Mount { - device_id: DeviceId, + source: DeviceId, mountpoint: PathBuf, - options: DelimitedList<',', MountOption>, + options: MountOptions, } impl Mount { @@ -855,22 +852,16 @@ impl Mount { I: IntoIterator, { Self { - device_id: source.into(), + source: source.into(), mountpoint: target.into(), - options: DelimitedList::<',', MountOption>::from_iter( - options.into_iter().map(Into::into), - ), + options: MountOptions::from_iter(options.into_iter().map(Into::into)), } } } impl FileSystem for Mount { fn mount(&mut self) -> std::io::Result<()> { - mount( - self.device_id.clone(), - &self.mountpoint, - self.options.clone(), - )?; + mount(self.source.clone(), &self.mountpoint, self.options.clone())?; 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, I: IntoIterator>( source: impl Into, target: impl Into, @@ -912,8 +916,8 @@ mod tests { }; use super::{ - Access, DeviceId, KeyValuePair, MapUsers, MountOperation, MountOption, OptionsMode, - OptionsSource, ParseMountOptionError, Subtree, UncheckedOptions, + Access, DeviceId, KeyValuePair, MapUsers, Mount, MountOperation, MountOption, MountOptions, + OptionsMode, OptionsSource, ParseMountOptionError, Subtree, UncheckedOptions, }; #[test] @@ -1253,4 +1257,15 @@ mod tests { 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"); + } } diff --git a/src/utils.rs b/src/utils.rs index 48cf104..67fb842 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -154,7 +154,6 @@ impl> From for DisplayString { Self(value.as_ref().into()) } } - impl Display for DisplayString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0.display())