use serde::{Deserialize, Serialize}; use std::{convert::Infallible, path::PathBuf, str::FromStr}; #[serde_with::serde_as] #[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)] pub struct Config { #[serde_as(as = "Vec>")] pub paths: Vec, } #[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, } impl SearchEntryConfig { pub fn new(path_buf: PathBuf) -> Self { Self { path_buf, ..Default::default() } } } impl From for SearchEntryConfig { fn from(path_buf: PathBuf) -> Self { Self::new(path_buf) } } impl FromStr for SearchEntryConfig { 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::*; 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() }, ]), } ); } }