use std::fmt;
use std::path::Path;
use super::super::fs::STAGING_CLAIM;
use super::super::printable::Printable;
#[derive(Debug)]
pub(in crate::cli) enum LeftChanged {
Unrestored { path: String, err: String },
Stranded {
path: String,
staging: String,
err: String,
},
Created { path: String },
Unchecked { path: String, err: String },
StagingLeak { path: String },
LinkLost {
path: String,
target: Option<String>,
},
MarkerKept {
path: String,
marked: Result<(), String>,
},
}
impl fmt::Display for LeftChanged {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unrestored { path, err } => {
write!(
f,
"{} (restore failed: {})",
Printable(path),
Printable(err)
)
}
Self::Stranded { path, staging, err } => write!(
f,
"{} (moved to {}, and could not be moved back: {})",
Printable(path),
Printable(staging),
Printable(err)
),
Self::Created { path } => {
write!(f, "{} (created and could not be removed)", Printable(path))
}
Self::Unchecked { path, err } => write!(
f,
"{} (could not check whether this run created it: {}; remove it only if you \
know it did)",
Printable(path),
Printable(err)
),
Self::StagingLeak { path } => write!(f, "{} ({STAGING_CLAIM})", Printable(path)),
Self::LinkLost { path, target } => write!(
f,
"{} ({} before this run: it holds its original text again, as a regular file, \
and the link was not put back)",
Printable(path),
Printable(&describe_link(path, target.as_deref()))
),
Self::MarkerKept {
path,
marked: Ok(()),
} => write!(
f,
"{} (kept: `oakum version` refuses while it is here; remove it once the other \
files listed are put right)",
Printable(path)
),
Self::MarkerKept {
path,
marked: Err(err),
} => write!(
f,
"{} (kept, but it could not be marked as rolled back ({}), so the next `oakum \
version` will misread it: keep none of this run's writes, remove no bump file, \
put the other files listed right, then remove it)",
Printable(path),
Printable(err)
),
}
}
}
pub(super) fn describe_link(path: &str, target: Option<&str>) -> String {
let Some(target) = target else {
return String::from("a link");
};
match Path::new(path).parent() {
Some(parent) if !parent.as_os_str().is_empty() && !Path::new(target).has_root() => format!(
"a link whose text was `{target}`, relative to `{}/`,",
parent.to_string_lossy()
),
_ => format!("a link whose text was `{target}`"),
}
}
#[derive(Debug)]
pub(in crate::cli) enum WriteSetFailure {
Refused { cause: String },
RolledBack {
cause: String,
left_changed: Vec<LeftChanged>,
unswept: Vec<String>,
},
}
impl WriteSetFailure {
pub(super) fn refused(cause: String) -> Self {
Self::Refused { cause }
}
pub(super) fn rolled_back(
cause: String,
mut left_changed: Vec<LeftChanged>,
mut unswept: Vec<String>,
) -> Self {
left_changed.sort_by_key(ToString::to_string);
unswept.sort();
Self::RolledBack {
cause,
left_changed,
unswept,
}
}
pub(in crate::cli) fn stranded(&self) -> Vec<(&str, &str)> {
let Self::RolledBack { left_changed, .. } = self else {
return Vec::new();
};
left_changed
.iter()
.filter_map(|entry| match entry {
LeftChanged::Stranded { path, staging, .. } => {
Some((staging.as_str(), path.as_str()))
}
_ => None,
})
.collect()
}
}
pub(in crate::cli) const HEAD_FILES: usize = 5;
impl fmt::Display for WriteSetFailure {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (cause, left_changed, unswept) = match self {
Self::Refused { cause } => return write!(f, "{}", Printable(cause)),
Self::RolledBack {
cause,
left_changed,
unswept,
} => (cause, left_changed, unswept),
};
write!(f, "{}", Printable(cause))?;
if !left_changed.is_empty() {
write!(f, "\n{} file(s) left changed:", left_changed.len())?;
for entry in left_changed.iter().take(HEAD_FILES) {
write!(f, "\n {entry}")?;
}
match left_changed.len().saturating_sub(HEAD_FILES) {
0 => {}
1 => write!(f, "\n … 1 more")?,
rest => write!(f, "\n … {rest} more")?,
}
}
if !unswept.is_empty() {
write!(
f,
"\n{} director{} could not be checked for staging files:",
unswept.len(),
if unswept.len() == 1 { "y" } else { "ies" }
)?;
for entry in unswept {
write!(f, "\n {}", Printable(entry))?;
}
}
Ok(())
}
}
impl std::error::Error for WriteSetFailure {}