1use std::path::Path;
2use std::str::FromStr;
3
4use crate::common::{
5 parse_break_test, parse_codepoint_association, CodepointIter, Codepoints,
6 UcdFile, UcdFileByCodepoint,
7};
8use crate::error::Error;
9
10/// A single row in the `auxiliary/WordBreakProperty.txt` file.
11#[derive(Clone, Debug, Default, Eq, PartialEq)]
12pub 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
19impl UcdFile for WordBreak {
20 fn relative_file_path() -> &'static Path {
21 Path::new("auxiliary/WordBreakProperty.txt")
22 }
23}
24
25impl UcdFileByCodepoint for WordBreak {
26 fn codepoints(&self) -> CodepointIter {
27 self.codepoints.into_iter()
28 }
29}
30
31impl FromStr for WordBreak {
32 type Err = Error;
33
34 fn from_str(line: &str) -> Result<WordBreak, Error> {
35 let (codepoints: Codepoints, value: &str) = parse_codepoint_association(line)?;
36 Ok(WordBreak { codepoints, value: value.to_string() })
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)]
44pub 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
52impl UcdFile for WordBreakTest {
53 fn relative_file_path() -> &'static Path {
54 Path::new("auxiliary/WordBreakTest.txt")
55 }
56}
57
58impl FromStr for WordBreakTest {
59 type Err = Error;
60
61 fn from_str(line: &str) -> Result<WordBreakTest, Error> {
62 let (groups: Vec, comment: String) = parse_break_test(line)?;
63 Ok(WordBreakTest { words: groups, comment })
64 }
65}
66
67#[cfg(test)]
68mod 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();
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 );
101 assert!(row.comment.contains("[4.0] COMBINING DIAERESIS (Extend_FE)"));
102 }
103}
104