ucd_parse/extracted/
derived_general_category.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/DerivedGeneralCategory.txt` file.
12///
13/// This file gives the derived values of the General_Category property.
14#[derive(Clone, Debug, Default, Eq, PartialEq)]
15pub struct DerivedGeneralCategory {
16    /// The codepoint or codepoint range for this entry.
17    pub codepoints: Codepoints,
18    /// The derived General_Category of the codepoints in this entry.
19    pub general_category: String,
20}
21
22impl UcdFile for DerivedGeneralCategory {
23    fn relative_file_path() -> &'static Path {
24        Path::new("extracted/DerivedGeneralCategory.txt")
25    }
26}
27
28impl UcdFileByCodepoint for DerivedGeneralCategory {
29    fn codepoints(&self) -> CodepointIter {
30        self.codepoints.into_iter()
31    }
32}
33
34impl std::str::FromStr for DerivedGeneralCategory {
35    type Err = Error;
36
37    fn from_str(line: &str) -> Result<DerivedGeneralCategory, Error> {
38        let (codepoints, general_category) =
39            parse_codepoint_association(line)?;
40        Ok(DerivedGeneralCategory {
41            codepoints,
42            general_category: general_category.to_string(),
43        })
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::DerivedGeneralCategory;
50
51    #[test]
52    fn parse_single() {
53        let line = "04D9          ; Ll #       CYRILLIC SMALL LETTER SCHWA\n";
54        let row: DerivedGeneralCategory = line.parse().unwrap();
55        assert_eq!(row.codepoints, 0x04D9);
56        assert_eq!(row.general_category, "Ll");
57    }
58
59    #[test]
60    fn parse_range() {
61        let line =  "0660..0669    ; Nd #  [10] ARABIC-INDIC DIGIT ZERO..ARABIC-INDIC DIGIT NINE";
62        let row: DerivedGeneralCategory = line.parse().unwrap();
63        assert_eq!(row.codepoints, (0x0660, 0x0669));
64        assert_eq!(row.general_category, "Nd");
65    }
66}