1use std::io::ErrorKind;
4use std::path::PathBuf;
5
6use sva_core::{CliError, cwd};
7
8pub fn composition(named: Option<&str>) -> Result<PathBuf, CliError> {
12 let dir = match named {
13 Some(path) => PathBuf::from(path),
14 None => cwd()?,
15 };
16 match std::fs::read_dir(&dir) {
17 Ok(_) => Ok(dir),
18 Err(e) if e.kind() == ErrorKind::NotFound => Err(CliError::NotFound(format!(
19 "no composition at `{}`",
20 dir.display()
21 ))),
22 Err(e) if e.kind() == ErrorKind::NotADirectory => Err(CliError::Usage(format!(
23 "`{}` is a file; a composition is the directory its nodes sit in",
24 dir.display()
25 ))),
26 Err(e) => Err(CliError::Io(format!(
27 "could not read the composition at `{}`: {e}",
28 dir.display()
29 ))),
30 }
31}