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/GraphemeBreakProperty.txt` file.
11#[derive(Clone, Debug, Default, Eq, PartialEq)]
12pub struct GraphemeClusterBreak {
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 GraphemeClusterBreak {
20 fn relative_file_path() -> &'static Path {
21 Path::new("auxiliary/GraphemeBreakProperty.txt")
22 }
23}
24
25impl UcdFileByCodepoint for GraphemeClusterBreak {
26 fn codepoints(&self) -> CodepointIter {
27 self.codepoints.into_iter()
28 }
29}
30
31impl FromStr for GraphemeClusterBreak {
32 type Err = Error;
33
34 fn from_str(line: &str) -> Result<GraphemeClusterBreak, Error> {
35 let (codepoints: Codepoints, value: &str) = parse_codepoint_association(line)?;
36 Ok(GraphemeClusterBreak { codepoints, value: value.to_string() })
37 }
38}
39
40/// A single row in the `auxiliary/GraphemeBreakTest.txt` file.
41///
42/// This file defines tests for the grapheme cluster break algorithm.
43#[derive(Clone, Debug, Default, Eq, PartialEq)]
44pub struct GraphemeClusterBreakTest {
45 /// Each string is a UTF-8 encoded group of codepoints that make up a
46 /// single grapheme cluster.
47 pub grapheme_clusters: Vec<String>,
48 /// A human readable description of this test.
49 pub comment: String,
50}
51
52impl UcdFile for GraphemeClusterBreakTest {
53 fn relative_file_path() -> &'static Path {
54 Path::new("auxiliary/GraphemeBreakTest.txt")
55 }
56}
57
58impl FromStr for GraphemeClusterBreakTest {
59 type Err = Error;
60
61 fn from_str(line: &str) -> Result<GraphemeClusterBreakTest, Error> {
62 let (groups: Vec, comment: String) = parse_break_test(line)?;
63 Ok(GraphemeClusterBreakTest { grapheme_clusters: groups, comment })
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use super::{GraphemeClusterBreak, GraphemeClusterBreakTest};
70
71 #[test]
72 fn parse_single() {
73 let line = "093B ; SpacingMark # Mc DEVANAGARI VOWEL SIGN OOE\n";
74 let row: GraphemeClusterBreak = line.parse().unwrap();
75 assert_eq!(row.codepoints, 0x093B);
76 assert_eq!(row.value, "SpacingMark");
77 }
78
79 #[test]
80 fn parse_range() {
81 let line = "1F1E6..1F1FF ; Regional_Indicator # So [26] REGIONAL INDICATOR SYMBOL LETTER A..REGIONAL INDICATOR SYMBOL LETTER Z\n";
82 let row: GraphemeClusterBreak = line.parse().unwrap();
83 assert_eq!(row.codepoints, (0x1F1E6, 0x1F1FF));
84 assert_eq!(row.value, "Regional_Indicator");
85 }
86
87 #[test]
88 fn parse_test() {
89 let line = "÷ 0061 × 1F3FF ÷ 1F476 × 200D × 1F6D1 ÷ # ÷ [0.2] LATIN SMALL LETTER A (Other) × [9.0] EMOJI MODIFIER FITZPATRICK TYPE-6 (Extend) ÷ [999.0] BABY (ExtPict) × [9.0] ZERO WIDTH JOINER (ZWJ_ExtCccZwj) × [11.0] OCTAGONAL SIGN (ExtPict) ÷ [0.3]\n";
90
91 let row: GraphemeClusterBreakTest = line.parse().unwrap();
92 assert_eq!(
93 row.grapheme_clusters,
94 vec!["\u{0061}\u{1F3FF}", "\u{1F476}\u{200D}\u{1F6D1}",]
95 );
96 assert!(row.comment.starts_with("÷ [0.2] LATIN SMALL LETTER A"));
97 }
98}
99