//! Typed configuration consumed by the shared library. use std::{fmt, path::PathBuf, str::FromStr}; use directories::ProjectDirs; use figment::{ Figment, error::Kind, providers::{Format, Toml}, }; use miette::{Diagnostic, NamedSource, SourceSpan}; use serde::{Deserialize, Serialize}; use crate::{Category, FindingSeverity}; const EMBEDDED_CONFIG: &str = include_str!("../assets/config.toml"); const EMBEDDED_HEURISTICS: &str = include_str!("../assets/heuristics.toml"); /// A validated named configuration without path syntax or a file extension. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ConfigName(String); impl ConfigName { /// Return the validated name as text. pub fn as_str(&self) -> &str { &self.0 } } impl fmt::Display for ConfigName { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(formatter) } } impl FromStr for ConfigName { type Err = ConfigNameError; fn from_str(value: &str) -> Result { if value.is_empty() || !value .bytes() .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-')) { return Err(ConfigNameError { value: value.to_owned(), }); } Ok(Self(value.to_owned())) } } /// A rejected named-configuration selector. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, Diagnostic)] #[diagnostic(code(john_slop::config::invalid_name))] #[error( "invalid config name `{value}`; use only ASCII letters, digits, `_`, and `-` without an extension" )] pub struct ConfigNameError { value: String, } /// Files considered by the layered configuration loader. #[derive(Debug, Clone, PartialEq, Eq)] pub struct ConfigPaths { /// Platform-specific John Slop configuration directory. pub directory: PathBuf, /// User-wide base configuration. pub base: PathBuf, /// Selected named user configuration, if any. pub named: Option, /// Explicit configuration supplied for this invocation, if any. pub explicit: Option, } /// Scalar configuration values supplied directly by a command-line adapter. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)] pub struct ConfigOverrides { /// Input-limit overrides. #[serde(skip_serializing_if = "InputOverrides::is_empty")] pub input: InputOverrides, /// Confidence-threshold overrides. #[serde(skip_serializing_if = "ThresholdOverrides::is_empty")] pub thresholds: ThresholdOverrides, } /// Command-line overrides for input resource limits. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)] pub struct InputOverrides { /// Override the maximum accepted bytes for one document. #[serde(skip_serializing_if = "Option::is_none")] pub max_document_bytes: Option, /// Override the maximum accepted bytes for an outer archive. #[serde(skip_serializing_if = "Option::is_none")] pub max_archive_bytes: Option, /// Override the maximum total expanded bytes for an archive. #[serde(skip_serializing_if = "Option::is_none")] pub max_expanded_bytes: Option, /// Override the maximum member count for an archive. #[serde(skip_serializing_if = "Option::is_none")] pub max_archive_entries: Option, /// Override the maximum expanded-to-compressed archive ratio. #[serde(skip_serializing_if = "Option::is_none")] pub max_compression_ratio: Option, /// Override the maximum recursive directory depth. #[serde(skip_serializing_if = "Option::is_none")] pub max_walk_depth: Option, } impl InputOverrides { fn is_empty(&self) -> bool { self.max_document_bytes.is_none() && self.max_archive_bytes.is_none() && self.max_expanded_bytes.is_none() && self.max_archive_entries.is_none() && self.max_compression_ratio.is_none() && self.max_walk_depth.is_none() } } /// Command-line overrides for confidence band boundaries. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)] pub struct ThresholdOverrides { /// Override the first low-confidence score. #[serde(skip_serializing_if = "Option::is_none")] pub low: Option, /// Override the first medium-confidence score. #[serde(skip_serializing_if = "Option::is_none")] pub medium: Option, /// Override the first high-confidence score. #[serde(skip_serializing_if = "Option::is_none")] pub high: Option, } impl ThresholdOverrides { fn is_empty(&self) -> bool { self.low.is_none() && self.medium.is_none() && self.high.is_none() } } /// Builder for the ordered configuration sources used by one invocation. #[derive(Debug, Clone)] pub struct ConfigLoader { paths: ConfigPaths, overrides: ConfigOverrides, } impl ConfigLoader { /// Discover the platform user directory and resolve optional named/explicit layers. pub fn discover( name: Option<&ConfigName>, explicit: Option, ) -> Result { let paths = ConfigPaths::discover(name, explicit).ok_or(ConfigError::NoConfigDirectory)?; Ok(Self::from_paths(paths)) } /// Construct a loader from pre-resolved paths, primarily for embedding and tests. pub fn from_paths(paths: ConfigPaths) -> Self { Self { paths, overrides: ConfigOverrides::default(), } } /// Add highest-precedence scalar overrides from the current invocation. pub fn with_overrides(mut self, overrides: ConfigOverrides) -> Self { self.overrides = overrides; self } /// Return every resolved path in precedence order. pub fn paths(&self) -> &ConfigPaths { &self.paths } /// Load embedded, user, named, explicit, and command-line layers in order. pub fn load(&self) -> Result { let mut config = Config::embedded(); apply_optional_file(&mut config, &self.paths.base)?; if let Some(path) = &self.paths.named { apply_optional_file(&mut config, path)?; } if let Some(path) = &self.paths.explicit { if !path.exists() { return Err(ConfigError::ExplicitFileMissing { path: path.clone() }); } apply_file(&mut config, path)?; } apply_cli_overrides(&mut config, &self.overrides); config.validate().map_err(ConfigError::Validation)?; Ok(config) } } /// Failure while resolving or extracting layered configuration. #[derive(Debug, thiserror::Error, Diagnostic)] pub enum ConfigError { /// The platform did not expose a user configuration directory. #[error("the operating system did not provide a user configuration directory")] #[diagnostic(code(john_slop::config::directory_unavailable))] NoConfigDirectory, /// A deliberately selected explicit file does not exist. #[error("explicit configuration file `{path}` does not exist")] #[diagnostic(code(john_slop::config::explicit_missing))] ExplicitFileMissing { /// Requested explicit path. path: PathBuf, }, /// Figment could not read, parse, merge, or deserialize a source. #[error("failed to load embedded configuration")] #[diagnostic(code(john_slop::config::embedded_invalid))] Extract(#[source] Box), /// A selected configuration file could not be read as UTF-8 text. #[error("failed to read configuration from `{path}`")] #[diagnostic(code(john_slop::config::read_failed))] Read { /// File that could not be read. path: PathBuf, /// Underlying filesystem/UTF-8 error. #[source] source: std::io::Error, }, /// A user configuration source could not be read or deserialized. #[error(transparent)] #[diagnostic(transparent)] Layer(Box), /// A new ID was introduced without all fields needed to construct it. #[error("incomplete new {kind} `{id}` in `{path}`: missing `{field}`")] #[diagnostic(code(john_slop::config::incomplete_entry))] 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}`")] #[diagnostic(code(john_slop::config::duplicate_layer_id))] DuplicateLayerId { /// Kind of configuration record. kind: &'static str, /// Repeated stable ID. id: String, /// Layer containing the duplicates. path: PathBuf, }, /// The fully resolved model violates a configuration invariant. #[error(transparent)] #[diagnostic(transparent)] Validation(ConfigValidationError), } /// A provider error retaining its source file, dotted key, and label span. #[derive(Debug, thiserror::Error, Diagnostic)] #[error("invalid configuration key `{key}` in `{path}`")] #[diagnostic( code(john_slop::config::invalid_value), help("Correct the highlighted TOML value or remove the unknown key.") )] pub struct ConfigLayerError { /// File containing the invalid layer. pub path: PathBuf, /// Dotted configuration key reported by the provider. pub key: String, /// Structured provider/deserialization failure. #[source] source: Box, /// Exact file text associated with the provider failure. #[source_code] source_code: NamedSource, /// Best-effort location of the reported key. #[label("invalid value for `{key}`")] span: SourceSpan, } /// A semantic invariant violated by the fully resolved configuration. #[derive(Debug, thiserror::Error, Diagnostic)] #[diagnostic(code(john_slop::config::validation))] pub enum ConfigValidationError { /// This build supports only schema version 1. #[error("unsupported schema_version {actual}; expected 1")] UnsupportedSchema { /// Version found after layering. actual: u32, }, /// A stable record ID is malformed. #[error("invalid {kind} ID `{id}`; expected lowercase kebab-case")] InvalidId { /// Kind of configuration record. kind: &'static str, /// Invalid stable ID. id: String, }, /// A stable ID occurs more than once in the resolved model. #[error("duplicate {kind} ID `{id}`")] DuplicateId { /// Kind of configuration record. kind: &'static str, /// Repeated ID. id: String, }, /// Two replacement rules target the same source text. #[error("replacement source `{text}` is configured more than once")] DuplicateReplacementSource { /// Conflicting source text. text: String, }, /// A replacement source is empty or contains only ASCII. #[error("replacement `{id}` must have a non-empty, non-ASCII source")] InvalidReplacementSource { /// Replacement rule ID. id: String, }, /// A replacement target is empty or contains non-ASCII text. #[error("replacement `{id}` must have a non-empty ASCII target")] InvalidReplacementTarget { /// Replacement rule ID. id: String, }, /// A user-facing explanatory field is empty. #[error("{kind} `{id}` has an empty `{field}`")] EmptyField { /// Kind of configuration record. kind: &'static str, /// Stable record ID. id: String, /// Empty field name. field: &'static str, }, /// A heuristic weight is negative or non-finite. #[error("rule `{id}` has invalid weight {weight}")] InvalidWeight { /// Heuristic rule ID. id: String, /// Invalid configured weight. weight: f64, }, /// A rule cannot contribute any occurrences. #[error("rule `{id}` must have an occurrence_cap greater than zero")] ZeroOccurrenceCap { /// Heuristic rule ID. id: String, }, /// A regular expression could not be compiled. #[error("rule `{id}` has an invalid regular expression")] InvalidRegex { /// Heuristic rule ID. id: String, /// Regex compiler failure. #[source] source: regex::Error, }, /// A matcher can succeed without consuming text. #[error("rule `{id}` can match empty text")] EmptyMatch { /// Heuristic rule ID. id: String, }, /// Replacement-backed matchers must use the catalog sentinel. #[error("replacement rule `{id}` must use pattern `configured-replacements`")] InvalidReplacementMatcher { /// Heuristic rule ID. id: String, }, /// A rule has no applicable content kinds. #[error("rule `{id}` must apply to at least one content kind")] EmptyScope { /// Heuristic rule ID. id: String, }, /// An extension includes path/dot/case syntax instead of a normalized value. #[error("rule `{id}` has invalid extension `{extension}`")] InvalidExtension { /// Heuristic rule ID. id: String, /// Invalid extension value. extension: String, }, /// A path applicability glob could not be compiled. #[error("rule `{id}` has invalid {field} glob `{pattern}`")] InvalidGlob { /// Heuristic rule ID. id: String, /// Scope field containing the invalid glob. field: &'static str, /// Invalid glob pattern. pattern: String, /// Glob compiler failure. #[source] source: globset::Error, }, /// Confidence thresholds are not strictly increasing. #[error("confidence thresholds must satisfy low < medium < high <= 100")] InvalidThresholds, /// An input resource limit is zero. #[error("input limit `{field}` must be greater than zero")] ZeroInputLimit { /// Zero-valued field. field: &'static str, }, } fn embedded_figment() -> Figment { Figment::from(Toml::string(EMBEDDED_CONFIG)).merge(Toml::string(EMBEDDED_HEURISTICS)) } fn apply_optional_file(config: &mut Config, path: &std::path::Path) -> Result<(), ConfigError> { if path.exists() { apply_file(config, path) } else { Ok(()) } } fn apply_file(config: &mut Config, path: &std::path::Path) -> Result<(), ConfigError> { let source_text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read { path: path.to_owned(), source, })?; let layer = Figment::from(Toml::string(&source_text)) .extract::() .map_err(|source| layer_error(path, source_text, source))?; apply_layer(config, layer, path) } fn layer_error(path: &std::path::Path, source_text: String, source: figment::Error) -> ConfigError { let key = if source.path.is_empty() { match &source.kind { Kind::UnknownField(field, _) => field.clone(), Kind::MissingField(field) => field.to_string(), _ => "".to_owned(), } } else { source.path.join(".") }; let needle = key.rsplit('.').next().unwrap_or(&key); let (offset, length) = source_text .find(needle) .map_or((0, source_text.len().min(1)), |offset| { (offset, needle.len()) }); ConfigError::Layer(Box::new(ConfigLayerError { path: path.to_owned(), key, source: Box::new(source), source_code: NamedSource::new(path.display().to_string(), source_text), span: (offset, length).into(), })) } 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.input.max_archive_bytes { config.input.max_archive_bytes = value; } if let Some(value) = overrides.input.max_expanded_bytes { config.input.max_expanded_bytes = value; } if let Some(value) = overrides.input.max_archive_entries { config.input.max_archive_entries = value; } if let Some(value) = overrides.input.max_compression_ratio { config.input.max_compression_ratio = value; } if let Some(value) = overrides.input.max_walk_depth { config.input.max_walk_depth = 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, severity: 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.severity { rule.severity = 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)?, severity: required(patch.severity, "rule", &id, "severity", 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 { let directory = ProjectDirs::from("com", "idle-org", "john_slop")? .config_dir() .to_owned(); Some(Self::in_directory(directory, name, explicit)) } /// Resolve paths under a known directory, primarily for deterministic tests. pub fn in_directory( directory: PathBuf, name: Option<&ConfigName>, explicit: Option, ) -> Self { let base = directory.join("config.toml"); let named = name.map(|name| directory.join(format!("{name}.toml"))); Self { directory, base, named, explicit, } } } /// One configured Unicode-to-ASCII replacement. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct ReplacementRule { /// Stable identifier used by diagnostics and overrides. pub id: String, /// Non-empty Unicode text to replace. pub source: String, /// ASCII replacement text. pub replacement: String, /// Why this replacement is safe or desirable. pub rationale: String, /// Whether matching and edit generation are active for this rule. pub enabled: bool, } /// Resolved John Slop configuration. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Config { /// Configuration schema understood by this build. pub schema_version: u32, /// Enabled replacement rules in deterministic matching order. pub replacements: Vec, /// Configured detection rules in deterministic declaration order. pub rules: Vec, /// Named confidence-band boundaries. pub thresholds: ConfidenceThresholds, /// Resource and traversal limits applied while reading inputs. pub input: InputLimits, } /// One configured data-driven detection rule. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct HeuristicRule { /// Stable lowercase kebab-case identifier. pub id: String, /// Broad evidence family. pub category: Category, /// Presentation severity for each finding produced by this rule. pub severity: FindingSeverity, /// Matching implementation selected for this rule. pub matcher: MatcherType, /// Exact phrase, regular expression, or named replacement source. pub pattern: String, /// Evidence contribution before aggregation and damping. pub weight: f64, /// Concise explanation shown with a finding. pub message: String, /// Remediation or review guidance. pub help: String, /// Maximum contributing occurrences per document. pub occurrence_cap: usize, /// Whether this rule participates in detection. pub enabled: bool, /// Content and extension filters for this rule. pub scope: RuleScope, } /// Matcher implementation named by a heuristic rule. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum MatcherType { /// Literal phrase matching. ExactPhrase, /// Rust regular-expression matching. Regex, /// Regex matching that activates only after multiple occurrences. RepeatedRegex, /// Findings produced by the configured replacement catalog. Replacement, } /// Applicability filters attached to a heuristic rule. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct RuleScope { /// Broad kinds of content this rule accepts. pub content: Vec, /// Lowercase extensions without a leading dot; empty means unrestricted. pub extensions: Vec, /// Path globs that opt inputs into this rule; empty means unrestricted. #[serde(default)] pub include: Vec, /// Path globs that opt inputs out of this rule. #[serde(default)] pub exclude: Vec, /// Ignore conventional generated-output paths. #[serde(default = "default_true")] pub skip_generated: bool, /// Ignore conventional vendored-dependency paths. #[serde(default = "default_true")] pub skip_vendor: bool, /// Ignore conventionally minified filenames. #[serde(default = "default_true")] pub skip_minified: bool, } const fn default_true() -> bool { true } /// Broad input kind used for heuristic applicability. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum ContentKind { /// Articles, documentation, and other prose. Prose, /// Programming-language source text. SourceCode, } /// Confidence boundaries used to name aggregate score bands. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct ConfidenceThresholds { /// First score considered low confidence. pub low: u8, /// First score considered medium confidence. pub medium: u8, /// First score considered high confidence. pub high: u8, } /// Bounded resource policy for filesystem and archive inputs. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct InputLimits { /// Maximum bytes accepted for a regular file or one archive member. pub max_document_bytes: u64, /// Maximum bytes accepted for an outer archive. pub max_archive_bytes: u64, /// Maximum total declared expanded bytes across archive members. pub max_expanded_bytes: u64, /// Maximum number of members in one archive. pub max_archive_entries: usize, /// Maximum allowed expanded-to-compressed ratio. pub max_compression_ratio: u64, /// Maximum directory traversal depth, including the root at depth zero. pub max_walk_depth: usize, } impl Default for InputLimits { fn default() -> Self { Self { max_document_bytes: 8 * 1024 * 1024, max_archive_bytes: 64 * 1024 * 1024, max_expanded_bytes: 256 * 1024 * 1024, max_archive_entries: 10_000, max_compression_ratio: 100, max_walk_depth: 64, } } } impl Config { /// Load the replacement defaults embedded in this build. pub fn embedded() -> Self { let config: Self = embedded_figment() .extract() .expect("embedded configuration must deserialize"); config .validate() .expect("embedded configuration must be semantically valid"); config } /// Check schema and semantic invariants after all layers have been applied. pub fn validate(&self) -> Result<(), ConfigValidationError> { if self.schema_version != 1 { return Err(ConfigValidationError::UnsupportedSchema { actual: self.schema_version, }); } validate_thresholds(self.thresholds)?; validate_input_limits(&self.input)?; validate_replacements(&self.replacements)?; validate_rules(&self.rules) } } fn validate_thresholds(thresholds: ConfidenceThresholds) -> Result<(), ConfigValidationError> { if !(thresholds.low < thresholds.medium && thresholds.medium < thresholds.high && thresholds.high <= 100) { return Err(ConfigValidationError::InvalidThresholds); } Ok(()) } fn validate_input_limits(input: &InputLimits) -> Result<(), ConfigValidationError> { for (field, value) in [ ("max_document_bytes", input.max_document_bytes), ("max_archive_bytes", input.max_archive_bytes), ("max_expanded_bytes", input.max_expanded_bytes), ("max_archive_entries", input.max_archive_entries as u64), ("max_compression_ratio", input.max_compression_ratio), ("max_walk_depth", input.max_walk_depth as u64), ] { if value == 0 { return Err(ConfigValidationError::ZeroInputLimit { field }); } } Ok(()) } fn validate_replacements(replacements: &[ReplacementRule]) -> Result<(), ConfigValidationError> { let mut ids = std::collections::BTreeSet::new(); let mut sources = std::collections::BTreeSet::new(); for rule in replacements { validate_id("replacement", &rule.id)?; if !ids.insert(&rule.id) { return Err(ConfigValidationError::DuplicateId { kind: "replacement", id: rule.id.clone(), }); } if rule.source.is_empty() || rule.source.is_ascii() { return Err(ConfigValidationError::InvalidReplacementSource { id: rule.id.clone(), }); } if !sources.insert(rule.source.as_str()) { return Err(ConfigValidationError::DuplicateReplacementSource { text: rule.source.clone(), }); } if rule.replacement.is_empty() || !rule.replacement.is_ascii() { return Err(ConfigValidationError::InvalidReplacementTarget { id: rule.id.clone(), }); } require_text("replacement", &rule.id, "rationale", &rule.rationale)?; } Ok(()) } fn validate_rules(rules: &[HeuristicRule]) -> Result<(), ConfigValidationError> { let mut ids = std::collections::BTreeSet::new(); for rule in rules { validate_id("rule", &rule.id)?; if !ids.insert(&rule.id) { return Err(ConfigValidationError::DuplicateId { kind: "rule", id: rule.id.clone(), }); } require_text("rule", &rule.id, "pattern", &rule.pattern)?; require_text("rule", &rule.id, "message", &rule.message)?; require_text("rule", &rule.id, "help", &rule.help)?; if !rule.weight.is_finite() || rule.weight < 0.0 { return Err(ConfigValidationError::InvalidWeight { id: rule.id.clone(), weight: rule.weight, }); } if rule.occurrence_cap == 0 { return Err(ConfigValidationError::ZeroOccurrenceCap { id: rule.id.clone(), }); } match rule.matcher { MatcherType::ExactPhrase => {} MatcherType::Regex | MatcherType::RepeatedRegex => { let regex = regex::Regex::new(&rule.pattern).map_err(|source| { ConfigValidationError::InvalidRegex { id: rule.id.clone(), source, } })?; if regex.is_match("") { return Err(ConfigValidationError::EmptyMatch { id: rule.id.clone(), }); } } MatcherType::Replacement if rule.pattern != "configured-replacements" => { return Err(ConfigValidationError::InvalidReplacementMatcher { id: rule.id.clone(), }); } MatcherType::Replacement => {} } if rule.scope.content.is_empty() { return Err(ConfigValidationError::EmptyScope { id: rule.id.clone(), }); } for extension in &rule.scope.extensions { if extension.is_empty() || !extension .bytes() .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit()) { return Err(ConfigValidationError::InvalidExtension { id: rule.id.clone(), extension: extension.clone(), }); } } for (field, patterns) in [ ("include", &rule.scope.include), ("exclude", &rule.scope.exclude), ] { for pattern in patterns { globset::Glob::new(pattern).map_err(|source| { ConfigValidationError::InvalidGlob { id: rule.id.clone(), field, pattern: pattern.clone(), source, } })?; } } } Ok(()) } fn validate_id(kind: &'static str, id: &str) -> Result<(), ConfigValidationError> { let valid = !id.is_empty() && !id.starts_with('-') && !id.ends_with('-') && !id.contains("--") && id .bytes() .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-'); if !valid { return Err(ConfigValidationError::InvalidId { kind, id: id.to_owned(), }); } Ok(()) } fn require_text( kind: &'static str, id: &str, field: &'static str, value: &str, ) -> Result<(), ConfigValidationError> { if value.trim().is_empty() { return Err(ConfigValidationError::EmptyField { kind, id: id.to_owned(), field, }); } Ok(()) } impl Default for Config { fn default() -> Self { Self::embedded() } } #[cfg(test)] mod tests { use std::collections::BTreeSet; use super::*; #[test] fn embedded_replacements_are_unique_and_ascii() { let config = Config::embedded(); let ids = config .replacements .iter() .map(|rule| rule.id.as_str()) .collect::>(); let sources = config .replacements .iter() .map(|rule| rule.source.as_str()) .collect::>(); assert_eq!(config.replacements.len(), 10); assert_eq!(config.schema_version, 1); assert_eq!(ids.len(), config.replacements.len()); assert_eq!(sources.len(), config.replacements.len()); assert!(config.replacements.iter().all(|rule| rule.enabled && !rule.source.is_ascii() && rule.replacement.is_ascii() && !rule.rationale.is_empty())); } #[test] fn default_input_limits_match_the_accepted_policy() { let limits = InputLimits::default(); assert_eq!(limits.max_document_bytes, 8 * 1024 * 1024); assert_eq!(limits.max_archive_bytes, 64 * 1024 * 1024); assert_eq!(limits.max_expanded_bytes, 256 * 1024 * 1024); assert_eq!(limits.max_archive_entries, 10_000); assert_eq!(limits.max_compression_ratio, 100); assert_eq!(limits.max_walk_depth, 64); } #[test] fn embedded_heuristics_have_complete_metadata() { let config = Config::embedded(); assert_eq!(config.rules.len(), 11); assert_eq!(config.thresholds.low, 20); assert_eq!(config.thresholds.medium, 50); assert_eq!(config.thresholds.high, 75); assert!(config.rules.iter().all(|rule| { !rule.id.is_empty() && !rule.pattern.is_empty() && !rule.message.is_empty() && !rule.help.is_empty() && rule.occurrence_cap > 0 && !rule.scope.content.is_empty() })); } #[test] fn config_names_accept_only_safe_ascii_components() { for accepted in ["work", "review_2", "strict-mode", "A1"] { assert_eq!(accepted.parse::().unwrap().as_str(), accepted); } for rejected in [ "", ".", "../work", "work.toml", "a/b", "équipe", "has space", ] { assert!( rejected.parse::().is_err(), "accepted {rejected:?}" ); } } #[test] fn config_paths_keep_named_files_inside_the_config_directory() { let directory = PathBuf::from("/configuration-root/john_slop"); let name = "strict-mode".parse::().unwrap(); let paths = ConfigPaths::in_directory( directory.clone(), Some(&name), Some(PathBuf::from("project.toml")), ); assert_eq!(paths.base, directory.join("config.toml")); assert_eq!(paths.named, Some(directory.join("strict-mode.toml"))); assert_eq!(paths.explicit, Some(PathBuf::from("project.toml"))); } #[test] fn layered_scalar_precedence_is_deterministic() { let directory = tempfile::tempdir().unwrap(); let name = "strict".parse::().unwrap(); let explicit = directory.path().join("project.toml"); std::fs::write( directory.path().join("config.toml"), "[input]\nmax_document_bytes = 100\n[thresholds]\nlow = 21\n", ) .unwrap(); std::fs::write( directory.path().join("strict.toml"), "[input]\nmax_document_bytes = 200\n[thresholds]\nlow = 22\n", ) .unwrap(); std::fs::write( &explicit, "[input]\nmax_document_bytes = 300\n[thresholds]\nlow = 23\n", ) .unwrap(); let paths = ConfigPaths::in_directory(directory.path().to_owned(), Some(&name), Some(explicit)); let loader = ConfigLoader::from_paths(paths).with_overrides(ConfigOverrides { input: InputOverrides { max_document_bytes: Some(400), ..InputOverrides::default() }, thresholds: ThresholdOverrides { low: Some(24), ..ThresholdOverrides::default() }, }); let config = loader.load().unwrap(); assert_eq!(config.input.max_document_bytes, 400); assert_eq!(config.thresholds.low, 24); assert_eq!(config.thresholds.medium, 50); } #[test] fn absent_optional_files_are_ignored_but_missing_explicit_file_is_fatal() { let directory = tempfile::tempdir().unwrap(); let paths = ConfigPaths::in_directory(directory.path().to_owned(), None, None); assert_eq!( ConfigLoader::from_paths(paths).load().unwrap(), Config::embedded() ); let missing = directory.path().join("missing.toml"); let paths = ConfigPaths::in_directory(directory.path().to_owned(), None, Some(missing.clone())); 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 [[replacements]] id = "right-arrow" source = "→" replacement = "->" rationale = "Use an ASCII arrow in plain text." enabled = true [[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(), 11); 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.replacements.last().unwrap().id, "right-arrow"); assert_eq!(config.rules.len(), 11); let heuristic = config .rules .iter() .find(|rule| rule.id == "templated-conclusion") .unwrap(); assert_eq!(heuristic.weight, 9.5); assert_eq!(heuristic.matcher, MatcherType::Regex); } #[test] fn resolved_configuration_rejects_invalid_semantics() { let mut config = Config::embedded(); config.thresholds.medium = config.thresholds.low; assert!(matches!( config.validate(), Err(ConfigValidationError::InvalidThresholds) )); let mut config = Config::embedded(); config.replacements[1].source = config.replacements[0].source.clone(); assert!(matches!( config.validate(), Err(ConfigValidationError::DuplicateReplacementSource { .. }) )); let mut config = Config::embedded(); config.replacements[0].source.clear(); assert!(matches!( config.validate(), Err(ConfigValidationError::InvalidReplacementSource { .. }) )); let mut config = Config::embedded(); config.replacements[0].replacement = "—".to_owned(); assert!(matches!( config.validate(), Err(ConfigValidationError::InvalidReplacementTarget { .. }) )); let mut config = Config::embedded(); config.rules[0].weight = f64::NAN; assert!(matches!( config.validate(), Err(ConfigValidationError::InvalidWeight { .. }) )); let mut config = Config::embedded(); let regex_rule = config .rules .iter_mut() .find(|rule| rule.matcher == MatcherType::Regex) .unwrap(); regex_rule.pattern = "(".to_owned(); assert!(matches!( config.validate(), Err(ConfigValidationError::InvalidRegex { .. }) )); let mut config = Config::embedded(); config.rules[0].scope.include.push("[".to_owned()); assert!(matches!( config.validate(), Err(ConfigValidationError::InvalidGlob { field: "include", .. }) )); } #[test] fn malformed_layers_preserve_the_file_and_key_for_diagnostics() { use miette::Diagnostic as _; let directory = tempfile::tempdir().unwrap(); let path = directory.path().join("config.toml"); std::fs::write(&path, "[input]\nunknown_limit = 12\n").unwrap(); let paths = ConfigPaths::in_directory(directory.path().to_owned(), None, None); let error = ConfigLoader::from_paths(paths).load().unwrap_err(); match &error { ConfigError::Layer(layer) => { assert_eq!(layer.path, path); assert!(layer.key.contains("unknown_limit")); } other => panic!("unexpected error: {other:?}"), } assert_eq!( error.code().unwrap().to_string(), "john_slop::config::invalid_value" ); } #[test] fn each_file_layer_can_override_embedded_defaults_independently() { let base_dir = tempfile::tempdir().unwrap(); std::fs::write( base_dir.path().join("config.toml"), "[thresholds]\nlow = 21\n", ) .unwrap(); let base = ConfigLoader::from_paths(ConfigPaths::in_directory( base_dir.path().to_owned(), None, None, )) .load() .unwrap(); assert_eq!(base.thresholds.low, 21); let named_dir = tempfile::tempdir().unwrap(); std::fs::write( named_dir.path().join("strict.toml"), "[thresholds]\nlow = 22\n", ) .unwrap(); let name = "strict".parse::().unwrap(); let named = ConfigLoader::from_paths(ConfigPaths::in_directory( named_dir.path().to_owned(), Some(&name), None, )) .load() .unwrap(); assert_eq!(named.thresholds.low, 22); let explicit_dir = tempfile::tempdir().unwrap(); let explicit_path = explicit_dir.path().join("project.toml"); std::fs::write(&explicit_path, "[thresholds]\nlow = 23\n").unwrap(); let explicit = ConfigLoader::from_paths(ConfigPaths::in_directory( explicit_dir.path().to_owned(), None, Some(explicit_path), )) .load() .unwrap(); assert_eq!(explicit.thresholds.low, 23); } #[test] fn named_layers_apply_only_when_selected() { let directory = tempfile::tempdir().unwrap(); std::fs::write( directory.path().join("strict.toml"), "[thresholds]\nlow = 25\n", ) .unwrap(); let unselected = ConfigLoader::from_paths(ConfigPaths::in_directory( directory.path().to_owned(), None, None, )) .load() .unwrap(); let name = "strict".parse::().unwrap(); let selected = ConfigLoader::from_paths(ConfigPaths::in_directory( directory.path().to_owned(), Some(&name), None, )) .load() .unwrap(); assert_eq!(unselected.thresholds.low, 20); assert_eq!(selected.thresholds.low, 25); } #[test] fn malformed_toml_is_a_fatal_layer_error() { let directory = tempfile::tempdir().unwrap(); let path = directory.path().join("config.toml"); std::fs::write(&path, "[input\nmax_document_bytes = 12\n").unwrap(); let paths = ConfigPaths::in_directory(directory.path().to_owned(), None, None); let error = ConfigLoader::from_paths(paths).load().unwrap_err(); assert!(matches!(error, ConfigError::Layer(layer) if layer.path == path)); } }