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
use crate::Result;
pub trait SmallKana {
fn small(self) -> Result;
}
impl SmallKana for &str {
fn small(self) -> Result {
let utf16: Vec<u16> = self
.encode_utf16()
.map(|u16char| match u16char {
0x3042...0x304A
| 0x3084...0x3088
| 0x3064
| 0x30C4
| 0x30A2...0x30AA
| 0x30E4...0x30E8
if u16char % 2 == 0 =>
{
u16char - 0x0001
}
_ => u16char,
})
.collect();
Ok(String::from_utf16(&utf16).unwrap())
}
}
impl SmallKana for String {
fn small(self) -> Result {
let s: &str = &self;
s.small()
}
}
impl SmallKana for Result {
fn small(self) -> Result {
self?.small()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ToKana;
#[test]
fn aiueo_hira() {
assert_eq!("ぁぃぅぇぉ", "あいうえお".small().unwrap());
}
#[test]
fn yayuyo_hira() {
assert_eq!("ゃゅょ", "やゆよ".small().unwrap());
}
#[test]
fn tsu_hira() {
assert_eq!("っ", "つ".small().unwrap());
}
#[test]
fn aiueo_kata() {
assert_eq!("ァィゥェォ", "アイウエオ".small().unwrap());
}
#[test]
fn yayuyo_kata() {
assert_eq!("ャュョ", "ヤユヨ".small().unwrap());
}
#[test]
fn tsu_kata() {
assert_eq!("ッ", "ツ".small().unwrap());
}
#[test]
fn unrecognized_pattern() {
if let Ok(_) = "unrecognized".hira().small() {
panic!("Unrecognized pattern was Ok");
}
}
}