ucd_parse/extracted/
derived_name.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/DerivedName.txt` file.
12///
13/// This file gives the derived values of the Name property.
14#[derive(Clone, Debug, Default, Eq, PartialEq)]
15pub struct DerivedName {
16    /// The codepoint or codepoint range for this entry.
17    pub codepoints: Codepoints,
18    /// The derived Name of the codepoints in this entry.
19    pub name: String,
20}
21
22impl UcdFile for DerivedName {
23    fn relative_file_path() -> &'static Path {
24        Path::new("extracted/DerivedName.txt")
25    }
26}
27
28impl UcdFileByCodepoint for DerivedName {
29    fn codepoints(&self) -> CodepointIter {
30        self.codepoints.into_iter()
31    }
32}
33
34impl std::str::FromStr for DerivedName {
35    type Err = Error;
36
37    fn from_str(line: &str) -> Result<DerivedName, Error> {
38        let (codepoints, name) = parse_codepoint_association(line)?;
39        Ok(DerivedName { codepoints, name: name.to_string() })
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::DerivedName;
46
47    #[test]
48    fn parse_single() {
49        let line = "0021          ; EXCLAMATION MARK\n";
50        let row: DerivedName = line.parse().unwrap();
51        assert_eq!(row.codepoints, 0x0021);
52        assert_eq!(row.name, "EXCLAMATION MARK");
53    }
54
55    #[test]
56    fn parse_range() {
57        let line = "3400..4DBF    ; CJK UNIFIED IDEOGRAPH-*\n";
58        let row: DerivedName = line.parse().unwrap();
59        assert_eq!(row.codepoints, (0x3400, 0x4DBF));
60        assert_eq!(row.name, "CJK UNIFIED IDEOGRAPH-*");
61    }
62}