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