1use std::path::Path;
2
3use crate::{
4 common::{CodepointIter, Codepoints, UcdFile, UcdFileByCodepoint},
5 error::Error,
6};
7
8/// A single row in the `extracted/DerivedNumericValues.txt` file.
9///
10/// This file gives the derived values of the Numeric_Value property.
11#[derive(Clone, Debug, Default, Eq, PartialEq)]
12pub struct DerivedNumericValues {
13 /// The codepoint or codepoint range for this entry.
14 pub codepoints: Codepoints,
15 /// The approximate Numeric_Value of the codepoints in this entry,
16 /// as a decimal.
17 pub numeric_value_decimal: String,
18 /// The exact Numeric_Value of the codepoints in this entry, as
19 /// a fraction.
20 pub numeric_value_fraction: String,
21}
22
23impl UcdFile for DerivedNumericValues {
24 fn relative_file_path() -> &'static Path {
25 Path::new("extracted/DerivedNumericValues.txt")
26 }
27}
28
29impl UcdFileByCodepoint for DerivedNumericValues {
30 fn codepoints(&self) -> CodepointIter {
31 self.codepoints.into_iter()
32 }
33}
34
35impl std::str::FromStr for DerivedNumericValues {
36 type Err = Error;
37
38 fn from_str(line: &str) -> Result<DerivedNumericValues, Error> {
39 let re_parts = regex!(
40 r"(?x)
41 ^
42 \s*(?P<codepoints>[^\s;]+)\s*;
43 \s*(?P<numeric_value_decimal>[^\s;]+)\s*;
44 \s*;
45 \s*(?P<numeric_value_fraction>[^\s;]+)\s*
46 ",
47 );
48
49 let caps = match re_parts.captures(line.trim()) {
50 Some(caps) => caps,
51 None => return err!("invalid PropList line: '{}'", line),
52 };
53 let codepoints = caps["codepoints"].parse()?;
54 let numeric_value_decimal = caps["numeric_value_decimal"].to_string();
55 let numeric_value_fraction =
56 caps["numeric_value_fraction"].to_string();
57
58 Ok(DerivedNumericValues {
59 codepoints,
60 numeric_value_decimal,
61 numeric_value_fraction,
62 })
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::DerivedNumericValues;
69
70 #[test]
71 fn parse_single() {
72 let line = "0030 ; 0.0 ; ; 0 # Nd DIGIT ZERO\n";
73 let row: DerivedNumericValues = line.parse().unwrap();
74 assert_eq!(row.codepoints, 0x0030);
75 assert_eq!(row.numeric_value_decimal, "0.0");
76 assert_eq!(row.numeric_value_fraction, "0");
77 }
78
79 #[test]
80 fn parse_range() {
81 let line = "11FC9..11FCA ; 0.0625 ; ; 1/16 # No [2] TAMIL FRACTION ONE SIXTEENTH-1..TAMIL FRACTION ONE SIXTEENTH-2\n";
82 let row: DerivedNumericValues = line.parse().unwrap();
83 assert_eq!(row.codepoints, (0x11FC9, 0x11FCA));
84 assert_eq!(row.numeric_value_decimal, "0.0625");
85 assert_eq!(row.numeric_value_fraction, "1/16");
86 }
87}
88