use std::path::{Path, PathBuf};
pub(crate) enum RootError {
Unreadable(std::io::Error),
NotADirectory,
}
impl RootError {
pub(crate) fn reason(self) -> Box<str> {
match self {
Self::Unreadable(source) => source.to_string().into_boxed_str(),
Self::NotADirectory => Box::from("the project root is not a directory"),
}
}
}
pub(crate) fn path_text(path: &Path) -> Box<str> {
path.display().to_string().into_boxed_str()
}
pub(crate) fn canonical_root(root: &Path) -> Result<PathBuf, RootError> {
let canonical = std::fs::canonicalize(root).map_err(RootError::Unreadable)?;
match canonical.is_dir() {
true => Ok(canonical),
false => Err(RootError::NotADirectory),
}
}