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 {
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(),
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<Item = T>,
{
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<T: Into<MountOption>, I: IntoIterator<Item = T>>(
source: impl Into<DeviceId>,
target: impl Into<PathBuf>,
@ -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");
}
}

View file

@ -154,7 +154,6 @@ impl<T: AsRef<OsStr>> From<T> 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())