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/DerivedCombiningClass.txt` file. |
11 | /// |
12 | /// This file gives the derived values of the Decomposition_Type |
13 | /// property. |
14 | #[derive (Clone, Debug, Default, Eq, PartialEq)] |
15 | pub struct DerivedDecompositionType { |
16 | /// The codepoint or codepoint range for this entry. |
17 | pub codepoints: Codepoints, |
18 | /// The derived Decomposition_Type of the codepoints in this entry. |
19 | pub decomposition_type: String, |
20 | } |
21 | |
22 | impl UcdFile for DerivedDecompositionType { |
23 | fn relative_file_path() -> &'static Path { |
24 | Path::new("extracted/DerivedDecompositionType.txt" ) |
25 | } |
26 | } |
27 | |
28 | impl UcdFileByCodepoint for DerivedDecompositionType { |
29 | fn codepoints(&self) -> CodepointIter { |
30 | self.codepoints.into_iter() |
31 | } |
32 | } |
33 | |
34 | impl FromStr for DerivedDecompositionType { |
35 | type Err = Error; |
36 | |
37 | fn from_str(line: &str) -> Result<DerivedDecompositionType, Error> { |
38 | let (codepoints: Codepoints, decomposition_type: &str) = |
39 | parse_codepoint_association(line)?; |
40 | Ok(DerivedDecompositionType { |
41 | codepoints, |
42 | decomposition_type: decomposition_type.to_string(), |
43 | }) |
44 | } |
45 | } |
46 | |
47 | #[cfg (test)] |
48 | mod tests { |
49 | use super::DerivedDecompositionType; |
50 | |
51 | #[test ] |
52 | fn parse_single() { |
53 | let line = "00A0 ; Nobreak # Zs NO-BREAK SPACE \n" ; |
54 | let row: DerivedDecompositionType = line.parse().unwrap(); |
55 | assert_eq!(row.codepoints, 0x00A0); |
56 | assert_eq!(row.decomposition_type, "Nobreak" ); |
57 | } |
58 | |
59 | #[test ] |
60 | fn parse_range() { |
61 | let line = "3070..3071 ; Canonical # Lo [2] HIRAGANA LETTER BA..HIRAGANA LETTER PA \n" ; |
62 | let row: DerivedDecompositionType = line.parse().unwrap(); |
63 | assert_eq!(row.codepoints, (0x3070, 0x3071)); |
64 | assert_eq!(row.decomposition_type, "Canonical" ); |
65 | } |
66 | } |
67 | |