feat: embed typed replacement configuration

This commit is contained in:
Dandaedre 2026-09-29 13:16:30 +02:00
parent 4a065d4fc9
commit 845fb891f2
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
4 changed files with 124 additions and 41 deletions

90
assets/config.toml Normal file
View File

@ -0,0 +1,90 @@
# Embedded replacement and input defaults. User configuration layers may
# override these values, but installed binaries never require this source file.
schema_version = 1
[input]
max_document_bytes = 8388608
max_archive_bytes = 67108864
max_expanded_bytes = 268435456
max_archive_entries = 10000
max_compression_ratio = 100
max_walk_depth = 64
# A replacement entry has a stable lowercase kebab-case ID, exactly one source
# character, an ASCII target, a rationale, and an enabled switch. Example:
# [[replacements]]
# id = "example-symbol"
# source = "→"
# replacement = "->"
# rationale = "Prefer an ASCII arrow in plain-text output."
# enabled = true
[[replacements]]
id = "hyphen"
source = "‐"
replacement = "-"
rationale = "Replace the Unicode hyphen with ASCII hyphen-minus."
enabled = true
[[replacements]]
id = "non-breaking-hyphen"
source = "‑"
replacement = "-"
rationale = "Replace the non-breaking hyphen with ASCII hyphen-minus."
enabled = true
[[replacements]]
id = "figure-dash"
source = "‒"
replacement = "-"
rationale = "Replace the figure dash with ASCII hyphen-minus."
enabled = true
[[replacements]]
id = "en-dash"
source = "–"
replacement = "-"
rationale = "Replace the en dash with ASCII hyphen-minus."
enabled = true
[[replacements]]
id = "em-dash"
source = "—"
replacement = "-"
rationale = "Replace the em dash with ASCII hyphen-minus."
enabled = true
[[replacements]]
id = "horizontal-bar"
source = "―"
replacement = "-"
rationale = "Replace the horizontal bar with ASCII hyphen-minus."
enabled = true
[[replacements]]
id = "minus-sign"
source = "−"
replacement = "-"
rationale = "Replace the mathematical minus sign with ASCII hyphen-minus."
enabled = true
[[replacements]]
id = "small-em-dash"
source = "﹘"
replacement = "-"
rationale = "Replace the small em dash with ASCII hyphen-minus."
enabled = true
[[replacements]]
id = "small-hyphen-minus"
source = "﹣"
replacement = "-"
rationale = "Replace the small hyphen-minus with ASCII hyphen-minus."
enabled = true
[[replacements]]
id = "fullwidth-hyphen-minus"
source = "-"
replacement = "-"
rationale = "Replace the fullwidth hyphen-minus with ASCII hyphen-minus."
enabled = true

View File

@ -1,19 +1,35 @@
//! Typed configuration consumed by the shared library.
use figment::{
Figment,
providers::{Format, Toml},
};
use serde::{Deserialize, Serialize};
const EMBEDDED_CONFIG: &str = include_str!("../assets/config.toml");
/// One configured Unicode-to-ASCII replacement.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReplacementRule {
/// Stable identifier used by diagnostics and overrides.
pub id: &'static str,
pub id: String,
/// Unicode character to replace.
pub source: char,
/// ASCII replacement text.
pub replacement: &'static str,
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, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, 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>,
/// Resource and traversal limits applied while reading inputs.
@ -21,7 +37,8 @@ pub struct Config {
}
/// Bounded resource policy for filesystem and archive inputs.
#[derive(Debug, Clone, PartialEq, Eq)]
#[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,
@ -52,33 +69,10 @@ impl Default for InputLimits {
impl Config {
/// Load the replacement defaults embedded in this build.
///
/// Layered TOML sources will replace this constructor during Milestone 2.
pub fn embedded() -> Self {
const HYPHENS: &[(char, &str)] = &[
('\u{2010}', "hyphen"),
('\u{2011}', "non-breaking-hyphen"),
('\u{2012}', "figure-dash"),
('\u{2013}', "en-dash"),
('\u{2014}', "em-dash"),
('\u{2015}', "horizontal-bar"),
('\u{2212}', "minus-sign"),
('\u{fe58}', "small-em-dash"),
('\u{fe63}', "small-hyphen-minus"),
('\u{ff0d}', "fullwidth-hyphen-minus"),
];
Self {
replacements: HYPHENS
.iter()
.map(|(source, id)| ReplacementRule {
id,
source: *source,
replacement: "-",
})
.collect(),
input: InputLimits::default(),
}
Figment::from(Toml::string(EMBEDDED_CONFIG))
.extract()
.expect("embedded replacement configuration must be valid")
}
}
@ -100,7 +94,7 @@ mod tests {
let ids = config
.replacements
.iter()
.map(|rule| rule.id)
.map(|rule| rule.id.as_str())
.collect::<BTreeSet<_>>();
let sources = config
.replacements
@ -109,14 +103,13 @@ mod tests {
.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.source.is_ascii() && rule.replacement.is_ascii())
);
assert!(config.replacements.iter().all(|rule| rule.enabled
&& !rule.source.is_ascii()
&& rule.replacement.is_ascii()
&& !rule.rationale.is_empty()));
}
#[test]

View File

@ -10,10 +10,10 @@ pub fn plan(config: &Config, source: &str) -> Vec<TextEdit> {
let rule = config
.replacements
.iter()
.find(|rule| rule.source == character)?;
.find(|rule| rule.enabled && rule.source == character)?;
Some(TextEdit {
byte_span: start..start + character.len_utf8(),
replacement: rule.replacement.to_owned(),
replacement: rule.replacement.clone(),
})
})
.collect()

View File

@ -81,7 +81,7 @@ entry do not discard successful documents.
- [x] Add Serde, `figment` with TOML support, and a platform-aware configuration
directory crate such as `directories`.
- [ ] Create `assets/config.toml` as the embedded replacement catalog with
- [x] Create `assets/config.toml` as the embedded replacement catalog with
`schema_version` and documented examples.
- [ ] Create `assets/heuristics.toml` as a separate embedded rule catalog with
stable IDs, categories, matcher types, weights, messages, help, occurrence caps,