mirror of
https://github.com/crate-ci/typos.git
synced 2024-12-22 23:52:12 -05:00
refactor(config): Allow defaulting type patterns
This commit is contained in:
parent
acf5fd331b
commit
7ce0438a70
2 changed files with 76 additions and 40 deletions
|
@ -7,7 +7,7 @@ pub struct Config {
|
||||||
pub files: Walk,
|
pub files: Walk,
|
||||||
pub default: EngineConfig,
|
pub default: EngineConfig,
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
pub type_: std::collections::HashMap<kstring::KString, TypeEngineConfig>,
|
pub type_: TypeEngineConfig,
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub overrides: EngineConfig,
|
pub overrides: EngineConfig,
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ impl Config {
|
||||||
Self {
|
Self {
|
||||||
files: Walk::from_defaults(),
|
files: Walk::from_defaults(),
|
||||||
default: EngineConfig::from_defaults(),
|
default: EngineConfig::from_defaults(),
|
||||||
type_: Default::default(),
|
type_: TypeEngineConfig::from_defaults(),
|
||||||
overrides: EngineConfig::default(),
|
overrides: EngineConfig::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,12 +47,7 @@ impl Config {
|
||||||
pub fn update(&mut self, source: &Config) {
|
pub fn update(&mut self, source: &Config) {
|
||||||
self.files.update(&source.files);
|
self.files.update(&source.files);
|
||||||
self.default.update(&source.default);
|
self.default.update(&source.default);
|
||||||
for (type_name, engine) in source.type_.iter() {
|
self.type_.update(&source.type_);
|
||||||
self.type_
|
|
||||||
.entry(type_name.to_owned())
|
|
||||||
.or_insert_with(TypeEngineConfig::default)
|
|
||||||
.update(engine);
|
|
||||||
}
|
|
||||||
self.overrides.update(&source.overrides);
|
self.overrides.update(&source.overrides);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,15 +143,44 @@ impl Walk {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||||
#[serde(deny_unknown_fields, default)]
|
#[serde(deny_unknown_fields, default)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(transparent)]
|
||||||
pub struct TypeEngineConfig {
|
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>,
|
pub extend_glob: Vec<kstring::KString>,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub engine: EngineConfig,
|
pub engine: EngineConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TypeEngineConfig {
|
impl GlobEngineConfig {
|
||||||
pub fn update(&mut self, source: &TypeEngineConfig) {
|
pub fn update(&mut self, source: &GlobEngineConfig) {
|
||||||
self.extend_glob.extend(source.extend_glob.iter().cloned());
|
self.extend_glob.extend(source.extend_glob.iter().cloned());
|
||||||
self.engine.update(&source.engine);
|
self.engine.update(&source.engine);
|
||||||
}
|
}
|
||||||
|
@ -449,8 +473,8 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_extend_glob_updates() {
|
fn test_extend_glob_updates() {
|
||||||
let null = TypeEngineConfig::default();
|
let null = GlobEngineConfig::default();
|
||||||
let extended = TypeEngineConfig {
|
let extended = GlobEngineConfig {
|
||||||
extend_glob: vec!["*.foo".into()],
|
extend_glob: vec!["*.foo".into()],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
@ -463,11 +487,11 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_extend_glob_extends() {
|
fn test_extend_glob_extends() {
|
||||||
let base = TypeEngineConfig {
|
let base = GlobEngineConfig {
|
||||||
extend_glob: vec!["*.foo".into()],
|
extend_glob: vec!["*.foo".into()],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let extended = TypeEngineConfig {
|
let extended = GlobEngineConfig {
|
||||||
extend_glob: vec!["*.bar".into()],
|
extend_glob: vec!["*.bar".into()],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
|
@ -139,21 +139,21 @@ impl<'s> ConfigEngine<'s> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut types = Default::default();
|
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
|
let mut types = types
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(type_, type_engine)| {
|
.map(|(type_, type_engine)| {
|
||||||
let mut new_engine = config.default.clone();
|
let mut new_engine = config.default.clone();
|
||||||
new_engine.update(&type_engine.engine);
|
new_engine.update(&type_engine.engine);
|
||||||
new_engine.update(&config.overrides);
|
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,
|
extend_glob: type_engine.extend_glob,
|
||||||
engine: new_engine,
|
engine: new_engine,
|
||||||
};
|
};
|
||||||
(type_, new_type_engine)
|
(type_, new_type_engine)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
std::mem::swap(&mut types, &mut config.type_);
|
std::mem::swap(&mut types, &mut config.type_.patterns);
|
||||||
|
|
||||||
config.default.update(&config.overrides);
|
config.default.update(&config.overrides);
|
||||||
|
|
||||||
|
@ -179,7 +179,7 @@ impl<'s> ConfigEngine<'s> {
|
||||||
let mut type_matcher = ignore::types::TypesBuilder::new();
|
let mut type_matcher = ignore::types::TypesBuilder::new();
|
||||||
type_matcher.add_defaults();
|
type_matcher.add_defaults();
|
||||||
let mut types: std::collections::HashMap<_, _> = Default::default();
|
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_engine.extend_glob.is_empty() {
|
||||||
if type_matcher
|
if type_matcher
|
||||||
.definitions()
|
.definitions()
|
||||||
|
@ -371,14 +371,16 @@ mod test {
|
||||||
check_filename: Some(true),
|
check_filename: Some(true),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
type_: maplit::hashmap! {
|
type_: crate::config::TypeEngineConfig {
|
||||||
type_name.clone() => crate::config::TypeEngineConfig {
|
patterns: maplit::hashmap! {
|
||||||
engine: crate::config::EngineConfig {
|
type_name.clone() => crate::config::GlobEngineConfig {
|
||||||
check_filename: Some(false),
|
engine: crate::config::EngineConfig {
|
||||||
check_file: Some(true),
|
check_filename: Some(false),
|
||||||
|
check_file: Some(true),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
..Default::default()
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
overrides: crate::config::EngineConfig {
|
overrides: crate::config::EngineConfig {
|
||||||
|
@ -395,13 +397,18 @@ mod test {
|
||||||
assert_eq!(loaded.default.binary, Some(false));
|
assert_eq!(loaded.default.binary, Some(false));
|
||||||
assert_eq!(loaded.default.check_filename, Some(true));
|
assert_eq!(loaded.default.check_filename, Some(true));
|
||||||
assert_eq!(loaded.default.check_file, Some(false));
|
assert_eq!(loaded.default.check_file, Some(false));
|
||||||
assert_eq!(loaded.type_[type_name.as_str()].engine.binary, Some(false));
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
loaded.type_[type_name.as_str()].engine.check_filename,
|
loaded.type_.patterns[type_name.as_str()].engine.binary,
|
||||||
Some(false)
|
Some(false)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
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)
|
Some(false)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -415,9 +422,11 @@ mod test {
|
||||||
let type_name = kstring::KString::from_static(NEVER_EXIST_TYPE);
|
let type_name = kstring::KString::from_static(NEVER_EXIST_TYPE);
|
||||||
|
|
||||||
let config = crate::config::Config {
|
let config = crate::config::Config {
|
||||||
type_: maplit::hashmap! {
|
type_: crate::config::TypeEngineConfig {
|
||||||
type_name => crate::config::TypeEngineConfig {
|
patterns: maplit::hashmap! {
|
||||||
..Default::default()
|
type_name => crate::config::GlobEngineConfig {
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
@ -457,12 +466,14 @@ mod test {
|
||||||
binary: Some(true),
|
binary: Some(true),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
type_: maplit::hashmap! {
|
type_: crate::config::TypeEngineConfig {
|
||||||
type_name.clone() => crate::config::TypeEngineConfig {
|
patterns: maplit::hashmap! {
|
||||||
extend_glob: vec![type_name],
|
type_name.clone() => crate::config::GlobEngineConfig {
|
||||||
engine: crate::config::EngineConfig {
|
extend_glob: vec![type_name],
|
||||||
binary: Some(false),
|
engine: crate::config::EngineConfig {
|
||||||
..Default::default()
|
binary: Some(false),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -489,14 +500,15 @@ mod test {
|
||||||
binary: Some(true),
|
binary: Some(true),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
type_: maplit::hashmap! {
|
type_: crate::config::TypeEngineConfig {
|
||||||
type_name.clone() => crate::config::TypeEngineConfig {
|
patterns: maplit::hashmap! {
|
||||||
|
type_name.clone() => crate::config::GlobEngineConfig {
|
||||||
extend_glob: vec![type_name],
|
extend_glob: vec![type_name],
|
||||||
engine: crate::config::EngineConfig {
|
engine: crate::config::EngineConfig {
|
||||||
binary: Some(false),
|
binary: Some(false),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
},
|
}},
|
||||||
},
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue