ucd_parse/extracted/
derived_numeric_type.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/DerivedNumericType.txt` file.
12///
13/// This file gives the derived values of the Numeric_Type property.
14#[derive(Clone, Debug, Default, Eq, PartialEq)]
15pub struct DerivedNumericType {
16    /// The codepoint or codepoint range for this entry.
17    pub codepoints: Codepoints,
18    /// The derived Numeric_Type of the codepoints in this entry.
19    pub numeric_type: String,
20}
21
22impl UcdFile for DerivedNumericType {
23    fn relative_file_path() -> &'static Path {
24        Path::new("extracted/DerivedNumericType.txt")
25    }
26}
27
28impl UcdFileByCodepoint for DerivedNumericType {
29    fn codepoints(&self) -> CodepointIter {
30        self.codepoints.into_iter()
31    }
32}
33
34impl std::str::FromStr for DerivedNumericType {
35    type Err = Error;
36
37    fn from_str(line: &str) -> Result<DerivedNumericType, Error> {
38        let (codepoints, numeric_type) = parse_codepoint_association(line)?;
39        Ok(DerivedNumericType {
40            codepoints,
41            numeric_type: numeric_type.to_string(),
42        })
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::DerivedNumericType;
49
50    #[test]
51    fn parse_single() {
52        let line =
53            "2189          ; Numeric # No       VULGAR FRACTION ZERO THIRDS\n";
54        let row: DerivedNumericType = line.parse().unwrap();
55        assert_eq!(row.codepoints, 0x2189);
56        assert_eq!(row.numeric_type, "Numeric");
57    }
58
59    #[test]
60    fn parse_range() {
61        let line =  "00B2..00B3    ; Digit # No   [2] SUPERSCRIPT TWO..SUPERSCRIPT THREE\n";
62        let row: DerivedNumericType = line.parse().unwrap();
63        assert_eq!(row.codepoints, (0x00B2, 0x00B3));
64        assert_eq!(row.numeric_type, "Digit");
65    }
66}