use std::path::{Path, PathBuf};
use crate::error::WorkspaceError;
pub(super) fn file_basename(path: &Path) -> String {
path.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| path.to_string_lossy().into_owned())
}
pub(super) fn resolve_root(root: &Path) -> Result<PathBuf, WorkspaceError> {
if root.is_file() {
return Ok(root.to_path_buf());
}
if root.is_dir() {
let candidate = root.join("apimock.toml");
if candidate.is_file() {
return Ok(candidate);
}
return Err(WorkspaceError::InvalidRoot {
path: root.to_path_buf(),
reason: "directory does not contain apimock.toml".to_owned(),
});
}
Err(WorkspaceError::InvalidRoot {
path: root.to_path_buf(),
reason: "path does not exist".to_owned(),
})
}