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/DerivedLineBreak.txt` file. |
11 | /// |
12 | /// This file gives the derived values of the Line_Break property. |
13 | #[derive (Clone, Debug, Default, Eq, PartialEq)] |
14 | pub struct DerivedLineBreak { |
15 | /// The codepoint or codepoint range for this entry. |
16 | pub codepoints: Codepoints, |
17 | /// The derived Line_Break of the codepoints in this entry. |
18 | pub line_break: String, |
19 | } |
20 | |
21 | impl UcdFile for DerivedLineBreak { |
22 | fn relative_file_path() -> &'static Path { |
23 | Path::new("extracted/DerivedLineBreak.txt" ) |
24 | } |
25 | } |
26 | |
27 | impl UcdFileByCodepoint for DerivedLineBreak { |
28 | fn codepoints(&self) -> CodepointIter { |
29 | self.codepoints.into_iter() |
30 | } |
31 | } |
32 | |
33 | impl FromStr for DerivedLineBreak { |
34 | type Err = Error; |
35 | |
36 | fn from_str(line: &str) -> Result<DerivedLineBreak, Error> { |
37 | let (codepoints: Codepoints, line_break: &str) = parse_codepoint_association(line)?; |
38 | Ok(DerivedLineBreak { codepoints, line_break: line_break.to_string() }) |
39 | } |
40 | } |
41 | |
42 | #[cfg (test)] |
43 | mod tests { |
44 | use super::DerivedLineBreak; |
45 | |
46 | #[test ] |
47 | fn parse_single() { |
48 | let line = "0028 ; OP # Ps LEFT PARENTHESIS \n" ; |
49 | let row: DerivedLineBreak = line.parse().unwrap(); |
50 | assert_eq!(row.codepoints, 0x0028); |
51 | assert_eq!(row.line_break, "OP" ); |
52 | } |
53 | |
54 | #[test ] |
55 | fn parse_range() { |
56 | let line = "0030..0039 ; NU # Nd [10] DIGIT ZERO..DIGIT NINE \n" ; |
57 | let row: DerivedLineBreak = line.parse().unwrap(); |
58 | assert_eq!(row.codepoints, (0x0030, 0x0039)); |
59 | assert_eq!(row.line_break, "NU" ); |
60 | } |
61 | } |
62 | |