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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::path::Path;
use std::str::FromStr;
use once_cell::sync::Lazy;
use regex::Regex;
use crate::common::{Codepoint, CodepointIter, UcdFile, UcdFileByCodepoint};
use crate::error::Error;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct NameAlias {
pub codepoint: Codepoint,
pub alias: String,
pub label: NameAliasLabel,
}
impl UcdFile for NameAlias {
fn relative_file_path() -> &'static Path {
Path::new("NameAliases.txt")
}
}
impl UcdFileByCodepoint for NameAlias {
fn codepoints(&self) -> CodepointIter {
self.codepoint.into_iter()
}
}
impl FromStr for NameAlias {
type Err = Error;
fn from_str(line: &str) -> Result<NameAlias, Error> {
static PARTS: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
^
(?P<codepoint>[A-Z0-9]+);
\s*
(?P<alias>[^;]+);
\s*
(?P<label>\S+)
",
)
.unwrap()
});
let caps = match PARTS.captures(line.trim()) {
Some(caps) => caps,
None => return err!("invalid NameAliases line"),
};
Ok(NameAlias {
codepoint: caps["codepoint"].parse()?,
alias: caps.name("alias").unwrap().as_str().to_string(),
label: caps["label"].parse()?,
})
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NameAliasLabel {
Correction,
Control,
Alternate,
Figment,
Abbreviation,
}
impl Default for NameAliasLabel {
fn default() -> NameAliasLabel {
NameAliasLabel::Correction
}
}
impl FromStr for NameAliasLabel {
type Err = Error;
fn from_str(s: &str) -> Result<NameAliasLabel, Error> {
match s {
"correction" => Ok(NameAliasLabel::Correction),
"control" => Ok(NameAliasLabel::Control),
"alternate" => Ok(NameAliasLabel::Alternate),
"figment" => Ok(NameAliasLabel::Figment),
"abbreviation" => Ok(NameAliasLabel::Abbreviation),
unknown => err!("unknown name alias label: '{}'", unknown),
}
}
}
#[cfg(test)]
mod tests {
use super::{NameAlias, NameAliasLabel};
#[test]
fn parse1() {
let line = "0000;NULL;control\n";
let row: NameAlias = line.parse().unwrap();
assert_eq!(row.codepoint, 0x0);
assert_eq!(row.alias, "NULL");
assert_eq!(row.label, NameAliasLabel::Control);
}
#[test]
fn parse2() {
let line = "000B;VERTICAL TABULATION;control\n";
let row: NameAlias = line.parse().unwrap();
assert_eq!(row.codepoint, 0xB);
assert_eq!(row.alias, "VERTICAL TABULATION");
assert_eq!(row.label, NameAliasLabel::Control);
}
#[test]
fn parse3() {
let line = "0081;HIGH OCTET PRESET;figment\n";
let row: NameAlias = line.parse().unwrap();
assert_eq!(row.codepoint, 0x81);
assert_eq!(row.alias, "HIGH OCTET PRESET");
assert_eq!(row.label, NameAliasLabel::Figment);
}
#[test]
fn parse4() {
let line = "E01EF;VS256;abbreviation\n";
let row: NameAlias = line.parse().unwrap();
assert_eq!(row.codepoint, 0xE01EF);
assert_eq!(row.alias, "VS256");
assert_eq!(row.label, NameAliasLabel::Abbreviation);
}
}