use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum PathRefusal {
Leaves { relative: String },
NotAPath { relative: String },
Unplaced { relative: String, document: String },
}
impl fmt::Display for PathRefusal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Leaves { relative } => write!(f, "`{relative}` leaves its root"),
Self::NotAPath { relative } => {
write!(
f,
"`{relative}` isn't a file's path: a name has `\\`, `:` or NUL"
)
}
Self::Unplaced { relative, document } => write!(
f,
"`{relative}` can't be resolved: the document's path, `{document}`, isn't a `/`-separated path in its root"
),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn each_refusal_says_why() {
let relative = "../x.csv".to_string();
assert_eq!(
PathRefusal::Leaves {
relative: relative.clone()
}
.to_string(),
"`../x.csv` leaves its root"
);
assert!(
PathRefusal::NotAPath {
relative: "a\\b".into()
}
.to_string()
.contains("isn't a file's path")
);
assert!(
PathRefusal::Unplaced {
relative,
document: "C:\\a.comp.md".into()
}
.to_string()
.contains("the document's path, `C:\\a.comp.md`,")
);
}
}