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
use crate::error::Error;
use crate::generators::CodeGen;
use std::fs::File;
use std::io::Write;

/// Generates the derived property `enum` with the
/// values described in the PRECIS
/// [Code Point Properties](https://datatracker.ietf.org/doc/html/rfc8264#section-8)
/// section.
/// # Example:
/// ```rust
/// pub enum DerivedPropertyValue {
///    PValid,
///    SpecClassPval,
///    SpecClassDis,
///    ContextJ,
///    ContextO,
///    Disallowed,
///    Unassigned,
/// }
/// ```
pub struct DerivedPropertyValueGen {}

impl DerivedPropertyValueGen {
    /// Creates a new generator for the `DerivedPropertyValue` `enum`.
    pub fn new() -> Self {
        Self {}
    }
}

impl Default for DerivedPropertyValueGen {
    fn default() -> Self {
        Self::new()
    }
}

impl CodeGen for DerivedPropertyValueGen {
    fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
        writeln!(file, "/// Derived property value")?;
        writeln!(file, "/// # Notes")?;
        writeln!(
            file,
            "/// * **SpecClassPVal** maps to those code points that are allowed"
        )?;
        writeln!(
            file,
            "///   to be used in specific string classes such as [`IdentifierClass`]"
        )?;
        writeln!(
            file,
            "///   and [`FreeformClass`]. PRECIS framework defines two allowed"
        )?;
        writeln!(
            file,
            "///   values for above classes (ID_PVAL adn FREE_PVAL). In practice,"
        )?;
        writeln!(
            file,
            "///   the derived property ID_PVAL is not used in this specification,"
        )?;
        writeln!(
            file,
            "///   because every ID_PVAL code point is PVALID, so only FREE_PVAL"
        )?;
        writeln!(file, "///   is actually mapped to SpecClassPVal.")?;
        writeln!(
            file,
            "/// * **SpecClassDis** maps to those code points that are not to be"
        )?;
        writeln!(
            file,
            "///   included in one of the string classes but that might be permitted"
        )?;
        writeln!(
            file,
            "///   in others. PRECIS framework defines \"FREE_DIS\" for the"
        )?;
        writeln!(
            file,
            "///   [`FreeformClass`] and \"ID_DIS\" for the [`IdentifierClass`]."
        )?;
        writeln!(
            file,
            "///   In practice, the derived property FREE_DIS is not used in this"
        )?;
        writeln!(
            file,
            "///   specification, because every FREE_DIS code point is DISALLOWED,"
        )?;
        writeln!(file, "///   so only ID_DIS is mapped to SpecClassDis.")?;
        writeln!(
            file,
            "///   Both SpecClassPVal and SpecClassDis values are used to ease"
        )?;
        writeln!(
            file,
            "///   extension if more classes are added beyond [`IdentifierClass`]"
        )?;
        writeln!(file, "///   and [`FreeformClass`] in the future.")?;
        writeln!(file, "#[derive(Clone, Copy, Debug, PartialEq, Eq)]")?;
        writeln!(file, "pub enum DerivedPropertyValue {{")?;
        writeln!(file, "\t/// Value assigned to all those code points that are allowed to be used in any PRECIS string class.")?;
        writeln!(file, "\tPValid,")?;
        writeln!(file, "\t/// Value assigned to all those code points that are allowed to be used in an specific PRECIS string class.")?;
        writeln!(file, "\tSpecClassPval,")?;
        writeln!(file, "\t/// Value assigned to all those code points that are disallowed by a specific PRECIS string class.")?;
        writeln!(file, "\tSpecClassDis,")?;
        writeln!(
            file,
            "\t/// Contextual rule required for Join_controls Unicode codepoints."
        )?;
        writeln!(file, "\tContextJ,")?;
        writeln!(
            file,
            "\t/// Contextual rule required for Others Unicode codepoints."
        )?;
        writeln!(file, "\tContextO,")?;
        writeln!(
            file,
            "\t/// Those code points that are not permitted in any PRECIS string class."
        )?;
        writeln!(file, "\tDisallowed,")?;
        writeln!(
            file,
            "\t/// Those code points that are not designated in the Unicode Standard."
        )?;
        writeln!(file, "\tUnassigned,")?;
        writeln!(file, "}}")?;

        writeln!(file)?;
        writeln!(file, "impl std::fmt::Display for DerivedPropertyValue {{")?;
        writeln!(
            file,
            "\tfn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {{"
        )?;
        writeln!(file, "\t\tmatch self {{")?;
        writeln!(
            file,
            "\t\t\tDerivedPropertyValue::PValid => writeln!(f, \"PValid\"),"
        )?;
        writeln!(
            file,
            "\t\t\tDerivedPropertyValue::SpecClassPval => writeln!(f, \"SpecClassPval\"),"
        )?;
        writeln!(
            file,
            "\t\t\tDerivedPropertyValue::SpecClassDis => writeln!(f, \"SpecClassDis\"),"
        )?;
        writeln!(
            file,
            "\t\t\tDerivedPropertyValue::ContextJ => writeln!(f, \"ContextJ\"),"
        )?;
        writeln!(
            file,
            "\t\t\tDerivedPropertyValue::ContextO => writeln!(f, \"ContextO\"),"
        )?;
        writeln!(
            file,
            "\t\t\tDerivedPropertyValue::Disallowed => writeln!(f, \"Disallowed\"),"
        )?;
        writeln!(
            file,
            "\t\t\tDerivedPropertyValue::Unassigned => writeln!(f, \"Unassigned\"),"
        )?;
        writeln!(file, "\t\t}}")?;

        writeln!(file, "\t}}")?;
        writeln!(file, "}}")?;

        Ok(writeln!(file)?)
    }
}