Skip to main content

sva_cli/
composition.rs

1// Concern: which directory a subcommand reads its composition from | Non-concern: loading it (sva-core), where a reading is written (destination.rs) | IO: (--in, or none) -> a directory or a refusal
2
3use std::io::ErrorKind;
4use std::path::PathBuf;
5
6use sva_core::{CliError, cwd};
7
8/// `--in` where the caller named a directory, the process's own where it did not. Either way
9/// the path is read here, so a caller is told which directory answered nothing rather than
10/// meeting the graph's own refusal for a tree that was never there.
11pub 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}