feat: merge configuration records by id
This commit is contained in:
parent
8b2a02309a
commit
18ec49ceb4
345
src/config.rs
345
src/config.rs
|
|
@ -5,7 +5,7 @@ use std::{fmt, path::PathBuf, str::FromStr};
|
|||
use directories::ProjectDirs;
|
||||
use figment::{
|
||||
Figment,
|
||||
providers::{Format, Serialized, Toml},
|
||||
providers::{Format, Toml},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
|
@ -154,21 +154,19 @@ impl ConfigLoader {
|
|||
|
||||
/// Load embedded, user, named, explicit, and command-line layers in order.
|
||||
pub fn load(&self) -> Result<Config, ConfigError> {
|
||||
let mut figment = embedded_figment();
|
||||
figment = merge_optional_file(figment, &self.paths.base);
|
||||
let mut config = Config::embedded();
|
||||
apply_optional_file(&mut config, &self.paths.base)?;
|
||||
if let Some(path) = &self.paths.named {
|
||||
figment = merge_optional_file(figment, path);
|
||||
apply_optional_file(&mut config, path)?;
|
||||
}
|
||||
if let Some(path) = &self.paths.explicit {
|
||||
if !path.exists() {
|
||||
return Err(ConfigError::ExplicitFileMissing { path: path.clone() });
|
||||
}
|
||||
figment = figment.merge(Toml::file(path));
|
||||
apply_file(&mut config, path)?;
|
||||
}
|
||||
figment
|
||||
.merge(Serialized::defaults(&self.overrides))
|
||||
.extract()
|
||||
.map_err(|error| ConfigError::Extract(Box::new(error)))
|
||||
apply_cli_overrides(&mut config, &self.overrides);
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -187,20 +185,304 @@ pub enum ConfigError {
|
|||
/// Figment could not read, parse, merge, or deserialize a source.
|
||||
#[error("failed to load configuration")]
|
||||
Extract(#[source] Box<figment::Error>),
|
||||
/// A user configuration source could not be read or deserialized.
|
||||
#[error("failed to load configuration from `{path}`")]
|
||||
Layer {
|
||||
/// File containing the invalid layer.
|
||||
path: PathBuf,
|
||||
/// Structured provider/deserialization failure.
|
||||
#[source]
|
||||
source: Box<figment::Error>,
|
||||
},
|
||||
/// A new ID was introduced without all fields needed to construct it.
|
||||
#[error("incomplete new {kind} `{id}` in `{path}`: missing `{field}`")]
|
||||
IncompleteEntry {
|
||||
/// Kind of configuration record.
|
||||
kind: &'static str,
|
||||
/// Stable ID being introduced.
|
||||
id: String,
|
||||
/// Required field that was omitted.
|
||||
field: &'static str,
|
||||
/// Layer containing the incomplete record.
|
||||
path: PathBuf,
|
||||
},
|
||||
/// One layer declared the same stable ID more than once.
|
||||
#[error("duplicate {kind} ID `{id}` in `{path}`")]
|
||||
DuplicateLayerId {
|
||||
/// Kind of configuration record.
|
||||
kind: &'static str,
|
||||
/// Repeated stable ID.
|
||||
id: String,
|
||||
/// Layer containing the duplicates.
|
||||
path: PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
fn embedded_figment() -> Figment {
|
||||
Figment::from(Toml::string(EMBEDDED_CONFIG)).merge(Toml::string(EMBEDDED_HEURISTICS))
|
||||
}
|
||||
|
||||
fn merge_optional_file(figment: Figment, path: &std::path::Path) -> Figment {
|
||||
fn apply_optional_file(config: &mut Config, path: &std::path::Path) -> Result<(), ConfigError> {
|
||||
if path.exists() {
|
||||
figment.merge(Toml::file(path))
|
||||
apply_file(config, path)
|
||||
} else {
|
||||
figment
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_file(config: &mut Config, path: &std::path::Path) -> Result<(), ConfigError> {
|
||||
let layer = Figment::from(Toml::file(path))
|
||||
.extract::<ConfigLayer>()
|
||||
.map_err(|source| ConfigError::Layer {
|
||||
path: path.to_owned(),
|
||||
source: Box::new(source),
|
||||
})?;
|
||||
apply_layer(config, layer, path)
|
||||
}
|
||||
|
||||
fn apply_cli_overrides(config: &mut Config, overrides: &ConfigOverrides) {
|
||||
if let Some(value) = overrides.input.max_document_bytes {
|
||||
config.input.max_document_bytes = value;
|
||||
}
|
||||
if let Some(value) = overrides.thresholds.low {
|
||||
config.thresholds.low = value;
|
||||
}
|
||||
if let Some(value) = overrides.thresholds.medium {
|
||||
config.thresholds.medium = value;
|
||||
}
|
||||
if let Some(value) = overrides.thresholds.high {
|
||||
config.thresholds.high = value;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
struct ConfigLayer {
|
||||
schema_version: Option<u32>,
|
||||
input: InputLayer,
|
||||
thresholds: ThresholdLayer,
|
||||
replacements: Vec<ReplacementPatch>,
|
||||
rules: Vec<RulePatch>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
struct InputLayer {
|
||||
max_document_bytes: Option<u64>,
|
||||
max_archive_bytes: Option<u64>,
|
||||
max_expanded_bytes: Option<u64>,
|
||||
max_archive_entries: Option<usize>,
|
||||
max_compression_ratio: Option<u64>,
|
||||
max_walk_depth: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
struct ThresholdLayer {
|
||||
low: Option<u8>,
|
||||
medium: Option<u8>,
|
||||
high: Option<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct ReplacementPatch {
|
||||
id: String,
|
||||
source: Option<char>,
|
||||
replacement: Option<String>,
|
||||
rationale: Option<String>,
|
||||
enabled: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct RulePatch {
|
||||
id: String,
|
||||
category: Option<Category>,
|
||||
matcher: Option<MatcherType>,
|
||||
pattern: Option<String>,
|
||||
weight: Option<f64>,
|
||||
message: Option<String>,
|
||||
help: Option<String>,
|
||||
occurrence_cap: Option<usize>,
|
||||
enabled: Option<bool>,
|
||||
scope: Option<RuleScope>,
|
||||
}
|
||||
|
||||
fn apply_layer(
|
||||
config: &mut Config,
|
||||
layer: ConfigLayer,
|
||||
path: &std::path::Path,
|
||||
) -> Result<(), ConfigError> {
|
||||
if let Some(value) = layer.schema_version {
|
||||
config.schema_version = value;
|
||||
}
|
||||
apply_input_layer(&mut config.input, layer.input);
|
||||
apply_threshold_layer(&mut config.thresholds, layer.thresholds);
|
||||
merge_replacements(&mut config.replacements, layer.replacements, path)?;
|
||||
merge_rules(&mut config.rules, layer.rules, path)
|
||||
}
|
||||
|
||||
fn apply_input_layer(target: &mut InputLimits, layer: InputLayer) {
|
||||
if let Some(value) = layer.max_document_bytes {
|
||||
target.max_document_bytes = value;
|
||||
}
|
||||
if let Some(value) = layer.max_archive_bytes {
|
||||
target.max_archive_bytes = value;
|
||||
}
|
||||
if let Some(value) = layer.max_expanded_bytes {
|
||||
target.max_expanded_bytes = value;
|
||||
}
|
||||
if let Some(value) = layer.max_archive_entries {
|
||||
target.max_archive_entries = value;
|
||||
}
|
||||
if let Some(value) = layer.max_compression_ratio {
|
||||
target.max_compression_ratio = value;
|
||||
}
|
||||
if let Some(value) = layer.max_walk_depth {
|
||||
target.max_walk_depth = value;
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_threshold_layer(target: &mut ConfidenceThresholds, layer: ThresholdLayer) {
|
||||
if let Some(value) = layer.low {
|
||||
target.low = value;
|
||||
}
|
||||
if let Some(value) = layer.medium {
|
||||
target.medium = value;
|
||||
}
|
||||
if let Some(value) = layer.high {
|
||||
target.high = value;
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_replacements(
|
||||
target: &mut Vec<ReplacementRule>,
|
||||
patches: Vec<ReplacementPatch>,
|
||||
path: &std::path::Path,
|
||||
) -> Result<(), ConfigError> {
|
||||
reject_duplicate_patch_ids("replacement", patches.iter().map(|patch| &patch.id), path)?;
|
||||
for patch in patches {
|
||||
if let Some(rule) = target.iter_mut().find(|rule| rule.id == patch.id) {
|
||||
if let Some(value) = patch.source {
|
||||
rule.source = value;
|
||||
}
|
||||
if let Some(value) = patch.replacement {
|
||||
rule.replacement = value;
|
||||
}
|
||||
if let Some(value) = patch.rationale {
|
||||
rule.rationale = value;
|
||||
}
|
||||
if let Some(value) = patch.enabled {
|
||||
rule.enabled = value;
|
||||
}
|
||||
} else {
|
||||
let id = patch.id;
|
||||
target.push(ReplacementRule {
|
||||
source: required(patch.source, "replacement", &id, "source", path)?,
|
||||
replacement: required(patch.replacement, "replacement", &id, "replacement", path)?,
|
||||
rationale: required(patch.rationale, "replacement", &id, "rationale", path)?,
|
||||
enabled: required(patch.enabled, "replacement", &id, "enabled", path)?,
|
||||
id,
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn merge_rules(
|
||||
target: &mut Vec<HeuristicRule>,
|
||||
patches: Vec<RulePatch>,
|
||||
path: &std::path::Path,
|
||||
) -> Result<(), ConfigError> {
|
||||
reject_duplicate_patch_ids("rule", patches.iter().map(|patch| &patch.id), path)?;
|
||||
for patch in patches {
|
||||
if let Some(rule) = target.iter_mut().find(|rule| rule.id == patch.id) {
|
||||
if let Some(value) = patch.category {
|
||||
rule.category = value;
|
||||
}
|
||||
if let Some(value) = patch.matcher {
|
||||
rule.matcher = value;
|
||||
}
|
||||
if let Some(value) = patch.pattern {
|
||||
rule.pattern = value;
|
||||
}
|
||||
if let Some(value) = patch.weight {
|
||||
rule.weight = value;
|
||||
}
|
||||
if let Some(value) = patch.message {
|
||||
rule.message = value;
|
||||
}
|
||||
if let Some(value) = patch.help {
|
||||
rule.help = value;
|
||||
}
|
||||
if let Some(value) = patch.occurrence_cap {
|
||||
rule.occurrence_cap = value;
|
||||
}
|
||||
if let Some(value) = patch.enabled {
|
||||
rule.enabled = value;
|
||||
}
|
||||
if let Some(value) = patch.scope {
|
||||
rule.scope = value;
|
||||
}
|
||||
} else {
|
||||
let id = patch.id;
|
||||
target.push(HeuristicRule {
|
||||
category: required(patch.category, "rule", &id, "category", path)?,
|
||||
matcher: required(patch.matcher, "rule", &id, "matcher", path)?,
|
||||
pattern: required(patch.pattern, "rule", &id, "pattern", path)?,
|
||||
weight: required(patch.weight, "rule", &id, "weight", path)?,
|
||||
message: required(patch.message, "rule", &id, "message", path)?,
|
||||
help: required(patch.help, "rule", &id, "help", path)?,
|
||||
occurrence_cap: required(
|
||||
patch.occurrence_cap,
|
||||
"rule",
|
||||
&id,
|
||||
"occurrence_cap",
|
||||
path,
|
||||
)?,
|
||||
enabled: required(patch.enabled, "rule", &id, "enabled", path)?,
|
||||
scope: required(patch.scope, "rule", &id, "scope", path)?,
|
||||
id,
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reject_duplicate_patch_ids<'a>(
|
||||
kind: &'static str,
|
||||
ids: impl Iterator<Item = &'a String>,
|
||||
path: &std::path::Path,
|
||||
) -> Result<(), ConfigError> {
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
for id in ids {
|
||||
if !seen.insert(id) {
|
||||
return Err(ConfigError::DuplicateLayerId {
|
||||
kind,
|
||||
id: id.clone(),
|
||||
path: path.to_owned(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn required<T>(
|
||||
value: Option<T>,
|
||||
kind: &'static str,
|
||||
id: &str,
|
||||
field: &'static str,
|
||||
path: &std::path::Path,
|
||||
) -> Result<T, ConfigError> {
|
||||
value.ok_or_else(|| ConfigError::IncompleteEntry {
|
||||
kind,
|
||||
id: id.to_owned(),
|
||||
field,
|
||||
path: path.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
impl ConfigPaths {
|
||||
/// Resolve paths using the operating system's application-directory APIs.
|
||||
pub fn discover(name: Option<&ConfigName>, explicit: Option<PathBuf>) -> Option<Self> {
|
||||
|
|
@ -526,4 +808,43 @@ mod tests {
|
|||
let error = ConfigLoader::from_paths(paths).load().unwrap_err();
|
||||
assert!(matches!(error, ConfigError::ExplicitFileMissing { path } if path == missing));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replacement_and_rule_arrays_merge_by_stable_id() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
std::fs::write(
|
||||
directory.path().join("config.toml"),
|
||||
r#"
|
||||
[[replacements]]
|
||||
id = "em-dash"
|
||||
enabled = false
|
||||
|
||||
[[rules]]
|
||||
id = "templated-conclusion"
|
||||
weight = 9.5
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
let paths = ConfigPaths::in_directory(directory.path().to_owned(), None, None);
|
||||
|
||||
let config = ConfigLoader::from_paths(paths).load().unwrap();
|
||||
|
||||
assert_eq!(config.replacements.len(), 10);
|
||||
let replacement = config
|
||||
.replacements
|
||||
.iter()
|
||||
.find(|rule| rule.id == "em-dash")
|
||||
.unwrap();
|
||||
assert!(!replacement.enabled);
|
||||
assert_eq!(replacement.source, '—');
|
||||
assert_eq!(replacement.replacement, "-");
|
||||
assert_eq!(config.rules.len(), 6);
|
||||
let heuristic = config
|
||||
.rules
|
||||
.iter()
|
||||
.find(|rule| rule.id == "templated-conclusion")
|
||||
.unwrap();
|
||||
assert_eq!(heuristic.weight, 9.5);
|
||||
assert_eq!(heuristic.matcher, MatcherType::Regex);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
2
tasks.md
2
tasks.md
|
|
@ -90,7 +90,7 @@ entry do not discard successful documents.
|
|||
user `<config_name>.toml` < explicit `--config` < explicit CLI flags.
|
||||
- [x] Validate config names and refuse traversal, extensions, empty names, and
|
||||
non-ASCII characters. Resolve the user path with platform APIs.
|
||||
- [ ] Implement merge-by-ID for rule/replacement tables rather than allowing
|
||||
- [x] Implement merge-by-ID for rule/replacement tables rather than allowing
|
||||
positional array replacement to produce surprising partial configuration.
|
||||
- [ ] Validate the fully resolved model: schema version, unique IDs/sources,
|
||||
ASCII-only replacement targets, regex compilation, finite non-negative weights,
|
||||
|
|
|
|||
Loading…
Reference in New Issue