From a98b50667bc6a11e4b8be464969adc14601e9e78 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Thu, 27 Apr 2023 17:59:33 -0500 Subject: feat: rewrite cli and add --all flag --- src/config.rs | 116 +++++++++++++++++++--------------------------------------- 1 file changed, 37 insertions(+), 79 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index cc1f28f..358a258 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,91 +1,49 @@ -use serde::{Deserialize, Serialize}; -use std::{convert::Infallible, path::PathBuf, str::FromStr}; +use clap::{Args, Parser}; +use tracing::{metadata::LevelFilter, Level}; -#[serde_with::serde_as] -#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)] -pub struct Config { - #[serde_as(as = "Vec>")] - pub paths: Vec, -} +use crate::search::Search; -#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)] -#[serde(default)] -pub struct SearchEntryConfig { - pub path_buf: PathBuf, - pub hidden: bool, - pub max_depth: Option, - pub pattern: Option, - #[cfg(feature = "git")] - pub git: bool, -} +/// Tool for listing project directories. +#[derive(Debug, Clone, Default, Parser)] +#[command(author, version, about)] +pub struct Config { + #[command(flatten)] + pub search: Search, -impl SearchEntryConfig { - pub fn new(path_buf: PathBuf) -> Self { - Self { - path_buf, - ..Default::default() - } - } + #[command(flatten)] + pub verbosity: Verbosity, } -impl From for SearchEntryConfig { - fn from(path_buf: PathBuf) -> Self { - Self::new(path_buf) - } +#[derive(Debug, Default, Clone, Args)] +pub struct Verbosity { + /// Print additional information per occurrence. + /// + /// Conflicts with `--quiet`. + #[arg(short, long, global = true, action = clap::ArgAction::Count, conflicts_with = "quiet")] + pub verbose: u8, + + /// Suppress all output. + /// + /// Conflicts with `--verbose`. + #[arg(short, long, global = true, conflicts_with = "verbose")] + pub quiet: bool, } -impl FromStr for SearchEntryConfig { - type Err = Infallible; - - fn from_str(s: &str) -> Result { - s.parse().map(|path_buf| Self { - path_buf, - ..Default::default() - }) +impl From<&Verbosity> for Option { + fn from(value: &Verbosity) -> Self { + match 1 + value.verbose - u8::from(value.quiet) { + 0 => None, + 1 => Some(Level::ERROR), + 2 => Some(Level::WARN), + 3 => Some(Level::INFO), + 4 => Some(Level::DEBUG), + _ => Some(Level::TRACE), + } } } -#[cfg(test)] -mod tests { - use super::*; - use pretty_assertions::assert_eq; - - #[test] - fn test_extract_config() { - let s = r#" - paths = [ - "/path/to/projects", - { path_buf = "/path/to/other_projects", hidden = true, max_depth = 1 }, - { path_buf = "/path/to/another_project", max_depth = 0 } - ] - "#; - - let config: Config = toml::from_str(s).unwrap(); - - assert_eq!( - config, - Config { - paths: Vec::from([ - SearchEntryConfig { - path_buf: "/path/to/projects".into(), - hidden: false, - max_depth: None, - ..Default::default() - }, - SearchEntryConfig { - path_buf: "/path/to/other_projects".into(), - hidden: true, - max_depth: Some(1), - ..Default::default() - }, - SearchEntryConfig { - path_buf: "/path/to/another_project".into(), - hidden: false, - max_depth: Some(0), - ..Default::default() - }, - ]), - } - ); +impl From<&Verbosity> for LevelFilter { + fn from(value: &Verbosity) -> Self { + Option::::from(value).into() } } -- cgit v1.2.3-70-g09d2