refactor(config): Allow defaulting type patterns

This commit is contained in:
Ed Page 2021-08-04 06:55:46 -05:00
parent acf5fd331b
commit 7ce0438a70
2 changed files with 76 additions and 40 deletions

View file

@ -7,7 +7,7 @@ pub struct Config {
pub files: Walk,
pub default: EngineConfig,
#[serde(rename = "type")]
pub type_: std::collections::HashMap<kstring::KString, TypeEngineConfig>,
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<kstring::KString, GlobEngineConfig>,
}
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<Item = (kstring::KString, GlobEngineConfig)> {
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<kstring::KString>,
#[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()
};

View file

@ -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,8 +371,9 @@ mod test {
check_filename: 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 {
engine: crate::config::EngineConfig {
check_filename: Some(false),
check_file: Some(true),
@ -381,6 +382,7 @@ mod test {
..Default::default()
},
},
},
overrides: crate::config::EngineConfig {
binary: Some(false),
check_file: Some(false),
@ -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,11 +422,13 @@ 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 {
type_: crate::config::TypeEngineConfig {
patterns: maplit::hashmap! {
type_name => crate::config::GlobEngineConfig {
..Default::default()
},
},
},
..Default::default()
};
engine.set_overrides(config);
@ -457,8 +466,9 @@ 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),
@ -466,6 +476,7 @@ mod test {
},
},
},
},
..Default::default()
};
engine.set_overrides(config);
@ -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()
};