diff options
Diffstat (limited to 'src/cli.rs')
-rw-r--r-- | src/cli.rs | 46 |
1 files changed, 29 insertions, 17 deletions
@@ -1,38 +1,44 @@ use crate::{Config, SearchPath}; use clap::{Args, Parser}; use figment::{value, Metadata, Profile, Provider}; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use std::path::PathBuf; -use tracing::Level; +use tracing::{metadata::LevelFilter, Level}; /// Tool for listing project directories. -#[derive(Debug, Clone, Default, Parser, Serialize)] +#[derive(Debug, Clone, Default, Parser, Serialize, Deserialize)] #[command(author, version, about)] -#[serde(into = "Config")] pub struct Cli { + #[command(flatten)] + pub projects: Projects, + + #[command(flatten)] + pub verbosity: Verbosity, +} + +#[derive(Debug, Default, Clone, Args, Serialize, Deserialize)] +#[serde(into = "Config")] +pub struct Projects { /// Directory to search. /// /// Directories are searched recursively based on `--max-depth`. - pub(crate) paths: Vec<PathBuf>, + paths: Vec<PathBuf>, /// Max depth to recurse. /// /// By default, no limit is set. Setting to 0 will only use the supplied directory. - #[arg(short = 'd', long)] - pub(crate) max_depth: Option<usize>, + #[arg(short = 'd', long, default_value = "1")] + max_depth: Option<usize>, /// Recurse into hidden directories. /// /// Traverse into hidden directories while searching. A directory is considered hidden /// if its name starts with a `.` sign (dot). If `--max-depth` is 0, this has no effect. #[arg(long, default_value_t)] - pub(crate) hidden: bool, - - #[command(flatten)] - pub verbose: Verbosity, + hidden: bool, } -impl Provider for Cli { +impl Provider for Projects { fn metadata(&self) -> Metadata { Metadata::named("Projectr cli provider") } @@ -42,8 +48,8 @@ impl Provider for Cli { } } -impl From<Cli> for Config { - fn from(value: Cli) -> Self { +impl From<Projects> for Config { + fn from(value: Projects) -> Self { let paths = value .paths .iter() @@ -51,7 +57,7 @@ impl From<Cli> for Config { .map(|p| SearchPath { path: p, hidden: value.hidden, - recurse: value.max_depth, + max_depth: value.max_depth, }) .collect(); @@ -59,7 +65,7 @@ impl From<Cli> for Config { } } -#[derive(Debug, Default, Clone, Args)] +#[derive(Debug, Default, Clone, Args, Serialize, Deserialize)] pub struct Verbosity { /// Print additional information per occurrence. /// @@ -76,7 +82,7 @@ pub struct Verbosity { impl From<Verbosity> for Option<Level> { fn from(value: Verbosity) -> Self { - match value.verbose + 1 - u8::from(value.quiet) { + match 1 + value.verbose - u8::from(value.quiet) { 0 => None, 1 => Some(Level::ERROR), 2 => Some(Level::WARN), @@ -86,3 +92,9 @@ impl From<Verbosity> for Option<Level> { } } } + +impl From<Verbosity> for LevelFilter { + fn from(value: Verbosity) -> Self { + Option::<Level>::from(value).into() + } +} |