ucd_parse/
core_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 `DerivedCoreProperties.txt` file.
12#[derive(Clone, Debug, Default, Eq, PartialEq)]
13pub struct CoreProperty {
14    /// The codepoint or codepoint range for this entry.
15    pub codepoints: Codepoints,
16    /// The property name assigned to the codepoints in this entry.
17    pub property: String,
18}
19
20impl UcdFile for CoreProperty {
21    fn relative_file_path() -> &'static Path {
22        Path::new("DerivedCoreProperties.txt")
23    }
24}
25
26impl UcdFileByCodepoint for CoreProperty {
27    fn codepoints(&self) -> CodepointIter {
28        self.codepoints.into_iter()
29    }
30}
31
32impl std::str::FromStr for CoreProperty {
33    type Err = Error;
34
35    fn from_str(line: &str) -> Result<CoreProperty, Error> {
36        let (codepoints, property) = parse_codepoint_association(line)?;
37        Ok(CoreProperty { codepoints, property: property.to_string() })
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::CoreProperty;
44
45    #[test]
46    fn parse_single() {
47        let line =
48            "1163D         ; Case_Ignorable # Mn       MODI SIGN ANUSVARA\n";
49        let row: CoreProperty = line.parse().unwrap();
50        assert_eq!(row.codepoints, 0x1163D);
51        assert_eq!(row.property, "Case_Ignorable");
52    }
53
54    #[test]
55    fn parse_range() {
56        let line = "11133..11134  ; Grapheme_Link # Mn   [2] CHAKMA VIRAMA..CHAKMA MAAYYAA\n";
57        let row: CoreProperty = line.parse().unwrap();
58        assert_eq!(row.codepoints, (0x11133, 0x11134));
59        assert_eq!(row.property, "Grapheme_Link");
60    }
61}