use std::io::ErrorKind;
use std::path::PathBuf;
use sva_core::{CliError, cwd};
pub fn composition(named: Option<&str>) -> Result<PathBuf, CliError> {
let dir = match named {
Some(path) => PathBuf::from(path),
None => cwd()?,
};
match std::fs::read_dir(&dir) {
Ok(_) => Ok(dir),
Err(e) if e.kind() == ErrorKind::NotFound => Err(CliError::NotFound(format!(
"no composition at `{}`",
dir.display()
))),
Err(e) if e.kind() == ErrorKind::NotADirectory => Err(CliError::Usage(format!(
"`{}` is a file; a composition is the directory its nodes sit in",
dir.display()
))),
Err(e) => Err(CliError::Io(format!(
"could not read the composition at `{}`: {e}",
dir.display()
))),
}
}