1use std::path::Path;
2use std::str::FromStr;
3
4use crate::common::{
5 parse_codepoint_association, CodepointIter, Codepoints, UcdFile,
6 UcdFileByCodepoint,
7};
8use crate::error::Error;
9
10/// A single row in the `extracted/DerivedEastAsianWidth.txt` file.
11///
12/// This file gives the derived values of the East_Asian_Width
13/// property.
14#[derive(Clone, Debug, Default, Eq, PartialEq)]
15pub struct DerivedEastAsianWidth {
16 /// The codepoint or codepoint range for this entry.
17 pub codepoints: Codepoints,
18 /// The derived East_Asian_Width of the codepoints in this entry.
19 pub east_asian_width: String,
20}
21
22impl UcdFile for DerivedEastAsianWidth {
23 fn relative_file_path() -> &'static Path {
24 Path::new("extracted/DerivedEastAsianWidth.txt")
25 }
26}
27
28impl UcdFileByCodepoint for DerivedEastAsianWidth {
29 fn codepoints(&self) -> CodepointIter {
30 self.codepoints.into_iter()
31 }
32}
33
34impl FromStr for DerivedEastAsianWidth {
35 type Err = Error;
36
37 fn from_str(line: &str) -> Result<DerivedEastAsianWidth, Error> {
38 let (codepoints: Codepoints, east_asian_width: &str) =
39 parse_codepoint_association(line)?;
40 Ok(DerivedEastAsianWidth {
41 codepoints,
42 east_asian_width: east_asian_width.to_string(),
43 })
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::DerivedEastAsianWidth;
50
51 #[test]
52 fn parse_single() {
53 let line = "00A0 ; N # Zs NO-BREAK SPACE\n";
54 let row: DerivedEastAsianWidth = line.parse().unwrap();
55 assert_eq!(row.codepoints, 0x00A0);
56 assert_eq!(row.east_asian_width, "N");
57 }
58
59 #[test]
60 fn parse_range() {
61 let line = "FF10..FF19 ; F # Nd [10] FULLWIDTH DIGIT ZERO..FULLWIDTH DIGIT NINE\n";
62 let row: DerivedEastAsianWidth = line.parse().unwrap();
63 assert_eq!(row.codepoints, (0xFF10, 0xFF19));
64 assert_eq!(row.east_asian_width, "F");
65 }
66}
67