diff options
author | Toby Vincent <tobyv13@gmail.com> | 2022-11-30 02:01:47 -0600 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2022-11-30 02:01:47 -0600 |
commit | fa956cc1598f69c81d6db54b4e2e8f5f35cd9b23 (patch) | |
tree | afb753d7dd999efb864af6dde3fb28633d86a41c /src/config.rs | |
parent | b32d10b65ee7bdd939915f2a34929386ac4595ea (diff) |
fix: fix config serialization and reorganize project
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 56 |
1 files changed, 47 insertions, 9 deletions
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<search::entry::Config>, + #[serde_as(as = "Vec<serde_with::PickFirst<(_, serde_with::DisplayFromStr)>>")] + pub(crate) paths: Vec<Entry>, } 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<usize>, + pub pattern: Option<String>, + #[cfg(feature = "git")] + pub git: bool, +} + +impl Entry { + pub fn new(path_buf: PathBuf) -> Self { + Self { + path_buf, + ..Default::default() + } + } +} + +impl From<PathBuf> 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<Self, Self::Err> { + 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), |