1 | use std::path::Path; |
2 | use std::str::FromStr; |
3 | |
4 | use crate::common::{ |
5 | parse_codepoint_association, CodepointIter, Codepoints, UcdFile, |
6 | UcdFileByCodepoint, |
7 | }; |
8 | use crate::error::Error; |
9 | |
10 | /// A single row in the `Scripts.txt` file. |
11 | #[derive (Clone, Debug, Default, Eq, PartialEq)] |
12 | pub struct Script { |
13 | /// The codepoint or codepoint range for this entry. |
14 | pub codepoints: Codepoints, |
15 | /// The script name assigned to the codepoints in this entry. |
16 | pub script: String, |
17 | } |
18 | |
19 | impl UcdFile for Script { |
20 | fn relative_file_path() -> &'static Path { |
21 | Path::new("Scripts.txt" ) |
22 | } |
23 | } |
24 | |
25 | impl UcdFileByCodepoint for Script { |
26 | fn codepoints(&self) -> CodepointIter { |
27 | self.codepoints.into_iter() |
28 | } |
29 | } |
30 | |
31 | impl FromStr for Script { |
32 | type Err = Error; |
33 | |
34 | fn from_str(line: &str) -> Result<Script, Error> { |
35 | let (codepoints: Codepoints, script: &str) = parse_codepoint_association(line)?; |
36 | Ok(Script { codepoints, script: script.to_string() }) |
37 | } |
38 | } |
39 | |
40 | #[cfg (test)] |
41 | mod tests { |
42 | use super::Script; |
43 | |
44 | #[test ] |
45 | fn parse_single() { |
46 | let line = "10A7F ; Old_South_Arabian # Po OLD SOUTH ARABIAN NUMERIC INDICATOR \n" ; |
47 | let row: Script = line.parse().unwrap(); |
48 | assert_eq!(row.codepoints, 0x10A7F); |
49 | assert_eq!(row.script, "Old_South_Arabian" ); |
50 | } |
51 | |
52 | #[test ] |
53 | fn parse_range() { |
54 | let line = "1200..1248 ; Ethiopic # Lo [73] ETHIOPIC SYLLABLE HA..ETHIOPIC SYLLABLE QWA \n" ; |
55 | let row: Script = line.parse().unwrap(); |
56 | assert_eq!(row.codepoints, (0x1200, 0x1248)); |
57 | assert_eq!(row.script, "Ethiopic" ); |
58 | } |
59 | } |
60 | |