use ignore::WalkBuilder; use serde::{Deserialize, Deserializer, Serialize}; use std::{convert::Infallible, path::PathBuf, str::FromStr}; use super::Entry; #[derive(Debug, PartialEq, Eq, Clone, Default, Serialize)] #[serde(default)] pub struct Config { pub path_buf: PathBuf, pub hidden: bool, pub max_depth: Option, pub git: bool, pub pattern: Option, } impl From for Entry { fn from(config: Config) -> Self { let iter = WalkBuilder::new(&config.path_buf) .standard_filters(true) .max_depth(config.max_depth) .hidden(!config.hidden) .build(); Self { iter, config } } } 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, git: bool, pattern: Option, }, } match Variants::deserialize(deserializer)? { Variants::String(s) => s.parse().map_err(serde::de::Error::custom), Variants::Struct { path_buf, hidden, max_depth, git, pattern, } => Ok(Self { path_buf, hidden, max_depth, git, pattern, }), } } }