aboutsummaryrefslogtreecommitdiffstats
path: root/src/cli.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-11-22 15:20:18 -0600
committerToby Vincent <tobyv13@gmail.com>2022-11-22 15:20:18 -0600
commit8c4e03340a39a966a06bdce0c948b5462774d020 (patch)
tree219cd883e757ddef446f921b0cf298c48a6df24f /src/cli.rs
parent72e9765d58b87125bdd5a2664bbc58202bdedff7 (diff)
refactor: improve cli and config parsing
Diffstat (limited to 'src/cli.rs')
-rw-r--r--src/cli.rs46
1 files changed, 29 insertions, 17 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 827aa54..a30bb7f 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -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()
+ }
+}