precis_tools/generators/
derived_property.rs

1use crate::error::Error;
2use crate::generators::CodeGen;
3use std::fs::File;
4use std::io::Write;
5
6/// Generates the derived property `enum` with the
7/// values described in the PRECIS
8/// [Code Point Properties](https://datatracker.ietf.org/doc/html/rfc8264#section-8)
9/// section.
10/// # Example:
11/// ```rust
12/// pub enum DerivedPropertyValue {
13///    PValid,
14///    SpecClassPval,
15///    SpecClassDis,
16///    ContextJ,
17///    ContextO,
18///    Disallowed,
19///    Unassigned,
20/// }
21/// ```
22pub struct DerivedPropertyValueGen {}
23
24impl DerivedPropertyValueGen {
25    /// Creates a new generator for the `DerivedPropertyValue` `enum`.
26    pub fn new() -> Self {
27        Self {}
28    }
29}
30
31impl Default for DerivedPropertyValueGen {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37impl CodeGen for DerivedPropertyValueGen {
38    fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
39        writeln!(file, "/// Derived property value")?;
40        writeln!(file, "/// # Notes")?;
41        writeln!(
42            file,
43            "/// * **SpecClassPVal** maps to those code points that are allowed"
44        )?;
45        writeln!(
46            file,
47            "///   to be used in specific string classes such as [`IdentifierClass`]"
48        )?;
49        writeln!(
50            file,
51            "///   and [`FreeformClass`]. PRECIS framework defines two allowed"
52        )?;
53        writeln!(
54            file,
55            "///   values for above classes (ID_PVAL adn FREE_PVAL). In practice,"
56        )?;
57        writeln!(
58            file,
59            "///   the derived property ID_PVAL is not used in this specification,"
60        )?;
61        writeln!(
62            file,
63            "///   because every ID_PVAL code point is PVALID, so only FREE_PVAL"
64        )?;
65        writeln!(file, "///   is actually mapped to SpecClassPVal.")?;
66        writeln!(
67            file,
68            "/// * **SpecClassDis** maps to those code points that are not to be"
69        )?;
70        writeln!(
71            file,
72            "///   included in one of the string classes but that might be permitted"
73        )?;
74        writeln!(
75            file,
76            "///   in others. PRECIS framework defines \"FREE_DIS\" for the"
77        )?;
78        writeln!(
79            file,
80            "///   [`FreeformClass`] and \"ID_DIS\" for the [`IdentifierClass`]."
81        )?;
82        writeln!(
83            file,
84            "///   In practice, the derived property FREE_DIS is not used in this"
85        )?;
86        writeln!(
87            file,
88            "///   specification, because every FREE_DIS code point is DISALLOWED,"
89        )?;
90        writeln!(file, "///   so only ID_DIS is mapped to SpecClassDis.")?;
91        writeln!(
92            file,
93            "///   Both SpecClassPVal and SpecClassDis values are used to ease"
94        )?;
95        writeln!(
96            file,
97            "///   extension if more classes are added beyond [`IdentifierClass`]"
98        )?;
99        writeln!(file, "///   and [`FreeformClass`] in the future.")?;
100        writeln!(file, "#[derive(Clone, Copy, Debug, PartialEq, Eq)]")?;
101        writeln!(file, "pub enum DerivedPropertyValue {{")?;
102        writeln!(file, "\t/// Value assigned to all those code points that are allowed to be used in any PRECIS string class.")?;
103        writeln!(file, "\tPValid,")?;
104        writeln!(file, "\t/// Value assigned to all those code points that are allowed to be used in an specific PRECIS string class.")?;
105        writeln!(file, "\tSpecClassPval,")?;
106        writeln!(file, "\t/// Value assigned to all those code points that are disallowed by a specific PRECIS string class.")?;
107        writeln!(file, "\tSpecClassDis,")?;
108        writeln!(
109            file,
110            "\t/// Contextual rule required for Join_controls Unicode codepoints."
111        )?;
112        writeln!(file, "\tContextJ,")?;
113        writeln!(
114            file,
115            "\t/// Contextual rule required for Others Unicode codepoints."
116        )?;
117        writeln!(file, "\tContextO,")?;
118        writeln!(
119            file,
120            "\t/// Those code points that are not permitted in any PRECIS string class."
121        )?;
122        writeln!(file, "\tDisallowed,")?;
123        writeln!(
124            file,
125            "\t/// Those code points that are not designated in the Unicode Standard."
126        )?;
127        writeln!(file, "\tUnassigned,")?;
128        writeln!(file, "}}")?;
129
130        writeln!(file)?;
131        writeln!(file, "impl std::fmt::Display for DerivedPropertyValue {{")?;
132        writeln!(
133            file,
134            "\tfn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {{"
135        )?;
136        writeln!(file, "\t\tmatch self {{")?;
137        writeln!(
138            file,
139            "\t\t\tDerivedPropertyValue::PValid => writeln!(f, \"PValid\"),"
140        )?;
141        writeln!(
142            file,
143            "\t\t\tDerivedPropertyValue::SpecClassPval => writeln!(f, \"SpecClassPval\"),"
144        )?;
145        writeln!(
146            file,
147            "\t\t\tDerivedPropertyValue::SpecClassDis => writeln!(f, \"SpecClassDis\"),"
148        )?;
149        writeln!(
150            file,
151            "\t\t\tDerivedPropertyValue::ContextJ => writeln!(f, \"ContextJ\"),"
152        )?;
153        writeln!(
154            file,
155            "\t\t\tDerivedPropertyValue::ContextO => writeln!(f, \"ContextO\"),"
156        )?;
157        writeln!(
158            file,
159            "\t\t\tDerivedPropertyValue::Disallowed => writeln!(f, \"Disallowed\"),"
160        )?;
161        writeln!(
162            file,
163            "\t\t\tDerivedPropertyValue::Unassigned => writeln!(f, \"Unassigned\"),"
164        )?;
165        writeln!(file, "\t\t}}")?;
166
167        writeln!(file, "\t}}")?;
168        writeln!(file, "}}")?;
169
170        Ok(writeln!(file)?)
171    }
172}