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
pub mod asciifier {
use std::collections::HashMap;
pub struct Asciifier {
character_map: HashMap<char, &'static str>,
}
impl Asciifier {
pub fn new() -> Asciifier {
let mut char_map = HashMap::with_capacity(20);
char_map.insert('ä', "ae");
char_map.insert('ö', "oe");
char_map.insert('ü', "ue");
char_map.insert('Ä', "Ae");
char_map.insert('Ö', "Oe");
char_map.insert('Ü', "Ue");
char_map.insert('ß', "ss");
Asciifier {
character_map: char_map,
}
}
pub fn to_ascii(&self, from: String) -> String {
let mut to = from;
for (special_char, replacement) in &self.character_map {
to = to.replace(*special_char, replacement);
}
to
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn replace_single_char() {
let asciifier = super::Asciifier::new();
let ascii_string = asciifier.to_ascii("ä".to_string());
assert_eq!(&ascii_string, "ae");
}
#[test]
fn replace_lots() {
let asciifier = super::Asciifier::new();
let utf8_str = "ÄÖÜßäöü";
let ascii_string = asciifier.to_ascii(utf8_str.to_string());
assert_eq!(&ascii_string, "AeOeUessaeoeue");
}
}