1use std::path::Path;
2use std::str::FromStr;
3
4use crate::common::{
5 parse_codepoint_association, CodepointIter, Codepoints, UcdFile,
6 UcdFileByCodepoint,
7};
8use crate::error::Error;
9
10/// A single row in the `extracted/DerivedBinaryProperties.txt` file.
11///
12/// This file indicates whether a codepoint has the Bidi_Mirrored property.
13#[derive(Clone, Debug, Default, Eq, PartialEq)]
14pub struct DerivedBinaryProperties {
15 /// The codepoint or codepoint range for this entry.
16 pub codepoints: Codepoints,
17 /// The derived property of the codepoints in this entry. Currently,
18 /// this is always the always the string "Bidi_Mirrored".
19 pub property: String,
20}
21
22impl UcdFile for DerivedBinaryProperties {
23 fn relative_file_path() -> &'static Path {
24 Path::new("extracted/DerivedBinaryProperties.txt")
25 }
26}
27
28impl UcdFileByCodepoint for DerivedBinaryProperties {
29 fn codepoints(&self) -> CodepointIter {
30 self.codepoints.into_iter()
31 }
32}
33
34impl FromStr for DerivedBinaryProperties {
35 type Err = Error;
36
37 fn from_str(line: &str) -> Result<DerivedBinaryProperties, Error> {
38 let (codepoints: Codepoints, property: &str) = parse_codepoint_association(line)?;
39 Ok(DerivedBinaryProperties {
40 codepoints,
41 property: property.to_string(),
42 })
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::DerivedBinaryProperties;
49
50 #[test]
51 fn parse_single() {
52 let line =
53 "0028 ; Bidi_Mirrored # Ps LEFT PARENTHESIS\n";
54 let row: DerivedBinaryProperties = line.parse().unwrap();
55 assert_eq!(row.codepoints, 0x0028);
56 assert_eq!(row.property, "Bidi_Mirrored");
57 }
58
59 #[test]
60 fn parse_range() {
61 let line = "2A3C..2A3E ; Bidi_Mirrored # Sm [3] INTERIOR PRODUCT..Z NOTATION RELATIONAL COMPOSITION\n";
62 let row: DerivedBinaryProperties = line.parse().unwrap();
63 assert_eq!(row.codepoints, (0x2A3C, 0x2A3E));
64 assert_eq!(row.property, "Bidi_Mirrored");
65 }
66}
67