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