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