From 7ce0438a709c5131d95cc5c0e87f0de085652442 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 4 Aug 2021 06:55:46 -0500 Subject: [PATCH] refactor(config): Allow defaulting type patterns --- src/config.rs | 54 +++++++++++++++++++++++++++++++------------- src/policy.rs | 62 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 76 insertions(+), 40 deletions(-) diff --git a/src/config.rs b/src/config.rs index 2290093..74c6309 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,7 @@ pub struct Config { pub files: Walk, pub default: EngineConfig, #[serde(rename = "type")] - pub type_: std::collections::HashMap, + pub type_: TypeEngineConfig, #[serde(skip)] pub overrides: EngineConfig, } @@ -39,7 +39,7 @@ impl Config { Self { files: Walk::from_defaults(), default: EngineConfig::from_defaults(), - type_: Default::default(), + type_: TypeEngineConfig::from_defaults(), overrides: EngineConfig::default(), } } @@ -47,12 +47,7 @@ impl Config { pub fn update(&mut self, source: &Config) { self.files.update(&source.files); self.default.update(&source.default); - for (type_name, engine) in source.type_.iter() { - self.type_ - .entry(type_name.to_owned()) - .or_insert_with(TypeEngineConfig::default) - .update(engine); - } + self.type_.update(&source.type_); self.overrides.update(&source.overrides); } } @@ -148,15 +143,44 @@ impl Walk { #[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(deny_unknown_fields, default)] -#[serde(rename_all = "kebab-case")] +#[serde(transparent)] pub struct TypeEngineConfig { + pub patterns: std::collections::HashMap, +} + +impl TypeEngineConfig { + pub fn from_defaults() -> Self { + let empty = Self::default(); + Self { + patterns: empty.patterns().collect(), + } + } + + pub fn update(&mut self, source: &Self) { + for (type_name, engine) in source.patterns.iter() { + self.patterns + .entry(type_name.to_owned()) + .or_insert_with(GlobEngineConfig::default) + .update(engine); + } + } + + pub fn patterns(&self) -> impl Iterator { + self.patterns.clone().into_iter() + } +} + +#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(deny_unknown_fields, default)] +#[serde(rename_all = "kebab-case")] +pub struct GlobEngineConfig { pub extend_glob: Vec, #[serde(flatten)] pub engine: EngineConfig, } -impl TypeEngineConfig { - pub fn update(&mut self, source: &TypeEngineConfig) { +impl GlobEngineConfig { + pub fn update(&mut self, source: &GlobEngineConfig) { self.extend_glob.extend(source.extend_glob.iter().cloned()); self.engine.update(&source.engine); } @@ -449,8 +473,8 @@ mod test { #[test] fn test_extend_glob_updates() { - let null = TypeEngineConfig::default(); - let extended = TypeEngineConfig { + let null = GlobEngineConfig::default(); + let extended = GlobEngineConfig { extend_glob: vec!["*.foo".into()], ..Default::default() }; @@ -463,11 +487,11 @@ mod test { #[test] fn test_extend_glob_extends() { - let base = TypeEngineConfig { + let base = GlobEngineConfig { extend_glob: vec!["*.foo".into()], ..Default::default() }; - let extended = TypeEngineConfig { + let extended = GlobEngineConfig { extend_glob: vec!["*.bar".into()], ..Default::default() }; diff --git a/src/policy.rs b/src/policy.rs index ede2fa6..5c85e83 100644 --- a/src/policy.rs +++ b/src/policy.rs @@ -139,21 +139,21 @@ impl<'s> ConfigEngine<'s> { } let mut types = Default::default(); - std::mem::swap(&mut types, &mut config.type_); + std::mem::swap(&mut types, &mut config.type_.patterns); let mut types = types .into_iter() .map(|(type_, type_engine)| { let mut new_engine = config.default.clone(); new_engine.update(&type_engine.engine); new_engine.update(&config.overrides); - let new_type_engine = crate::config::TypeEngineConfig { + let new_type_engine = crate::config::GlobEngineConfig { extend_glob: type_engine.extend_glob, engine: new_engine, }; (type_, new_type_engine) }) .collect(); - std::mem::swap(&mut types, &mut config.type_); + std::mem::swap(&mut types, &mut config.type_.patterns); config.default.update(&config.overrides); @@ -179,7 +179,7 @@ impl<'s> ConfigEngine<'s> { let mut type_matcher = ignore::types::TypesBuilder::new(); type_matcher.add_defaults(); let mut types: std::collections::HashMap<_, _> = Default::default(); - for (type_name, type_engine) in type_.into_iter() { + for (type_name, type_engine) in type_.patterns() { if type_engine.extend_glob.is_empty() { if type_matcher .definitions() @@ -371,14 +371,16 @@ mod test { check_filename: Some(true), ..Default::default() }, - type_: maplit::hashmap! { - type_name.clone() => crate::config::TypeEngineConfig { - engine: crate::config::EngineConfig { - check_filename: Some(false), - check_file: Some(true), + type_: crate::config::TypeEngineConfig { + patterns: maplit::hashmap! { + type_name.clone() => crate::config::GlobEngineConfig { + engine: crate::config::EngineConfig { + check_filename: Some(false), + check_file: Some(true), + ..Default::default() + }, ..Default::default() }, - ..Default::default() }, }, overrides: crate::config::EngineConfig { @@ -395,13 +397,18 @@ mod test { assert_eq!(loaded.default.binary, Some(false)); assert_eq!(loaded.default.check_filename, Some(true)); assert_eq!(loaded.default.check_file, Some(false)); - assert_eq!(loaded.type_[type_name.as_str()].engine.binary, Some(false)); assert_eq!( - loaded.type_[type_name.as_str()].engine.check_filename, + loaded.type_.patterns[type_name.as_str()].engine.binary, Some(false) ); assert_eq!( - loaded.type_[type_name.as_str()].engine.check_file, + loaded.type_.patterns[type_name.as_str()] + .engine + .check_filename, + Some(false) + ); + assert_eq!( + loaded.type_.patterns[type_name.as_str()].engine.check_file, Some(false) ); } @@ -415,9 +422,11 @@ mod test { let type_name = kstring::KString::from_static(NEVER_EXIST_TYPE); let config = crate::config::Config { - type_: maplit::hashmap! { - type_name => crate::config::TypeEngineConfig { - ..Default::default() + type_: crate::config::TypeEngineConfig { + patterns: maplit::hashmap! { + type_name => crate::config::GlobEngineConfig { + ..Default::default() + }, }, }, ..Default::default() @@ -457,12 +466,14 @@ mod test { binary: Some(true), ..Default::default() }, - type_: maplit::hashmap! { - type_name.clone() => crate::config::TypeEngineConfig { - extend_glob: vec![type_name], - engine: crate::config::EngineConfig { - binary: Some(false), - ..Default::default() + type_: crate::config::TypeEngineConfig { + patterns: maplit::hashmap! { + type_name.clone() => crate::config::GlobEngineConfig { + extend_glob: vec![type_name], + engine: crate::config::EngineConfig { + binary: Some(false), + ..Default::default() + }, }, }, }, @@ -489,14 +500,15 @@ mod test { binary: Some(true), ..Default::default() }, - type_: maplit::hashmap! { - type_name.clone() => crate::config::TypeEngineConfig { + type_: crate::config::TypeEngineConfig { + patterns: maplit::hashmap! { + type_name.clone() => crate::config::GlobEngineConfig { extend_glob: vec![type_name], engine: crate::config::EngineConfig { binary: Some(false), ..Default::default() }, - }, + }}, }, ..Default::default() };