use clap::{Parser, Subcommand, ValueEnum};
use std::path::PathBuf;
#[derive(Parser)]
#[command(
name = "drft",
version,
about = "Structural integrity checker for linked file systems"
)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
#[arg(short = 'C', global = true)]
pub directory: Option<PathBuf>,
#[arg(long, global = true, default_value = "text")]
pub format: OutputFormat,
#[arg(long, global = true, default_value = "auto")]
pub color: ColorChoice,
}
#[derive(Subcommand)]
pub enum Commands {
Init,
Lock {
paths: Vec<String>,
},
Graph {
#[arg(long)]
raw: bool,
},
Impact {
#[arg(required = true)]
paths: Vec<String>,
#[arg(long, default_value = "1")]
depth: Depth,
#[arg(long, default_value = "inbound")]
direction: Direction,
},
Check,
}
#[derive(Clone, Copy, ValueEnum)]
pub enum OutputFormat {
Text,
Json,
}
#[derive(Clone, Copy, ValueEnum)]
pub enum Direction {
Inbound,
Outbound,
Both,
}
#[derive(Clone, Copy, ValueEnum)]
pub enum ColorChoice {
Auto,
Always,
Never,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Depth {
All,
Hops(usize),
}
impl Depth {
pub fn max_hops(self) -> Option<usize> {
match self {
Depth::All => None,
Depth::Hops(n) => Some(n),
}
}
}
impl std::str::FromStr for Depth {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.eq_ignore_ascii_case("all") {
return Ok(Depth::All);
}
match s.parse::<usize>() {
Ok(0) => Err(
"depth 0 would traverse nothing; use `--depth all` for the full reachable set"
.into(),
),
Ok(n) => Ok(Depth::Hops(n)),
Err(_) => Err(format!(
"invalid depth `{s}` (expected a positive number or `all`)"
)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
#[test]
fn depth_parses_hops_and_all() {
assert_eq!(Depth::from_str("1").unwrap(), Depth::Hops(1));
assert_eq!(Depth::from_str("3").unwrap(), Depth::Hops(3));
assert_eq!(Depth::from_str("all").unwrap(), Depth::All);
assert_eq!(Depth::from_str("ALL").unwrap(), Depth::All);
}
#[test]
fn depth_zero_is_rejected_with_a_pointer_to_all() {
let err = Depth::from_str("0").unwrap_err();
assert!(err.contains("--depth all"), "got: {err}");
}
#[test]
fn depth_rejects_nonsense() {
assert!(Depth::from_str("banana").is_err());
assert!(Depth::from_str("-1").is_err());
}
#[test]
fn max_hops_maps_all_to_unbounded() {
assert_eq!(Depth::All.max_hops(), None);
assert_eq!(Depth::Hops(2).max_hops(), Some(2));
}
}