ucd_parse/
scripts.rs

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