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