]>
Commit | Line | Data |
---|---|---|
dfeec247 XL |
1 | use std::path::Path; |
2 | use std::str::FromStr; | |
3 | ||
3dfed10e XL |
4 | use crate::common::{ |
5 | parse_break_test, parse_codepoint_association, CodepointIter, Codepoints, | |
6 | UcdFile, UcdFileByCodepoint, | |
dfeec247 | 7 | }; |
3dfed10e | 8 | use crate::error::Error; |
dfeec247 XL |
9 | |
10 | /// A single row in the `auxiliary/WordBreakProperty.txt` file. | |
11 | #[derive(Clone, Debug, Default, Eq, PartialEq)] | |
12 | pub struct WordBreak { | |
13 | /// The codepoint or codepoint range for this entry. | |
14 | pub codepoints: Codepoints, | |
15 | /// The property value assigned to the codepoints in this entry. | |
16 | pub value: String, | |
17 | } | |
18 | ||
19 | impl UcdFile for WordBreak { | |
20 | fn relative_file_path() -> &'static Path { | |
21 | Path::new("auxiliary/WordBreakProperty.txt") | |
22 | } | |
23 | } | |
24 | ||
25 | impl UcdFileByCodepoint for WordBreak { | |
26 | fn codepoints(&self) -> CodepointIter { | |
27 | self.codepoints.into_iter() | |
28 | } | |
29 | } | |
30 | ||
31 | impl FromStr for WordBreak { | |
32 | type Err = Error; | |
33 | ||
34 | fn from_str(line: &str) -> Result<WordBreak, Error> { | |
35 | let (codepoints, value) = parse_codepoint_association(line)?; | |
3dfed10e | 36 | Ok(WordBreak { codepoints, value: value.to_string() }) |
dfeec247 XL |
37 | } |
38 | } | |
39 | ||
40 | /// A single row in the `auxiliary/WordBreakTest.txt` file. | |
41 | /// | |
42 | /// This file defines tests for the word break algorithm. | |
43 | #[derive(Clone, Debug, Default, Eq, PartialEq)] | |
44 | pub struct WordBreakTest { | |
45 | /// Each string is a UTF-8 encoded group of codepoints that make up a | |
46 | /// single word. | |
47 | pub words: Vec<String>, | |
48 | /// A human readable description of this test. | |
49 | pub comment: String, | |
50 | } | |
51 | ||
52 | impl UcdFile for WordBreakTest { | |
53 | fn relative_file_path() -> &'static Path { | |
54 | Path::new("auxiliary/WordBreakTest.txt") | |
55 | } | |
56 | } | |
57 | ||
58 | impl FromStr for WordBreakTest { | |
59 | type Err = Error; | |
60 | ||
61 | fn from_str(line: &str) -> Result<WordBreakTest, Error> { | |
62 | let (groups, comment) = parse_break_test(line)?; | |
3dfed10e | 63 | Ok(WordBreakTest { words: groups, comment }) |
dfeec247 XL |
64 | } |
65 | } | |
66 | ||
67 | #[cfg(test)] | |
68 | mod tests { | |
69 | use super::{WordBreak, WordBreakTest}; | |
70 | ||
71 | #[test] | |
72 | fn parse_single() { | |
73 | let line = "0A83 ; Extend # Mc GUJARATI SIGN VISARGA\n"; | |
74 | let row: WordBreak = line.parse().unwrap(); | |
75 | assert_eq!(row.codepoints, 0x0A83); | |
76 | assert_eq!(row.value, "Extend"); | |
77 | } | |
78 | ||
79 | #[test] | |
80 | fn parse_range() { | |
81 | let line = "104A0..104A9 ; Numeric # Nd [10] OSMANYA DIGIT ZERO..OSMANYA DIGIT NINE\n"; | |
82 | let row: WordBreak = line.parse().unwrap(); | |
83 | assert_eq!(row.codepoints, (0x104A0, 0x104A9)); | |
84 | assert_eq!(row.value, "Numeric"); | |
85 | } | |
86 | ||
87 | #[test] | |
88 | fn parse_test() { | |
89 | let line = "÷ 0031 ÷ 0027 × 0308 ÷ 0061 ÷ 0027 × 2060 ÷ # ÷ [0.2] DIGIT ONE (Numeric) ÷ [999.0] APOSTROPHE (Single_Quote) × [4.0] COMBINING DIAERESIS (Extend_FE) ÷ [999.0] LATIN SMALL LETTER A (ALetter) ÷ [999.0] APOSTROPHE (Single_Quote) × [4.0] WORD JOINER (Format_FE) ÷ [0.3]"; | |
90 | ||
91 | let row: WordBreakTest = line.parse().unwrap(); | |
3dfed10e XL |
92 | assert_eq!( |
93 | row.words, | |
94 | vec![ | |
95 | "\u{0031}", | |
96 | "\u{0027}\u{0308}", | |
97 | "\u{0061}", | |
98 | "\u{0027}\u{2060}", | |
99 | ] | |
100 | ); | |
dfeec247 XL |
101 | assert!(row.comment.contains("[4.0] COMBINING DIAERESIS (Extend_FE)")); |
102 | } | |
103 | } |