1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
extern crate cfg_if;
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(target_os = "macos")] {
mod mac;
use crate::mac as imp;
} else if #[cfg(windows)] {
mod win;
use crate::win as imp;
} else if #[cfg(unix)] {
mod unix;
use crate::unix as imp;
} else {
compile_error!("target platform is not supported");
}
}
#[derive(Debug)]
pub struct Checker(imp::Checker);
impl Checker {
pub fn new() -> Self {
Checker(imp::Checker::new())
}
pub fn check<'a, 'b: 'a>(
&'b mut self,
text: &'a str,
) -> impl Iterator<Item = SpellingError> + 'a {
self.0.check(text).map(SpellingError)
}
pub fn ignore(&mut self, word: &str) {
self.0.ignore(word)
}
}
pub struct SpellingError(imp::SpellingError);
impl SpellingError {
pub fn text(&self) -> &str {
self.0.text()
}
}
#[cfg(test)]
mod tests {
use super::Checker;
#[test]
fn no_errors() {
let text = "I'm happy that this sentence has no errors.";
let mut checker = Checker::new();
assert_eq!(checker.check(&text).count(), 0);
}
#[test]
fn single_error() {
let text = "asdf";
let mut checker = Checker::new();
let errors = checker.check(&text).collect::<Vec<_>>();
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].text(), "asdf");
}
#[test]
fn multiple_errors() {
let text = "asdf hjkl qwer uiop";
let mut checker = Checker::new();
let errors = checker.check(&text).collect::<Vec<_>>();
assert_eq!(errors.len(), 4);
assert_eq!(errors[0].text(), "asdf");
assert_eq!(errors[1].text(), "hjkl");
assert_eq!(errors[2].text(), "qwer");
assert_eq!(errors[3].text(), "uiop");
}
#[test]
fn empty() {
let mut checker = Checker::new();
assert_eq!(checker.check("").count(), 0);
}
#[test]
fn ignore() {
let mut checker = Checker::new();
assert_eq!(checker.check("foobarbaz").count(), 1);
checker.ignore("foobarbaz");
assert_eq!(checker.check("foobarbaz").count(), 0);
}
#[test]
fn ignore_not_permanent() {
let mut checker = Checker::new();
checker.ignore("foobarbaz");
drop(checker);
let mut checker = Checker::new();
assert_eq!(checker.check("foobarbaz").count(), 1);
}
}