ucd_parse/extracted/
derived_binary_properties.rs

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