john-slop/src/config.rs

1799 lines
59 KiB
Rust

//! 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<Self, Self::Err> {
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<PathBuf>,
/// Explicit configuration supplied for this invocation, if any.
pub explicit: Option<PathBuf>,
}
/// 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,
/// Evidence-score 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<u64>,
/// Override the maximum accepted bytes for an outer archive.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_archive_bytes: Option<u64>,
/// Override the maximum total expanded bytes for an archive.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_expanded_bytes: Option<u64>,
/// Override the maximum member count for an archive.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_archive_entries: Option<usize>,
/// Override the maximum expanded-to-compressed archive ratio.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_compression_ratio: Option<u64>,
/// Override the maximum recursive directory depth.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_walk_depth: Option<usize>,
}
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 evidence band boundaries.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct ThresholdOverrides {
/// Override the first low evidence score.
#[serde(skip_serializing_if = "Option::is_none")]
pub low: Option<u8>,
/// Override the first medium evidence score.
#[serde(skip_serializing_if = "Option::is_none")]
pub medium: Option<u8>,
/// Override the first high evidence score.
#[serde(skip_serializing_if = "Option::is_none")]
pub high: Option<u8>,
}
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<PathBuf>,
) -> Result<Self, ConfigError> {
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<Config, ConfigError> {
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<figment::Error>),
/// 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<ConfigLayerError>),
/// 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<figment::Error>,
/// Exact file text associated with the provider failure.
#[source_code]
source_code: NamedSource<String>,
/// 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,
},
/// Evidence thresholds are not strictly increasing.
#[error("evidence 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::<ConfigLayer>()
.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(),
_ => "<document>".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<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<String>,
replacement: Option<String>,
rationale: Option<String>,
enabled: Option<bool>,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RulePatch {
id: String,
category: Option<Category>,
severity: Option<FindingSeverity>,
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 EvidenceThresholds, 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.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<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> {
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<PathBuf>,
) -> 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<ReplacementRule>,
/// Configured detection rules in deterministic declaration order.
pub rules: Vec<HeuristicRule>,
/// Named evidence-band boundaries.
pub thresholds: EvidenceThresholds,
/// 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<ContentKind>,
/// Lowercase extensions without a leading dot; empty means unrestricted.
pub extensions: Vec<String>,
/// Path globs that opt inputs into this rule; empty means unrestricted.
#[serde(default)]
pub include: Vec<String>,
/// Path globs that opt inputs out of this rule.
#[serde(default)]
pub exclude: Vec<String>,
/// 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,
}
/// Boundaries used to name aggregate evidence-score bands.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EvidenceThresholds {
/// First score considered low evidence.
pub low: u8,
/// First score considered medium evidence.
pub medium: u8,
/// First score considered high evidence.
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: EvidenceThresholds) -> 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::<BTreeSet<_>>();
let sources = config
.replacements
.iter()
.map(|rule| rule.source.as_str())
.collect::<BTreeSet<_>>();
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::<ConfigName>().unwrap().as_str(), accepted);
}
for rejected in [
"",
".",
"../work",
"work.toml",
"a/b",
"équipe",
"has space",
] {
assert!(
rejected.parse::<ConfigName>().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::<ConfigName>().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::<ConfigName>().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::<ConfigName>().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::<ConfigName>().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));
}
#[test]
fn all_cli_scalar_overrides_are_applied_and_serialize_compactly() {
let overrides = ConfigOverrides {
input: InputOverrides {
max_document_bytes: Some(11),
max_archive_bytes: Some(22),
max_expanded_bytes: Some(33),
max_archive_entries: Some(44),
max_compression_ratio: Some(55),
max_walk_depth: Some(66),
},
thresholds: ThresholdOverrides {
low: Some(10),
medium: Some(40),
high: Some(90),
},
};
let mut config = Config::embedded();
apply_cli_overrides(&mut config, &overrides);
assert_eq!(config.input.max_document_bytes, 11);
assert_eq!(config.input.max_archive_bytes, 22);
assert_eq!(config.input.max_expanded_bytes, 33);
assert_eq!(config.input.max_archive_entries, 44);
assert_eq!(config.input.max_compression_ratio, 55);
assert_eq!(config.input.max_walk_depth, 66);
assert_eq!(
config.thresholds,
EvidenceThresholds {
low: 10,
medium: 40,
high: 90
}
);
assert_eq!(toml::to_string(&ConfigOverrides::default()).unwrap(), "");
let serialized = toml::to_string(&overrides).unwrap();
assert!(serialized.contains("max_archive_entries = 44"));
assert!(serialized.contains("high = 90"));
}
#[test]
fn full_existing_patches_and_input_layers_update_every_field() {
let directory = tempfile::tempdir().unwrap();
std::fs::write(
directory.path().join("config.toml"),
r#"
schema_version = 1
[input]
max_document_bytes = 101
max_archive_bytes = 102
max_expanded_bytes = 103
max_archive_entries = 104
max_compression_ratio = 105
max_walk_depth = 106
[thresholds]
low = 10
medium = 40
high = 90
[[replacements]]
id = "em-dash"
source = "⸺"
replacement = "--"
rationale = "Test every replacement patch field."
enabled = false
[[rules]]
id = "templated-conclusion"
category = "source-code"
severity = "error"
matcher = "exact-phrase"
pattern = "patched phrase"
weight = 7.5
message = "patched message"
help = "patched help"
occurrence_cap = 4
enabled = false
scope = { content = ["source-code"], extensions = ["rs"], include = ["src/**"], exclude = ["vendor/**"], skip_generated = false, skip_vendor = false, skip_minified = false }
"#,
)
.unwrap();
let config = ConfigLoader::from_paths(ConfigPaths::in_directory(
directory.path().to_owned(),
None,
None,
))
.load()
.unwrap();
assert_eq!(config.input.max_document_bytes, 101);
assert_eq!(config.input.max_archive_bytes, 102);
assert_eq!(config.input.max_expanded_bytes, 103);
assert_eq!(config.input.max_archive_entries, 104);
assert_eq!(config.input.max_compression_ratio, 105);
assert_eq!(config.input.max_walk_depth, 106);
assert_eq!(
config.thresholds,
EvidenceThresholds {
low: 10,
medium: 40,
high: 90
}
);
let replacement = config
.replacements
.iter()
.find(|rule| rule.id == "em-dash")
.unwrap();
assert_eq!(replacement.source, "⸺");
assert_eq!(replacement.replacement, "--");
assert_eq!(replacement.rationale, "Test every replacement patch field.");
assert!(!replacement.enabled);
let rule = config
.rules
.iter()
.find(|rule| rule.id == "templated-conclusion")
.unwrap();
assert_eq!(rule.category, Category::SourceCode);
assert_eq!(rule.severity, FindingSeverity::Error);
assert_eq!(rule.matcher, MatcherType::ExactPhrase);
assert_eq!(rule.pattern, "patched phrase");
assert_eq!(rule.weight, 7.5);
assert_eq!(rule.message, "patched message");
assert_eq!(rule.help, "patched help");
assert_eq!(rule.occurrence_cap, 4);
assert!(!rule.enabled);
assert_eq!(rule.scope.extensions, ["rs"]);
assert!(!rule.scope.skip_generated);
}
#[test]
fn validation_reports_each_semantic_error_family() {
fn assert_invalid(
mutator: impl FnOnce(&mut Config),
expected: fn(&ConfigValidationError) -> bool,
) {
let mut config = Config::embedded();
mutator(&mut config);
let error = config.validate().unwrap_err();
assert!(expected(&error), "unexpected validation error: {error:?}");
}
assert_invalid(
|config| config.schema_version = 2,
|error| {
matches!(
error,
ConfigValidationError::UnsupportedSchema { actual: 2 }
)
},
);
for field in 0..6 {
assert_invalid(
|config| match field {
0 => config.input.max_document_bytes = 0,
1 => config.input.max_archive_bytes = 0,
2 => config.input.max_expanded_bytes = 0,
3 => config.input.max_archive_entries = 0,
4 => config.input.max_compression_ratio = 0,
_ => config.input.max_walk_depth = 0,
},
|error| matches!(error, ConfigValidationError::ZeroInputLimit { .. }),
);
}
assert_invalid(
|config| config.replacements[0].id = "Bad_ID".into(),
|error| {
matches!(
error,
ConfigValidationError::InvalidId {
kind: "replacement",
..
}
)
},
);
assert_invalid(
|config| config.replacements[1].id = config.replacements[0].id.clone(),
|error| {
matches!(
error,
ConfigValidationError::DuplicateId {
kind: "replacement",
..
}
)
},
);
assert_invalid(
|config| config.replacements[0].rationale = " ".into(),
|error| {
matches!(
error,
ConfigValidationError::EmptyField {
kind: "replacement",
field: "rationale",
..
}
)
},
);
assert_invalid(
|config| config.rules[0].id = "-bad".into(),
|error| matches!(error, ConfigValidationError::InvalidId { kind: "rule", .. }),
);
assert_invalid(
|config| config.rules[1].id = config.rules[0].id.clone(),
|error| {
matches!(
error,
ConfigValidationError::DuplicateId { kind: "rule", .. }
)
},
);
assert_invalid(
|config| config.rules[0].message.clear(),
|error| {
matches!(
error,
ConfigValidationError::EmptyField {
kind: "rule",
field: "message",
..
}
)
},
);
assert_invalid(
|config| config.rules[0].weight = -1.0,
|error| matches!(error, ConfigValidationError::InvalidWeight { .. }),
);
assert_invalid(
|config| config.rules[0].occurrence_cap = 0,
|error| matches!(error, ConfigValidationError::ZeroOccurrenceCap { .. }),
);
assert_invalid(
|config| {
config.rules[0].matcher = MatcherType::Regex;
config.rules[0].pattern = "a*".into();
},
|error| matches!(error, ConfigValidationError::EmptyMatch { .. }),
);
assert_invalid(
|config| {
config.rules[0].matcher = MatcherType::Replacement;
config.rules[0].pattern = "wrong".into();
},
|error| {
matches!(
error,
ConfigValidationError::InvalidReplacementMatcher { .. }
)
},
);
assert_invalid(
|config| config.rules[0].scope.content.clear(),
|error| matches!(error, ConfigValidationError::EmptyScope { .. }),
);
assert_invalid(
|config| config.rules[0].scope.extensions = vec!["Rust".into()],
|error| matches!(error, ConfigValidationError::InvalidExtension { .. }),
);
assert_invalid(
|config| config.rules[0].scope.exclude.push("[".into()),
|error| {
matches!(
error,
ConfigValidationError::InvalidGlob {
field: "exclude",
..
}
)
},
);
}
#[test]
fn duplicate_and_incomplete_new_layer_entries_are_actionable() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("config.toml");
std::fs::write(
&path,
"[[replacements]]\nid = \"em-dash\"\n[[replacements]]\nid = \"em-dash\"\n",
)
.unwrap();
let error = ConfigLoader::from_paths(ConfigPaths::in_directory(
directory.path().to_owned(),
None,
None,
))
.load()
.unwrap_err();
assert!(matches!(
error,
ConfigError::DuplicateLayerId {
kind: "replacement",
..
}
));
std::fs::write(&path, "[[rules]]\nid = \"new-rule\"\n").unwrap();
let error = ConfigLoader::from_paths(ConfigPaths::in_directory(
directory.path().to_owned(),
None,
None,
))
.load()
.unwrap_err();
assert!(matches!(
error,
ConfigError::IncompleteEntry {
kind: "rule",
field: "category",
..
}
));
}
}