summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs116
1 files changed, 37 insertions, 79 deletions
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<serde_with::PickFirst<(_, serde_with::DisplayFromStr)>>")]
- pub paths: Vec<SearchEntryConfig>,
-}
+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<usize>,
- pub pattern: Option<String>,
- #[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<PathBuf> 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<Self, Self::Err> {
- s.parse().map(|path_buf| Self {
- path_buf,
- ..Default::default()
- })
+impl From<&Verbosity> for Option<Level> {
+ 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::<Level>::from(value).into()
}
}