1use std::path::Path;
2
3use crate::{
4 common::{
5 parse_codepoint_association, CodepointIter, Codepoints, UcdFile,
6 UcdFileByCodepoint,
7 },
8 error::Error,
9};
10
11/// A single row in the `extracted/DerivedBidiClass.txt` file.
12///
13/// This file gives the derived values of the Bidi_Class property.
14#[derive(Clone, Debug, Default, Eq, PartialEq)]
15pub struct DerivedBidiClass {
16 /// The codepoint or codepoint range for this entry.
17 pub codepoints: Codepoints,
18 /// The derived Bidi_Class of the codepoints in this entry.
19 pub bidi_class: String,
20}
21
22impl UcdFile for DerivedBidiClass {
23 fn relative_file_path() -> &'static Path {
24 Path::new("extracted/DerivedBidiClass.txt")
25 }
26}
27
28impl UcdFileByCodepoint for DerivedBidiClass {
29 fn codepoints(&self) -> CodepointIter {
30 self.codepoints.into_iter()
31 }
32}
33
34impl std::str::FromStr for DerivedBidiClass {
35 type Err = Error;
36
37 fn from_str(line: &str) -> Result<DerivedBidiClass, Error> {
38 let (codepoints: Codepoints, bidi_class: &str) = parse_codepoint_association(line)?;
39 Ok(DerivedBidiClass { codepoints, bidi_class: bidi_class.to_string() })
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::DerivedBidiClass;
46
47 #[test]
48 fn parse_single() {
49 let line = "00B5 ; L # L& MICRO SIGN\n";
50 let row: DerivedBidiClass = line.parse().unwrap();
51 assert_eq!(row.codepoints, 0x00B5);
52 assert_eq!(row.bidi_class, "L");
53 }
54
55 #[test]
56 fn parse_range() {
57 let line = "0030..0039 ; EN # Nd [10] DIGIT ZERO..DIGIT NINE\n";
58 let row: DerivedBidiClass = line.parse().unwrap();
59 assert_eq!(row.codepoints, (0x0030, 0x0039));
60 assert_eq!(row.bidi_class, "EN");
61 }
62}
63