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
extern crate phf;
use std::cmp::Ordering;
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
#[derive(PartialEq, Eq, PartialOrd, Debug)]
pub enum PinYin {
Other(char),
Chinese(char, &'static str),
}
impl Ord for PinYin {
fn cmp(&self, other: &PinYin) -> Ordering {
match *self {
PinYin::Other(ref sc) => {
match *other {
PinYin::Other(ref oc) => sc.cmp(oc),
PinYin::Chinese(_, _) => Ordering::Less,
}
}
PinYin::Chinese(ref sc, ref sp) => {
match *other {
PinYin::Other(_) => Ordering::Greater,
PinYin::Chinese(ref oc, ref op) => {
let pinyin_order = sp.cmp(op);
if pinyin_order == Ordering::Equal {
sc.cmp(oc)
} else {
pinyin_order
}
}
}
}
}
}
}
pub fn as_pinyin(s: &str) -> Vec<PinYin> {
s.chars()
.map(|c| match PINYIN_MAP.get(&c) {
Some(pinyin) => PinYin::Chinese(c, pinyin),
None => PinYin::Other(c),
})
.collect()
}