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 `DerivedAge.txt` file.
11#[derive(Clone, Debug, Default, Eq, PartialEq)]
12pub struct Age {
13 /// The codepoint or codepoint range for this entry.
14 pub codepoints: Codepoints,
15 /// The age assigned to the codepoints in this entry.
16 pub age: String,
17}
18
19impl UcdFile for Age {
20 fn relative_file_path() -> &'static Path {
21 Path::new("DerivedAge.txt")
22 }
23}
24
25impl UcdFileByCodepoint for Age {
26 fn codepoints(&self) -> CodepointIter {
27 self.codepoints.into_iter()
28 }
29}
30
31impl FromStr for Age {
32 type Err = Error;
33
34 fn from_str(line: &str) -> Result<Age, Error> {
35 let (codepoints: Codepoints, script: &str) = parse_codepoint_association(line)?;
36 Ok(Age { codepoints, age: script.to_string() })
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use super::Age;
43
44 #[test]
45 fn parse_single() {
46 let line = "2BD2 ; 10.0 # GROUP MARK\n";
47 let row: Age = line.parse().unwrap();
48 assert_eq!(row.codepoints, 0x2BD2);
49 assert_eq!(row.age, "10.0");
50 }
51
52 #[test]
53 fn parse_range() {
54 let line = "11D0B..11D36 ; 10.0 # [44] MASARAM GONDI LETTER AU..MASARAM GONDI VOWEL SIGN VOCALIC R\n";
55 let row: Age = line.parse().unwrap();
56 assert_eq!(row.codepoints, (0x11D0B, 0x11D36));
57 assert_eq!(row.age, "10.0");
58 }
59}
60