From 18ec49ceb4dcf6f66ff69ba3eade7502bd3050a0 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 29 Sep 2026 13:23:02 +0200 Subject: [PATCH] feat: merge configuration records by id --- src/config.rs | 345 ++++++++++++++++++++++++++++++++++++++++++++++++-- tasks.md | 2 +- 2 files changed, 334 insertions(+), 13 deletions(-) diff --git a/src/config.rs b/src/config.rs index 2feef0b..d9590a3 100644 --- a/src/config.rs +++ b/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 { - 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), + /// 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, + }, + /// 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::() + .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, + input: InputLayer, + thresholds: ThresholdLayer, + replacements: Vec, + rules: Vec, +} + +#[derive(Debug, Default, Deserialize)] +#[serde(default, deny_unknown_fields)] +struct InputLayer { + max_document_bytes: Option, + max_archive_bytes: Option, + max_expanded_bytes: Option, + max_archive_entries: Option, + max_compression_ratio: Option, + max_walk_depth: Option, +} + +#[derive(Debug, Default, Deserialize)] +#[serde(default, deny_unknown_fields)] +struct ThresholdLayer { + low: Option, + medium: Option, + high: Option, +} + +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct ReplacementPatch { + id: String, + source: Option, + replacement: Option, + rationale: Option, + enabled: Option, +} + +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct RulePatch { + id: String, + category: Option, + matcher: Option, + pattern: Option, + weight: Option, + message: Option, + help: Option, + occurrence_cap: Option, + enabled: Option, + scope: Option, +} + +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, + patches: Vec, + 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, + patches: Vec, + 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, + 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( + value: Option, + kind: &'static str, + id: &str, + field: &'static str, + path: &std::path::Path, +) -> Result { + 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) -> Option { @@ -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); + } } diff --git a/tasks.md b/tasks.md index 11c79a7..711e756 100644 --- a/tasks.md +++ b/tasks.md @@ -90,7 +90,7 @@ entry do not discard successful documents. user `.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,