Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): support more currencies
Elijah Potterd39380f6c7fc
diff --git a/harper-core/src/currency.rs b/harper-core/src/currency.rsnew file mode 100644index 000000000..4121c810a--- /dev/null+++ b/harper-core/src/currency.rs@@ -0,0 +1,41 @@+use is_macro::Is;+use serde::{Deserialize, Serialize};++#[derive(Debug, Is, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Hash)]+pub enum Currency {+ // $+ Dollar,+ // €+ Euro,+ // ₽+ Ruble,+ // ₺+ Lira,+ // £+ Pound,+}++impl Currency {+ pub fn from_char(c: char) -> Option<Self> {+ let cur = match c {+ '$' => Self::Dollar,+ '€' => Self::Euro,+ '₽' => Self::Ruble,+ '₺' => Self::Lira,+ '£' => Self::Pound,+ _ => return None,+ };++ Some(cur)+ }++ pub fn to_char(&self) -> char {+ match self {+ Self::Dollar => '$',+ Self::Euro => '€',+ Self::Ruble => '₽',+ Self::Lira => '₺',+ Self::Pound => '£',+ }+ }+}diff --git a/harper-core/src/lib.rs b/harper-core/src/lib.rsindex c3a71c80a..37801a975 100644--- a/harper-core/src/lib.rs+++ b/harper-core/src/lib.rs@@ -3,6 +3,7 @@mod char_ext;mod char_string;+mod currency;mod document;mod fat_token;pub mod language_detection;@@ -24,6 +25,7 @@ mod word_metadata;use std::collections::VecDeque;pub use char_string::{CharString, CharStringExt};+pub use currency::Currency;pub use document::Document;pub use fat_token::FatToken;use linting::Lint;diff --git a/harper-core/src/linting/dollar_placement.rs b/harper-core/src/linting/currency_placement.rssimilarity index 66%rename from harper-core/src/linting/dollar_placement.rsrename to harper-core/src/linting/currency_placement.rsindex 5052fbabf..7b86a0497 100644--- a/harper-core/src/linting/dollar_placement.rs+++ b/harper-core/src/linting/currency_placement.rs@@ -5,16 +5,17 @@ use crate::{use super::{Lint, LintKind, PatternLinter, Suggestion};-static DESCRIPTION: &str = "Dollar signs should always come before the quantity being described.";+static DESCRIPTION: &str =+ "Currency symbols should always come before the quantity being described.";-pub struct DollarPlacement {+pub struct CurrencyPlacement {pattern: Box<dyn Pattern>,}-impl Default for DollarPlacement {+impl Default for CurrencyPlacement {fn default() -> Self {- let dollar_pat = Box::new(|t: &Token, _source: &[char]| {- matches!(t.kind, TokenKind::Punctuation(Punctuation::Dollar))+ let currency_pat = Box::new(|t: &Token, _source: &[char]| {+ matches!(t.kind, TokenKind::Punctuation(Punctuation::Currency(..)))});let pattern = EitherPattern::new(vec![@@ -22,9 +23,9 @@ impl Default for DollarPlacement {SequencePattern::default().then_number().then_whitespace()- .then(dollar_pat.clone()),+ .then(currency_pat.clone()),),- Box::new(SequencePattern::default().then_number().then(dollar_pat)),+ Box::new(SequencePattern::default().then_number().then(currency_pat)),]);Self {@@ -33,17 +34,24 @@ impl Default for DollarPlacement {}}-impl PatternLinter for DollarPlacement {+impl PatternLinter for CurrencyPlacement {fn pattern(&self) -> &dyn Pattern {self.pattern.as_ref()}fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Lint {+ let currency_tok = matched_tokens.first_punctuation().unwrap();+ let currency = currency_tok+ .kind+ .as_punctuation()+ .unwrap()+ .expect_currency();+// We can unwrap like this thanks to the pattern.let number_tok = matched_tokens.first_number().unwrap();- let (value, suffix) = number_tok.kind.as_number().unwrap();+ let (value, suffix) = number_tok.kind.expect_number();- let mut fix = vec!['$'];+ let mut fix = vec![currency.to_char()];fix.extend(value.to_string().chars());@@ -69,13 +77,13 @@ impl PatternLinter for DollarPlacement {mod tests {use crate::linting::tests::assert_suggestion_result;- use super::DollarPlacement;+ use super::CurrencyPlacement;#[test]fn eof() {assert_suggestion_result("It was my last bill worth more than 4$.",- DollarPlacement::default(),+ CurrencyPlacement::default(),"It was my last bill worth more than $4.",);}@@ -84,7 +92,7 @@ mod tests {fn blog_title() {assert_suggestion_result("The Best 25$ I Ever Spent",- DollarPlacement::default(),+ CurrencyPlacement::default(),"The Best $25 I Ever Spent",);}@@ -93,25 +101,34 @@ mod tests {fn blog_title_with_space() {assert_suggestion_result("The Best 25 $ I Ever Spent",- DollarPlacement::default(),+ CurrencyPlacement::default(),"The Best $25 I Ever Spent",);}#[test]- fn multiple() {+ fn multiple_dollar() {assert_suggestion_result("They were either 25$ 24$ or 23$.",- DollarPlacement::default(),+ CurrencyPlacement::default(),"They were either $25 $24 or $23.",);}+ #[test]+ fn multiple_pound() {+ assert_suggestion_result(+ "They were either 25£ 24£ or 23£.",+ CurrencyPlacement::default(),+ "They were either £25 £24 or £23.",+ );+ }+#[test]fn suffix() {assert_suggestion_result("It was my 20th$.",- DollarPlacement::default(),+ CurrencyPlacement::default(),"It was my $20th.",);}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 8d311f40b..906a49c69 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -30,7 +30,7 @@ use super::that_which::ThatWhich;use super::unclosed_quotes::UnclosedQuotes;use super::use_genitive::UseGenitive;use super::wrong_quotes::WrongQuotes;-use super::{DollarPlacement, Lint, Linter, OxfordComma};+use super::{CurrencyPlacement, Lint, Linter, OxfordComma};use crate::{Dictionary, Document};macro_rules! create_lint_group_config {@@ -187,7 +187,7 @@ create_lint_group_config!(PluralConjugate => false,OxfordComma => true,PronounContraction => true,- DollarPlacement => true+ CurrencyPlacement => true);impl<T: Dictionary + Default> Default for LintGroup<T> {diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 8dc32f5b3..addc123cd 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -3,8 +3,8 @@ mod avoid_curses;mod boring_words;mod capitalize_personal_pronouns;mod correct_number_suffix;+mod currency_placement;mod dashes;-mod dollar_placement;mod dot_initialisms;mod ellipsis_length;mod linking_verbs;@@ -37,7 +37,7 @@ pub use avoid_curses::AvoidCurses;pub use boring_words::BoringWords;pub use capitalize_personal_pronouns::CapitalizePersonalPronouns;pub use correct_number_suffix::CorrectNumberSuffix;-pub use dollar_placement::DollarPlacement;+pub use currency_placement::CurrencyPlacement;pub use dot_initialisms::DotInitialisms;pub use ellipsis_length::EllipsisLength;pub use linking_verbs::LinkingVerbs;diff --git a/harper-core/src/punctuation.rs b/harper-core/src/punctuation.rsindex 767b809ec..a625e80a2 100644--- a/harper-core/src/punctuation.rs+++ b/harper-core/src/punctuation.rs@@ -1,6 +1,8 @@use is_macro::Is;use serde::{Deserialize, Serialize};+use crate::Currency;+#[derive(Debug, Is, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Default, Hash,)]@@ -69,8 +71,7 @@ pub enum Punctuation {Carrot,/// `+`Plus,- /// `$`- Dollar,+ Currency(Currency),/// `|`Pipe,/// `_`@@ -111,10 +112,9 @@ impl Punctuation {'…' => Punctuation::Ellipsis,'^' => Punctuation::Carrot,'+' => Punctuation::Plus,- '$' => Punctuation::Dollar,'|' => Punctuation::Pipe,'_' => Punctuation::Underscore,- _ => return None,+ _ => Punctuation::Currency(Currency::from_char(c)?),};Some(punct)