ucd_parse/extracted/
derived_east_asian_width.rs

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