1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use crate::file_writer;
use crate::ucd_parsers;
use crate::Error;
use crate::{CodeGen, UcdLineParser};
use std::fs::File;
use std::io::Write;
use ucd_parse::{CodepointRange, Codepoints};

/// Generates a table of tuples (`Codepoints`, `BidiClass`) representing
/// the values of the Unicode character property
/// [`Bidi_Class`](http://www.unicode.org/reports/tr44/#Bidi_Class).
/// Possible values are listed in
/// [`UAX44`, Table 13](http://www.unicode.org/reports/tr44/#Bidi_Class_Values).
pub struct BidiClassGen {
    table_name: String,
    vec: Vec<(Codepoints, String)>,
}

impl BidiClassGen {
    /// Creates a new [`BidiClassGen`]
    pub fn new(table_name: &str) -> Self {
        Self {
            table_name: String::from(table_name),
            vec: Vec::new(),
        }
    }
}

impl CodeGen for BidiClassGen {
    fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
        generate_bidi_class_enum(file)?;
        self.generate_bidi_class_table(file)
    }
}

impl UcdLineParser<ucd_parsers::UnicodeData> for BidiClassGen {
    fn process_entry(&mut self, udata: &ucd_parsers::UnicodeData) -> Result<(), Error> {
        self.vec.push((udata.codepoints, udata.bidi_class.clone()));
        Ok(())
    }
}

fn add_range(range: &CodepointRange, bidi: &str, vec: &mut Vec<(Codepoints, String)>) {
    if range.start.value() == range.end.value() {
        vec.push((Codepoints::Single(range.start), String::from(bidi)));
    } else {
        vec.push((Codepoints::Range(*range), String::from(bidi)));
    }
}

fn generate_bidi_class_enum(file: &mut File) -> Result<(), Error> {
    writeln!(
        file,
        "/// Represents values of the Unicode character property"
    )?;
    writeln!(
        file,
        "/// [Bidi_Class](http://www.unicode.org/reports/tr44/#Bidi_Class),"
    )?;
    writeln!(
        file,
        "/// also known as the bidirectional character type. Possible values"
    )?;
    writeln!(file,
		"/// are listed in [UAX44, Table 13](http://www.unicode.org/reports/tr44/#Bidi_Class_Values)"
		)?;
    writeln!(file)?;

    // Values in `BidiClass` `enum` are generated by parsing the `UCD` files, they are upper case
    // which goes against the rust naming rules. Let's allow it just for this time
    writeln!(file, "#[allow(clippy::upper_case_acronyms)]")?;

    writeln!(file, "#[derive(Copy, Clone, PartialEq, Eq, Debug)]")?;
    writeln!(file, "pub enum BidiClass {{")?;
    writeln!(file, "\tAL,")?;
    writeln!(file, "\tAN,")?;
    writeln!(file, "\tB,")?;
    writeln!(file, "\tBN,")?;
    writeln!(file, "\tCS,")?;
    writeln!(file, "\tEN,")?;
    writeln!(file, "\tES,")?;
    writeln!(file, "\tET,")?;
    writeln!(file, "\tFSI,")?;
    writeln!(file, "\tL,")?;
    writeln!(file, "\tLRE,")?;
    writeln!(file, "\tLRI,")?;
    writeln!(file, "\tLRO,")?;
    writeln!(file, "\tNSM,")?;
    writeln!(file, "\tON,")?;
    writeln!(file, "\tPDF,")?;
    writeln!(file, "\tPDI,")?;
    writeln!(file, "\tR,")?;
    writeln!(file, "\tRLE,")?;
    writeln!(file, "\tRLI,")?;
    writeln!(file, "\tRLO,")?;
    writeln!(file, "\tS,")?;
    writeln!(file, "\tWS,")?;
    writeln!(file, "}}")?;

    Ok(writeln!(file)?)
}
impl BidiClassGen {
    fn generate_bidi_class_table(&mut self, file: &mut File) -> Result<(), Error> {
        self.compress_into_ranges();
        self.write_table_to_file(file)
    }

    fn compress_into_ranges(&mut self) {
        let mut out = Vec::new();
        let mut range: Option<CodepointRange> = None;
        let mut val: Option<String> = None;

        for (cp, bidi) in self.vec.iter() {
            if val.is_none() {
                val = Some(bidi.clone());
            }

            if val.as_ref() != Some(bidi) {
                // No same `bidi` class
                match range.as_ref() {
                    Some(r) => {
                        add_range(r, &val.unwrap(), &mut out);
                        range = None;
                    }
                    None => out.push((*cp, val.unwrap())),
                }
                val = Some(bidi.clone());
            }

            match cp {
                Codepoints::Single(cp) => {
                    match range.as_mut() {
                        Some(r) => {
                            if cp.value() - r.end.value() == 1 {
                                r.end = *cp;
                            } else {
                                // there is a gap, non-consecutive numbers
                                add_range(r, bidi, &mut out);
                                // Start a new range
                                range = Some(CodepointRange {
                                    start: *cp,
                                    end: *cp,
                                });
                            }
                        }
                        None => {
                            range = Some(CodepointRange {
                                start: *cp,
                                end: *cp,
                            });
                        }
                    }
                }
                Codepoints::Range(cp) => {
                    match range.as_mut() {
                        Some(r) => {
                            if cp.start.value() - r.end.value() == 1 {
                                // This range can be included in the previous one
                                r.end = cp.end;
                            } else {
                                // no consecutive ranges
                                out.push((Codepoints::Range(*r), bidi.clone()));
                                out.push((Codepoints::Range(*cp), bidi.clone()));
                                range = None;
                            }
                        }
                        None => {
                            range = Some(*cp);
                        }
                    }
                }
            }
        }

        self.vec = out;
    }

    fn write_table_to_file(&mut self, file: &mut File) -> Result<(), Error> {
        writeln!(
            file,
            "static {}: [(Codepoints, BidiClass); {}] = [",
            self.table_name.to_uppercase(),
            self.vec.len()
        )?;

        for (cp, bidi) in self.vec.iter() {
            writeln!(
                file,
                "\t({}, BidiClass::{}),",
                file_writer::generate_codepoint_str(cp),
                bidi
            )?;
        }

        writeln!(file, "];")?;
        Ok(writeln!(file)?)
    }
}