ucd_parse/
script_extensions.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 `ScriptExtensions.txt` file.
12#[derive(Clone, Debug, Default, Eq, PartialEq)]
13pub struct ScriptExtension {
14    /// The codepoint or codepoint range for this entry.
15    pub codepoints: Codepoints,
16    /// The script extension names assigned to the codepoints in this entry.
17    pub scripts: Vec<String>,
18}
19
20impl UcdFile for ScriptExtension {
21    fn relative_file_path() -> &'static Path {
22        Path::new("ScriptExtensions.txt")
23    }
24}
25
26impl UcdFileByCodepoint for ScriptExtension {
27    fn codepoints(&self) -> CodepointIter {
28        self.codepoints.into_iter()
29    }
30}
31
32impl std::str::FromStr for ScriptExtension {
33    type Err = Error;
34
35    fn from_str(line: &str) -> Result<ScriptExtension, Error> {
36        let (codepoints, scripts) = parse_codepoint_association(line)?;
37        Ok(ScriptExtension {
38            codepoints,
39            scripts: scripts.split_whitespace().map(str::to_string).collect(),
40        })
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::ScriptExtension;
47
48    #[test]
49    fn parse_single() {
50        let line = "060C          ; Arab Syrc Thaa # Po       ARABIC COMMA\n";
51        let row: ScriptExtension = line.parse().unwrap();
52        assert_eq!(row.codepoints, 0x060C);
53        assert_eq!(row.scripts, vec!["Arab", "Syrc", "Thaa"]);
54    }
55
56    #[test]
57    fn parse_range() {
58        let line = "A836..A837    ; Deva Gujr Guru Kthi Mahj Modi Sind Takr Tirh # So   [2] NORTH INDIC QUARTER MARK..NORTH INDIC PLACEHOLDER MARK\n";
59        let row: ScriptExtension = line.parse().unwrap();
60        assert_eq!(row.codepoints, (0xA836, 0xA837));
61        assert_eq!(
62            row.scripts,
63            vec![
64                "Deva", "Gujr", "Guru", "Kthi", "Mahj", "Modi", "Sind",
65                "Takr", "Tirh",
66            ]
67        );
68    }
69}