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 `DerivedCoreProperties.txt` file. |
11 | #[derive (Clone, Debug, Default, Eq, PartialEq)] |
12 | pub struct CoreProperty { |
13 | /// The codepoint or codepoint range for this entry. |
14 | pub codepoints: Codepoints, |
15 | /// The property name assigned to the codepoints in this entry. |
16 | pub property: String, |
17 | } |
18 | |
19 | impl UcdFile for CoreProperty { |
20 | fn relative_file_path() -> &'static Path { |
21 | Path::new("DerivedCoreProperties.txt" ) |
22 | } |
23 | } |
24 | |
25 | impl UcdFileByCodepoint for CoreProperty { |
26 | fn codepoints(&self) -> CodepointIter { |
27 | self.codepoints.into_iter() |
28 | } |
29 | } |
30 | |
31 | impl FromStr for CoreProperty { |
32 | type Err = Error; |
33 | |
34 | fn from_str(line: &str) -> Result<CoreProperty, Error> { |
35 | let (codepoints: Codepoints, property: &str) = parse_codepoint_association(line)?; |
36 | Ok(CoreProperty { codepoints, property: property.to_string() }) |
37 | } |
38 | } |
39 | |
40 | #[cfg (test)] |
41 | mod tests { |
42 | use super::CoreProperty; |
43 | |
44 | #[test ] |
45 | fn parse_single() { |
46 | let line = |
47 | "1163D ; Case_Ignorable # Mn MODI SIGN ANUSVARA \n" ; |
48 | let row: CoreProperty = line.parse().unwrap(); |
49 | assert_eq!(row.codepoints, 0x1163D); |
50 | assert_eq!(row.property, "Case_Ignorable" ); |
51 | } |
52 | |
53 | #[test ] |
54 | fn parse_range() { |
55 | let line = "11133..11134 ; Grapheme_Link # Mn [2] CHAKMA VIRAMA..CHAKMA MAAYYAA \n" ; |
56 | let row: CoreProperty = line.parse().unwrap(); |
57 | assert_eq!(row.codepoints, (0x11133, 0x11134)); |
58 | assert_eq!(row.property, "Grapheme_Link" ); |
59 | } |
60 | } |
61 | |