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 `PropList.txt` file.
12///
13/// The `PropList.txt` file is the source of truth on several Unicode
14/// properties.
15#[derive(Clone, Debug, Default, Eq, PartialEq)]
16pub struct Property {
17 /// The codepoint or codepoint range for this entry.
18 pub codepoints: Codepoints,
19 /// The property name assigned to the codepoints in this entry.
20 pub property: String,
21}
22
23impl UcdFile for Property {
24 fn relative_file_path() -> &'static Path {
25 Path::new("PropList.txt")
26 }
27}
28
29impl UcdFileByCodepoint for Property {
30 fn codepoints(&self) -> CodepointIter {
31 self.codepoints.into_iter()
32 }
33}
34
35impl std::str::FromStr for Property {
36 type Err = Error;
37
38 fn from_str(line: &str) -> Result<Property, Error> {
39 let (codepoints: Codepoints, property: &str) = parse_codepoint_association(line)?;
40 Ok(Property { codepoints, property: property.to_string() })
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::Property;
47
48 #[test]
49 fn parse_single() {
50 let line =
51 "061C ; Bidi_Control # Cf ARABIC LETTER MARK\n";
52 let row: Property = line.parse().unwrap();
53 assert_eq!(row.codepoints, 0x061C);
54 assert_eq!(row.property, "Bidi_Control");
55 }
56
57 #[test]
58 fn parse_range() {
59 let line = "0009..000D ; White_Space # Cc [5] <control-0009>..<control-000D>\n";
60 let row: Property = line.parse().unwrap();
61 assert_eq!(row.codepoints, (0x0009, 0x000D));
62 assert_eq!(row.property, "White_Space");
63 }
64}
65