290 lines
9.4 KiB
Rust
290 lines
9.4 KiB
Rust
//! Pure planning and application of configured text replacements.
|
||
|
||
use std::{borrow::Cow, ops::Range};
|
||
|
||
use miette::Diagnostic;
|
||
|
||
use crate::{Config, TextEdit};
|
||
|
||
/// An edit set cannot be applied safely to its source snapshot.
|
||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, Diagnostic)]
|
||
#[diagnostic(code(john_slop::replacement::invalid_edit))]
|
||
pub enum ReplacementError {
|
||
/// A range is empty, reversed, or extends beyond the source.
|
||
#[error("replacement byte range {start}..{end} is invalid for a {source_len}-byte source")]
|
||
InvalidRange {
|
||
/// Inclusive start byte supplied by the edit.
|
||
start: usize,
|
||
/// Exclusive end byte supplied by the edit.
|
||
end: usize,
|
||
/// Source snapshot length in bytes.
|
||
source_len: usize,
|
||
},
|
||
/// A range endpoint does not lie on a UTF-8 character boundary.
|
||
#[error("replacement byte offset {offset} is not a UTF-8 character boundary")]
|
||
InvalidCharacterBoundary {
|
||
/// Invalid endpoint.
|
||
offset: usize,
|
||
},
|
||
/// Two edits cover at least one common source byte.
|
||
#[error("replacement ranges overlap at byte {offset}")]
|
||
OverlappingEdits {
|
||
/// Start of the later edit inside an earlier edit.
|
||
offset: usize,
|
||
},
|
||
}
|
||
|
||
/// Plan deterministic non-overlapping edits without modifying the source.
|
||
pub fn plan(config: &Config, source: &str) -> Vec<TextEdit> {
|
||
let mut rules = config
|
||
.replacements
|
||
.iter()
|
||
.filter(|rule| rule.enabled)
|
||
.collect::<Vec<_>>();
|
||
rules.sort_by(|left, right| {
|
||
right
|
||
.source
|
||
.len()
|
||
.cmp(&left.source.len())
|
||
.then_with(|| left.id.cmp(&right.id))
|
||
});
|
||
|
||
let mut edits = Vec::new();
|
||
let mut offset = 0;
|
||
while offset < source.len() {
|
||
if let Some(rule) = rules
|
||
.iter()
|
||
.find(|rule| source[offset..].starts_with(&rule.source))
|
||
{
|
||
let end = offset + rule.source.len();
|
||
edits.push(TextEdit {
|
||
byte_span: offset..end,
|
||
replacement: rule.replacement.clone(),
|
||
});
|
||
offset = end;
|
||
} else {
|
||
offset += source[offset..].chars().next().map_or(0, char::len_utf8);
|
||
}
|
||
}
|
||
edits
|
||
}
|
||
|
||
/// Validate and apply edits from the end of the source toward the beginning.
|
||
///
|
||
/// With no edits, the returned [`Cow`] borrows the original source without an
|
||
/// allocation. All ranges refer to the original source snapshot.
|
||
pub fn apply<'source>(
|
||
source: &'source str,
|
||
edits: &[TextEdit],
|
||
) -> Result<Cow<'source, str>, ReplacementError> {
|
||
if edits.is_empty() {
|
||
return Ok(Cow::Borrowed(source));
|
||
}
|
||
|
||
let mut ordered = edits.iter().collect::<Vec<_>>();
|
||
ordered.sort_by(|left, right| range_order(&left.byte_span, &right.byte_span));
|
||
let mut previous_end = 0;
|
||
for edit in &ordered {
|
||
let range = &edit.byte_span;
|
||
if range.start >= range.end || range.end > source.len() {
|
||
return Err(ReplacementError::InvalidRange {
|
||
start: range.start,
|
||
end: range.end,
|
||
source_len: source.len(),
|
||
});
|
||
}
|
||
for offset in [range.start, range.end] {
|
||
if !source.is_char_boundary(offset) {
|
||
return Err(ReplacementError::InvalidCharacterBoundary { offset });
|
||
}
|
||
}
|
||
if range.start < previous_end {
|
||
return Err(ReplacementError::OverlappingEdits {
|
||
offset: range.start,
|
||
});
|
||
}
|
||
previous_end = range.end;
|
||
}
|
||
|
||
let mut output = source.to_owned();
|
||
for edit in ordered.into_iter().rev() {
|
||
output.replace_range(edit.byte_span.clone(), &edit.replacement);
|
||
}
|
||
Ok(Cow::Owned(output))
|
||
}
|
||
|
||
fn range_order(left: &Range<usize>, right: &Range<usize>) -> std::cmp::Ordering {
|
||
(left.start, left.end).cmp(&(right.start, right.end))
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn plans_only_configured_non_ascii_characters() {
|
||
let edits = plan(&Config::embedded(), "plain-word—fixed");
|
||
|
||
assert_eq!(edits.len(), 1);
|
||
assert_eq!(edits[0].byte_span, 10..13);
|
||
assert_eq!(edits[0].replacement, "-");
|
||
}
|
||
|
||
#[test]
|
||
fn plans_multiple_edits_in_source_order_with_utf8_ranges() {
|
||
let edits = plan(&Config::embedded(), "a—b−c");
|
||
|
||
assert_eq!(
|
||
edits,
|
||
vec![
|
||
TextEdit {
|
||
byte_span: 1..4,
|
||
replacement: "-".to_owned(),
|
||
},
|
||
TextEdit {
|
||
byte_span: 5..8,
|
||
replacement: "-".to_owned(),
|
||
},
|
||
]
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn empty_or_ascii_sources_need_no_edits() {
|
||
let config = Config::embedded();
|
||
|
||
assert!(plan(&config, "").is_empty());
|
||
assert!(plan(&config, "ordinary-ascii").is_empty());
|
||
}
|
||
|
||
#[test]
|
||
fn longest_source_wins_independently_of_configuration_order() {
|
||
let mut config = Config::embedded();
|
||
config.replacements.insert(
|
||
0,
|
||
crate::ReplacementRule {
|
||
id: "double-em-dash".to_owned(),
|
||
source: "——".to_owned(),
|
||
replacement: "--".to_owned(),
|
||
rationale: "Preserve a doubled dash.".to_owned(),
|
||
enabled: true,
|
||
},
|
||
);
|
||
config.validate().unwrap();
|
||
|
||
let edits = plan(&config, "a——b—c");
|
||
|
||
assert_eq!(edits.len(), 2);
|
||
assert_eq!(edits[0].byte_span, 1..7);
|
||
assert_eq!(edits[0].replacement, "--");
|
||
assert_eq!(edits[1].byte_span, 8..11);
|
||
}
|
||
|
||
#[test]
|
||
fn applies_edits_in_reverse_and_preserves_line_endings() {
|
||
let source = "a—b\r\nc−d\n";
|
||
let edits = plan(&Config::embedded(), source);
|
||
|
||
assert_eq!(apply(source, &edits).unwrap(), "a-b\r\nc-d\n");
|
||
assert_eq!(
|
||
apply(&apply(source, &edits).unwrap(), &[]).unwrap(),
|
||
"a-b\r\nc-d\n"
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn no_edits_borrow_the_original_source() {
|
||
let source = "ordinary ASCII";
|
||
|
||
assert!(matches!(apply(source, &[]).unwrap(), Cow::Borrowed(value) if value == source));
|
||
}
|
||
|
||
#[test]
|
||
fn rejects_invalid_boundaries_ranges_and_overlaps() {
|
||
let invalid_boundary = [TextEdit {
|
||
byte_span: 1..2,
|
||
replacement: "-".to_owned(),
|
||
}];
|
||
assert!(matches!(
|
||
apply("é", &invalid_boundary),
|
||
Err(ReplacementError::InvalidCharacterBoundary { offset: 1 })
|
||
));
|
||
|
||
let invalid_range = [TextEdit {
|
||
byte_span: 0..3,
|
||
replacement: "-".to_owned(),
|
||
}];
|
||
assert!(matches!(
|
||
apply("ab", &invalid_range),
|
||
Err(ReplacementError::InvalidRange { .. })
|
||
));
|
||
|
||
let overlapping = [
|
||
TextEdit {
|
||
byte_span: 0..2,
|
||
replacement: "x".to_owned(),
|
||
},
|
||
TextEdit {
|
||
byte_span: 1..3,
|
||
replacement: "y".to_owned(),
|
||
},
|
||
];
|
||
assert!(matches!(
|
||
apply("abc", &overlapping),
|
||
Err(ReplacementError::OverlappingEdits { offset: 1 })
|
||
));
|
||
}
|
||
|
||
#[test]
|
||
fn generated_sequences_are_stable_non_overlapping_and_idempotent() {
|
||
let config = Config::embedded();
|
||
let alphabet = ["a", "é", "—", "−", "\r", "\n"];
|
||
|
||
for first in alphabet {
|
||
for second in alphabet {
|
||
for third in alphabet {
|
||
for fourth in alphabet {
|
||
let source = [first, second, third, fourth].concat();
|
||
let edits = plan(&config, &source);
|
||
assert_eq!(edits, plan(&config, &source));
|
||
for edit in &edits {
|
||
assert!(source.is_char_boundary(edit.byte_span.start));
|
||
assert!(source.is_char_boundary(edit.byte_span.end));
|
||
}
|
||
assert!(
|
||
edits
|
||
.windows(2)
|
||
.all(|pair| { pair[0].byte_span.end <= pair[1].byte_span.start })
|
||
);
|
||
|
||
let formatted = apply(&source, &edits).unwrap();
|
||
assert_eq!(
|
||
source
|
||
.chars()
|
||
.filter(|character| *character == '\r')
|
||
.count(),
|
||
formatted
|
||
.chars()
|
||
.filter(|character| *character == '\r')
|
||
.count()
|
||
);
|
||
assert_eq!(
|
||
source
|
||
.chars()
|
||
.filter(|character| *character == '\n')
|
||
.count(),
|
||
formatted
|
||
.chars()
|
||
.filter(|character| *character == '\n')
|
||
.count()
|
||
);
|
||
let second_edits = plan(&config, &formatted);
|
||
assert!(second_edits.is_empty());
|
||
assert_eq!(apply(&formatted, &second_edits).unwrap(), formatted);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|