Skip to content

Commit

Permalink
more renames for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Fogapod committed Nov 15, 2023
1 parent 860b88f commit 8cce35f
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/accent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ use regex::Regex;
#[cfg_attr(feature = "deserialize", serde(try_from = "AccentDef"))]
pub struct Accent {
normalize_case: bool,
// a copy of replacements for each severity level, sorted from lowest to highest
// a set of rules for each severity level, sorted from lowest to highest
severities: Vec<(u64, Vec<Rule>)>,
}

impl Accent {
fn merge_patterns(
words: Vec<(Regex, Replacement)>,
patterns: Vec<(Regex, Replacement)>,
fn merge_rules(
first: Vec<(Regex, Replacement)>,
second: Vec<(Regex, Replacement)>,
) -> Vec<Rule> {
words
first
.into_iter()
.chain(patterns)
.chain(second)
.map(|(regex, replacement)| Rule {
source: regex,
replacement,
Expand All @@ -33,9 +33,9 @@ impl Accent {
}

// keeps collection order, rewrites left duplicates with right ones
fn dedup_patterns(
fn dedup_rules(
collection: Vec<(Regex, Replacement)>,
collection_name: &str,
pretty_name: &str,
drop_expected: bool,
) -> Vec<(Regex, Replacement)> {
let mut filtered = vec![];
Expand All @@ -50,7 +50,7 @@ impl Accent {
"{} already present at position {} in {}",
word.0,
previous,
collection_name,
pretty_name,
);
}
} else {
Expand All @@ -69,25 +69,25 @@ impl Accent {
mut patterns: Vec<(Regex, Replacement)>,
severities_def: BTreeMap<u64, Severity>,
) -> Self {
words = Self::dedup_patterns(words, "words", false);
patterns = Self::dedup_patterns(patterns, "patterns", false);
words = Self::dedup_rules(words, "words", false);
patterns = Self::dedup_rules(patterns, "patterns", false);

let mut severities = Vec::with_capacity(severities_def.len());

severities.push((0, Self::merge_patterns(words.clone(), patterns.clone())));
severities.push((0, Self::merge_rules(words.clone(), patterns.clone())));

for (severity, override_or_addition) in severities_def {
let replacements = match override_or_addition {
let rules = match override_or_addition {
Severity::Replace(overrides) => {
words = Self::dedup_patterns(overrides.words, "words", false);
patterns = Self::dedup_patterns(overrides.patterns, "patterns", false);
words = Self::dedup_rules(overrides.words, "words", false);
patterns = Self::dedup_rules(overrides.patterns, "patterns", false);

Self::merge_patterns(words.clone(), patterns.clone())
Self::merge_rules(words.clone(), patterns.clone())
}
Severity::Extend(additions) => {
// no duplicates are allowed inside new definitions
let new_words = Self::dedup_patterns(additions.words, "words", false);
let new_patterns = Self::dedup_patterns(additions.patterns, "patterns", false);
let new_words = Self::dedup_rules(additions.words, "words", false);
let new_patterns = Self::dedup_rules(additions.patterns, "patterns", false);

// NOTE: we do not just add everything to the end of `replacements`. words and
// patterns maintain relative order where words are always first
Expand All @@ -96,14 +96,14 @@ impl Accent {

// we deduped old and new words separately, now they are merged. dedup again
// without warnings. new ones take priority over old while keeping position
words = Self::dedup_patterns(words, "words", true);
patterns = Self::dedup_patterns(patterns, "patterns", true);
words = Self::dedup_rules(words, "words", true);
patterns = Self::dedup_rules(patterns, "patterns", true);

Self::merge_patterns(words.clone(), patterns.clone())
Self::merge_rules(words.clone(), patterns.clone())
}
};

severities.push((severity, replacements));
severities.push((severity, rules));
}

Self {
Expand Down

0 comments on commit 8cce35f

Please sign in to comment.