From fa956cc1598f69c81d6db54b4e2e8f5f35cd9b23 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Wed, 30 Nov 2022 02:01:47 -0600 Subject: fix: fix config serialization and reorganize project --- src/cli.rs | 4 +-- src/config.rs | 56 ++++++++++++++++++++++++++----- src/search.rs | 28 ++++++++-------- src/search/entry.rs | 17 ++++------ src/search/entry/config.rs | 84 ---------------------------------------------- 5 files changed, 70 insertions(+), 119 deletions(-) delete mode 100644 src/search/entry/config.rs (limited to 'src') diff --git a/src/cli.rs b/src/cli.rs index a5cd2dd..b1af9f6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use std::path::PathBuf; use tracing::{metadata::LevelFilter, Level}; -use crate::{search, Config}; +use crate::{config::Entry, Config}; /// Tool for listing project directories. #[derive(Debug, Clone, Default, Parser, Serialize, Deserialize)] @@ -64,7 +64,7 @@ impl From for Config { .paths .iter() .cloned() - .map(|path_buf| search::entry::Config { + .map(|path_buf| Entry { path_buf, hidden: value.hidden, max_depth: value.max_depth, diff --git a/src/config.rs b/src/config.rs index 126b939..e73b7e1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,12 @@ use figment::{providers::Serialized, value, Figment, Metadata, Profile, Provider}; use serde::{Deserialize, Serialize}; +use std::{convert::Infallible, path::PathBuf, str::FromStr}; -use crate::search; - +#[serde_with::serde_as] #[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)] pub struct Config { - pub(crate) paths: Vec, + #[serde_as(as = "Vec>")] + pub(crate) paths: Vec, } impl Config { @@ -30,6 +31,43 @@ impl Provider for Config { } } +#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)] +#[serde(default)] +pub struct Entry { + pub path_buf: PathBuf, + pub hidden: bool, + pub max_depth: Option, + pub pattern: Option, + #[cfg(feature = "git")] + pub git: bool, +} + +impl Entry { + pub fn new(path_buf: PathBuf) -> Self { + Self { + path_buf, + ..Default::default() + } + } +} + +impl From for Entry { + fn from(path_buf: PathBuf) -> Self { + Self::new(path_buf) + } +} + +impl FromStr for Entry { + type Err = Infallible; + + fn from_str(s: &str) -> Result { + s.parse().map(|path_buf| Self { + path_buf, + ..Default::default() + }) + } +} + #[cfg(test)] mod tests { use super::*; @@ -37,15 +75,15 @@ mod tests { use pretty_assertions::assert_eq; #[test] - fn test_extract() { + fn test_extract_config() { figment::Jail::expect_with(|jail| { jail.create_file( "file.toml", r#" paths = [ "/path/to/projects", - { path = "/path/to/other_projects", recurse = 1, hidden = true }, - { path = "/path/to/another_project", recurse = 0 }, + { path_buf = "/path/to/other_projects", hidden = true, max_depth = 1 }, + { path_buf = "/path/to/another_project", max_depth = 0 } ] "#, )?; @@ -58,19 +96,19 @@ mod tests { config, Config { paths: Vec::from([ - search::entry::Config { + Entry { path_buf: "/path/to/projects".into(), hidden: false, max_depth: None, ..Default::default() }, - search::entry::Config { + Entry { path_buf: "/path/to/other_projects".into(), hidden: true, max_depth: Some(1), ..Default::default() }, - search::entry::Config { + Entry { path_buf: "/path/to/another_project".into(), hidden: false, max_depth: Some(0), diff --git a/src/search.rs b/src/search.rs index b2db0e3..6249da4 100644 --- a/src/search.rs +++ b/src/search.rs @@ -1,14 +1,14 @@ use figment::Provider; use std::vec::IntoIter; -use self::entry::Entry; -use crate::{project::ProjectItem, Config, Result}; +use self::entry::EntryIter; +use crate::{config::Entry, project::ProjectItem, Config, Result}; pub mod entry; pub struct Search { - iter: IntoIter, - curr: Option, + iter: IntoIter, + curr: Option, } impl std::fmt::Debug for Search { @@ -32,9 +32,15 @@ impl Search { } } +impl From for Search { + fn from(config: Config) -> Self { + config.paths.into() + } +} + impl From for Search where - T: IntoIterator>, + T: IntoIterator>, { fn from(value: T) -> Self { Self { @@ -44,12 +50,6 @@ where } } -impl From for Search { - fn from(value: Config) -> Self { - value.paths.into() - } -} - impl Iterator for Search { type Item = ProjectItem; @@ -86,17 +86,17 @@ mod tests { let project4 = temp_dir.join("subdir/project4"); let paths = Search::from(Vec::from([ - entry::Config { + Entry { path_buf: project_dir.to_owned(), max_depth: Some(1), ..Default::default() }, - entry::Config { + Entry { path_buf: project3.to_owned(), max_depth: Some(0), ..Default::default() }, - entry::Config { + Entry { path_buf: project4.to_owned(), max_depth: Some(0), ..Default::default() diff --git a/src/search/entry.rs b/src/search/entry.rs index 6cd601c..9e58962 100644 --- a/src/search/entry.rs +++ b/src/search/entry.rs @@ -2,21 +2,18 @@ use ignore::{Walk, WalkBuilder}; use tracing::error; use crate::{ + config::Entry, project::{path::PathMatcher, ProjectParser, ProjectParserGroup}, search::ProjectItem, }; -pub use config::Config; - -mod config; - -pub struct Entry { +pub struct EntryIter { parsers: ProjectParserGroup, iter: Walk, } -impl Entry { - fn new(config: &Config) -> Self { +impl EntryIter { + fn new(config: &Entry) -> Self { let iter = WalkBuilder::new(&config.path_buf) .standard_filters(true) .max_depth(config.max_depth) @@ -38,13 +35,13 @@ impl Entry { } } -impl From for Entry { - fn from(config: Config) -> Self { +impl From for EntryIter { + fn from(config: Entry) -> Self { Self::new(&config) } } -impl Iterator for Entry { +impl Iterator for EntryIter { type Item = ProjectItem; fn next(&mut self) -> Option { diff --git a/src/search/entry/config.rs b/src/search/entry/config.rs deleted file mode 100644 index 4372356..0000000 --- a/src/search/entry/config.rs +++ /dev/null @@ -1,84 +0,0 @@ -use serde::{Deserialize, Deserializer, Serialize}; -use std::{convert::Infallible, path::PathBuf, str::FromStr}; - -#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize)] -#[serde(default)] -pub struct Config { - pub path_buf: PathBuf, - pub hidden: bool, - pub max_depth: Option, - pub pattern: Option, - - #[cfg(feature = "git")] - pub git: bool, -} - -impl From for Config { - fn from(path_buf: PathBuf) -> Self { - Self { - path_buf, - ..Default::default() - } - } -} - -impl Config { - pub fn new(path_buf: PathBuf) -> Self { - Self { - path_buf, - ..Default::default() - } - } -} - -impl FromStr for Config { - type Err = Infallible; - - fn from_str(s: &str) -> Result { - s.parse().map(PathBuf::into) - } -} - -// Custom deserialize impl to accept either string or struct -impl<'de> Deserialize<'de> for Config { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - #[derive(Deserialize)] - #[serde(untagged)] - enum Variants { - String(String), - Struct { - path_buf: PathBuf, - hidden: bool, - max_depth: Option, - pattern: Option, - - #[cfg(feature = "git")] - git: bool, - }, - } - - match Variants::deserialize(deserializer)? { - Variants::String(s) => s.parse().map_err(serde::de::Error::custom), - Variants::Struct { - path_buf, - hidden, - max_depth, - pattern, - - #[cfg(feature = "git")] - git, - } => Ok(Self { - path_buf, - hidden, - max_depth, - pattern, - - #[cfg(feature = "git")] - git, - }), - } - } -} -- cgit v1.2.3-70-g09d2