1use std::path::Path;
2use std::str::FromStr;
3
4use once_cell::sync::Lazy;
5use regex::Regex;
6
7use crate::common::{Codepoint, CodepointIter, UcdFile, UcdFileByCodepoint};
8use crate::error::Error;
9
10/// Represents a single row in the `ArabicShaping.txt` file.
11///
12/// The field names were taken from the header of ArabicShaping.txt.
13#[derive(Clone, Debug, Default, Eq, PartialEq)]
14pub struct ArabicShaping {
15 /// The codepoint corresponding to this row.
16 pub codepoint: Codepoint,
17 /// A short schematic name for the codepoint.
18 ///
19 /// The schematic name is descriptive of the shape, based as consistently as
20 /// possible on a name for the skeleton and then the diacritic marks applied
21 /// to the skeleton, if any. Note that this schematic name is considered a
22 /// comment, and does not constitute a formal property value.
23 pub schematic_name: String,
24 /// The "joining type" of this codepoint.
25 pub joining_type: JoiningType,
26 /// The "joining group" of this codepoint.
27 pub joining_group: String,
28}
29
30/// The Joining_Type field read from ArabicShaping.txt
31#[derive(Clone, Copy, Debug, Eq, PartialEq)]
32pub enum JoiningType {
33 RightJoining,
34 LeftJoining,
35 DualJoining,
36 JoinCausing,
37 NonJoining,
38 Transparent,
39}
40
41impl JoiningType {
42 pub fn as_str(&self) -> &str {
43 match self {
44 JoiningType::RightJoining => "R",
45 JoiningType::LeftJoining => "L",
46 JoiningType::DualJoining => "D",
47 JoiningType::JoinCausing => "C",
48 JoiningType::NonJoining => "U",
49 JoiningType::Transparent => "T",
50 }
51 }
52}
53
54impl Default for JoiningType {
55 fn default() -> JoiningType {
56 JoiningType::NonJoining
57 }
58}
59
60impl FromStr for JoiningType {
61 type Err = Error;
62
63 fn from_str(s: &str) -> Result<JoiningType, Error> {
64 match s {
65 "R" => Ok(JoiningType::RightJoining),
66 "L" => Ok(JoiningType::LeftJoining),
67 "D" => Ok(JoiningType::DualJoining),
68 "C" => Ok(JoiningType::JoinCausing),
69 "U" => Ok(JoiningType::NonJoining),
70 "T" => Ok(JoiningType::Transparent),
71 _ => err!(
72 "unrecognized joining type: '{}' \
73 (must be one of R, L, D, C, U or T)",
74 s
75 ),
76 }
77 }
78}
79
80impl UcdFile for ArabicShaping {
81 fn relative_file_path() -> &'static Path {
82 Path::new("ArabicShaping.txt")
83 }
84}
85
86impl UcdFileByCodepoint for ArabicShaping {
87 fn codepoints(&self) -> CodepointIter {
88 self.codepoint.into_iter()
89 }
90}
91
92impl FromStr for ArabicShaping {
93 type Err = Error;
94
95 fn from_str(line: &str) -> Result<ArabicShaping, Error> {
96 static PARTS: Lazy<Regex> = Lazy::new(|| {
97 Regex::new(
98 r"(?x)
99 ^
100 \s*(?P<codepoint>[A-F0-9]+)\s*;
101 \s*(?P<name>[^;]+)\s*;
102 \s*(?P<joining_type>[^;]+)\s*;
103 \s*(?P<joining_group>[^;]+)
104 $
105 ",
106 )
107 .unwrap()
108 });
109 let caps = match PARTS.captures(line.trim()) {
110 Some(caps) => caps,
111 None => return err!("invalid ArabicShaping line"),
112 };
113
114 Ok(ArabicShaping {
115 codepoint: caps["codepoint"].parse()?,
116 schematic_name: caps["name"].to_string(),
117 joining_type: caps["joining_type"].parse()?,
118 joining_group: caps["joining_group"].to_string(),
119 })
120 }
121}
122
123#[cfg(test)]
124mod tests {
125 use crate::common::Codepoint;
126
127 use super::{ArabicShaping, JoiningType};
128
129 fn codepoint(n: u32) -> Codepoint {
130 Codepoint::from_u32(n).unwrap()
131 }
132
133 fn s(string: &str) -> String {
134 string.to_string()
135 }
136
137 #[test]
138 fn parse1() {
139 let line = "0600; ARABIC NUMBER SIGN; U; No_Joining_Group\n";
140 let data: ArabicShaping = line.parse().unwrap();
141 assert_eq!(
142 data,
143 ArabicShaping {
144 codepoint: codepoint(0x0600),
145 schematic_name: s("ARABIC NUMBER SIGN"),
146 joining_type: JoiningType::NonJoining,
147 joining_group: s("No_Joining_Group")
148 }
149 );
150 }
151
152 #[test]
153 fn parse2() {
154 let line = "063D; FARSI YEH WITH INVERTED V ABOVE; D; FARSI YEH\n";
155 let data: ArabicShaping = line.parse().unwrap();
156 assert_eq!(
157 data,
158 ArabicShaping {
159 codepoint: codepoint(0x063D),
160 schematic_name: s("FARSI YEH WITH INVERTED V ABOVE"),
161 joining_type: JoiningType::DualJoining,
162 joining_group: s("FARSI YEH")
163 }
164 );
165 }
166
167 #[test]
168 fn parse3() {
169 let line =
170 "10D23; HANIFI ROHINGYA DOTLESS KINNA YA WITH DOT ABOVE; D; HANIFI ROHINGYA KINNA YA\n";
171 let data: ArabicShaping = line.parse().unwrap();
172 assert_eq!(
173 data,
174 ArabicShaping {
175 codepoint: codepoint(0x10D23),
176 schematic_name: s(
177 "HANIFI ROHINGYA DOTLESS KINNA YA WITH DOT ABOVE"
178 ),
179 joining_type: JoiningType::DualJoining,
180 joining_group: s("HANIFI ROHINGYA KINNA YA")
181 }
182 );
183 }
184}
185