1#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use crate::format_spec::FormatExpr;
30use crate::syntax::DcSyntaxRule;
31use serde::{Deserialize, Serialize};
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
35pub enum GeneralCategory {
36 Lu,
37 Ll,
38 Lt,
39 Lm,
40 Lo,
41 Mn,
42 Mc,
43 Me,
44 Nd,
45 Nl,
46 No,
47 Pc,
48 Pd,
49 Ps,
50 Pe,
51 Pi,
52 Pf,
53 Po,
54 Sm,
55 Sc,
56 Sk,
57 So,
58 Zs,
59 Zl,
60 Zp,
61 Cc,
62 Cf,
63 Cs,
64 Co,
65 Cn,
66 #[serde(rename = "!Cx")]
67 NonUnicodeControl,
68}
69
70impl GeneralCategory {
71 #[must_use]
72 pub const fn as_str(self) -> &'static str {
73 match self {
74 Self::Lu => "Lu",
75 Self::Ll => "Ll",
76 Self::Lt => "Lt",
77 Self::Lm => "Lm",
78 Self::Lo => "Lo",
79 Self::Mn => "Mn",
80 Self::Mc => "Mc",
81 Self::Me => "Me",
82 Self::Nd => "Nd",
83 Self::Nl => "Nl",
84 Self::No => "No",
85 Self::Pc => "Pc",
86 Self::Pd => "Pd",
87 Self::Ps => "Ps",
88 Self::Pe => "Pe",
89 Self::Pi => "Pi",
90 Self::Pf => "Pf",
91 Self::Po => "Po",
92 Self::Sm => "Sm",
93 Self::Sc => "Sc",
94 Self::Sk => "Sk",
95 Self::So => "So",
96 Self::Zs => "Zs",
97 Self::Zl => "Zl",
98 Self::Zp => "Zp",
99 Self::Cc => "Cc",
100 Self::Cf => "Cf",
101 Self::Cs => "Cs",
102 Self::Co => "Co",
103 Self::Cn => "Cn",
104 Self::NonUnicodeControl => "!Cx",
105 }
106 }
107}
108
109impl std::fmt::Display for GeneralCategory {
110 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111 f.write_str(self.as_str())
112 }
113}
114
115impl std::str::FromStr for GeneralCategory {
116 type Err = anyhow::Error;
117
118 fn from_str(s: &str) -> Result<Self, Self::Err> {
119 match s.trim() {
120 "Lu" => Ok(Self::Lu),
121 "Ll" => Ok(Self::Ll),
122 "Lt" => Ok(Self::Lt),
123 "Lm" => Ok(Self::Lm),
124 "Lo" => Ok(Self::Lo),
125 "Mn" => Ok(Self::Mn),
126 "Mc" => Ok(Self::Mc),
127 "Me" => Ok(Self::Me),
128 "Nd" => Ok(Self::Nd),
129 "Nl" => Ok(Self::Nl),
130 "No" => Ok(Self::No),
131 "Pc" => Ok(Self::Pc),
132 "Pd" => Ok(Self::Pd),
133 "Ps" => Ok(Self::Ps),
134 "Pe" => Ok(Self::Pe),
135 "Pi" => Ok(Self::Pi),
136 "Pf" => Ok(Self::Pf),
137 "Po" => Ok(Self::Po),
138 "Sm" => Ok(Self::Sm),
139 "Sc" => Ok(Self::Sc),
140 "Sk" => Ok(Self::Sk),
141 "So" => Ok(Self::So),
142 "Zs" => Ok(Self::Zs),
143 "Zl" => Ok(Self::Zl),
144 "Zp" => Ok(Self::Zp),
145 "Cc" => Ok(Self::Cc),
146 "Cf" => Ok(Self::Cf),
147 "Cs" => Ok(Self::Cs),
148 "Co" => Ok(Self::Co),
149 "Cn" => Ok(Self::Cn),
150 "!Cx" => Ok(Self::NonUnicodeControl),
151 other => bail!(
152 "Invalid General Category '{other}' (expected standard Unicode category or '!Cx')"
153 ),
154 }
155 }
156}
157
158#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
160pub enum BidiClass {
161 L,
162 R,
163 AL,
164 EN,
165 ES,
166 ET,
167 AN,
168 CS,
169 NSM,
170 BN,
171 B,
172 S,
173 WS,
174 ON,
175 LRE,
176 LRO,
177 RLE,
178 RLO,
179 PDF,
180 LRI,
181 RLI,
182 FSI,
183 PDI,
184}
185
186impl BidiClass {
187 #[must_use]
188 pub const fn as_str(self) -> &'static str {
189 match self {
190 Self::L => "L",
191 Self::R => "R",
192 Self::AL => "AL",
193 Self::EN => "EN",
194 Self::ES => "ES",
195 Self::ET => "ET",
196 Self::AN => "AN",
197 Self::CS => "CS",
198 Self::NSM => "NSM",
199 Self::BN => "BN",
200 Self::B => "B",
201 Self::S => "S",
202 Self::WS => "WS",
203 Self::ON => "ON",
204 Self::LRE => "LRE",
205 Self::LRO => "LRO",
206 Self::RLE => "RLE",
207 Self::RLO => "RLO",
208 Self::PDF => "PDF",
209 Self::LRI => "LRI",
210 Self::RLI => "RLI",
211 Self::FSI => "FSI",
212 Self::PDI => "PDI",
213 }
214 }
215}
216
217impl std::fmt::Display for BidiClass {
218 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
219 f.write_str(self.as_str())
220 }
221}
222
223impl std::str::FromStr for BidiClass {
224 type Err = anyhow::Error;
225
226 fn from_str(s: &str) -> Result<Self, Self::Err> {
227 match s.trim() {
228 "L" => Ok(Self::L),
229 "R" => Ok(Self::R),
230 "AL" => Ok(Self::AL),
231 "EN" => Ok(Self::EN),
232 "ES" => Ok(Self::ES),
233 "ET" => Ok(Self::ET),
234 "AN" => Ok(Self::AN),
235 "CS" => Ok(Self::CS),
236 "NSM" => Ok(Self::NSM),
237 "BN" => Ok(Self::BN),
238 "B" => Ok(Self::B),
239 "S" => Ok(Self::S),
240 "WS" => Ok(Self::WS),
241 "ON" => Ok(Self::ON),
242 "LRE" => Ok(Self::LRE),
243 "LRO" => Ok(Self::LRO),
244 "RLE" => Ok(Self::RLE),
245 "RLO" => Ok(Self::RLO),
246 "PDF" => Ok(Self::PDF),
247 "LRI" => Ok(Self::LRI),
248 "RLI" => Ok(Self::RLI),
249 "FSI" => Ok(Self::FSI),
250 "PDI" => Ok(Self::PDI),
251 other => bail!(
252 "Invalid Bidi Class '{other}' (expected standard Unicode bidi class)"
253 ),
254 }
255 }
256}
257
258#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
260pub struct SupportLevel {
261 pub general: i32,
263 pub dc: i32,
265}
266
267impl SupportLevel {
268 pub fn parse(val: &str) -> Result<Self> {
273 let trimmed = val.trim();
274 if trimmed.is_empty() {
275 return Ok(Self { general: 0, dc: 0 });
276 }
277 let parts: Vec<&str> = trimmed.split_whitespace().collect();
278 let (first, second) = match parts.as_slice() {
279 [first, second] => (*first, *second),
280 _ => bail!(
281 "Invalid support level '{val}' (expected exactly two space-separated levels '<general> <dc>' in range -1..=5, or blank)"
282 ),
283 };
284 let parse_part = |part: &str| -> Result<i32> {
285 match part {
286 "-1" | "0" | "1" | "2" | "3" | "4" | "5" => {
287 part.parse::<i32>().context("Parsing support level integer")
288 }
289 _ => bail!(
290 "Invalid support level component '{part}' (expected -1..=5)"
291 ),
292 }
293 };
294 let general = parse_part(first)?;
295 let dc = parse_part(second)?;
296 Ok(Self { general, dc })
297 }
298
299 #[must_use]
301 pub fn format_csv(&self) -> String {
302 if self.general == 0 && self.dc == 0 {
303 String::new()
304 } else {
305 format!("{} {}", self.general, self.dc)
306 }
307 }
308}
309
310#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
312pub struct FormatDetails {
313 #[serde(skip_serializing_if = "Option::is_none")]
314 pub implies: Option<String>,
315 #[serde(skip_serializing_if = "Option::is_none")]
316 pub based_on: Option<String>,
317 #[serde(skip_serializing_if = "Option::is_none")]
318 pub chain: Option<String>,
319 #[serde(skip_serializing_if = "Option::is_none")]
320 pub format_spec: Option<FormatExpr>,
321 #[serde(skip_serializing_if = "Option::is_none")]
322 pub extensions: Option<String>,
323 #[serde(skip_serializing_if = "Option::is_none")]
324 pub mime: Option<String>,
325 #[serde(skip_serializing_if = "Option::is_none")]
326 pub uti: Option<String>,
327 #[serde(skip_serializing_if = "Option::is_none")]
328 pub apple_type: Option<String>,
329 #[serde(skip_serializing_if = "Option::is_none")]
330 pub nicknames: Option<String>,
331 #[serde(skip_serializing_if = "Option::is_none")]
332 pub import_support: Option<String>,
333 #[serde(skip_serializing_if = "Option::is_none")]
334 pub export_support: Option<String>,
335 #[serde(skip_serializing_if = "Option::is_none")]
336 pub tests: Option<String>,
337 #[serde(skip_serializing_if = "Option::is_none")]
338 pub variant_types: Option<String>,
339 #[serde(skip_serializing_if = "Option::is_none")]
340 pub title: Option<String>,
341 #[serde(skip_serializing_if = "Option::is_none")]
342 pub default_line_ending: Option<String>,
343 #[serde(skip_serializing_if = "Option::is_none")]
344 pub vary_as: Option<String>,
345 #[serde(
346 default,
347 skip_serializing_if = "Vec::is_empty",
348 serialize_with = "serialize_shorthands",
349 deserialize_with = "deserialize_shorthands"
350 )]
351 pub varies: Vec<ctb_storage_minimal::shorthand::DcShorthand>,
352}
353
354fn serialize_shorthands<S>(
355 shorthands: &[ctb_storage_minimal::shorthand::DcShorthand],
356 serializer: S,
357) -> std::result::Result<S::Ok, S::Error>
358where
359 S: serde::Serializer,
360{
361 use serde::ser::SerializeSeq;
362 let mut seq = serializer.serialize_seq(Some(shorthands.len()))?;
363 for sh in shorthands {
364 seq.serialize_element(&sh.to_string())?;
365 }
366 seq.end()
367}
368
369fn deserialize_shorthands<'de, D>(
370 deserializer: D,
371) -> std::result::Result<
372 Vec<ctb_storage_minimal::shorthand::DcShorthand>,
373 D::Error,
374>
375where
376 D: serde::Deserializer<'de>,
377{
378 let strings = Vec::<String>::deserialize(deserializer)?;
379 let mut res = Vec::with_capacity(strings.len());
380 for s in strings {
381 let sh = ctb_storage_minimal::shorthand::DcShorthand::parse(&s)
382 .map_err(serde::de::Error::custom)?;
383 res.push(sh);
384 }
385 Ok(res)
386}
387
388fn serialize_opt_shorthand<S>(
389 shorthand: &Option<ctb_storage_minimal::shorthand::DcShorthand>,
390 serializer: S,
391) -> std::result::Result<S::Ok, S::Error>
392where
393 S: serde::Serializer,
394{
395 match shorthand {
396 Some(sh) => serializer.serialize_some(&sh.to_string()),
397 None => serializer.serialize_none(),
398 }
399}
400
401fn deserialize_opt_shorthand<'de, D>(
402 deserializer: D,
403) -> std::result::Result<
404 Option<ctb_storage_minimal::shorthand::DcShorthand>,
405 D::Error,
406>
407where
408 D: serde::Deserializer<'de>,
409{
410 let opt_str = Option::<String>::deserialize(deserializer)?;
411 match opt_str {
412 Some(s) => {
413 let sh = ctb_storage_minimal::shorthand::DcShorthand::parse(&s)
414 .map_err(serde::de::Error::custom)?;
415 Ok(Some(sh))
416 }
417 None => Ok(None),
418 }
419}
420
421#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
423pub struct DcDefn {
424 pub dc_id: u128,
426 #[serde(default, skip_serializing_if = "Option::is_none")]
427 pub short_id: Option<usize>,
428 #[serde(skip_serializing_if = "Option::is_none")]
429 pub ident: Option<String>,
430 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
431 pub stable_ident: bool,
432 #[serde(skip_serializing_if = "Option::is_none")]
433 pub title: Option<String>,
434 pub name: String,
435 pub category: String,
436
437 pub combining_class: u8,
439 pub bidi_class: BidiClass,
440 #[serde(skip_serializing_if = "Option::is_none")]
441 pub casing_partner: Option<u32>,
442 pub general_category: GeneralCategory,
443 pub script: String,
444 pub is_deprecated: bool,
445 pub decompositions: Vec<String>,
446
447 pub aliases: Vec<String>,
449 #[serde(default, skip_serializing_if = "Vec::is_empty")]
450 pub formal_aliases: Vec<(String, String)>,
451 pub cross_references: Vec<String>,
452 #[serde(default, skip_serializing_if = "Vec::is_empty")]
453 pub annotations: Vec<String>,
454 #[serde(default, skip_serializing_if = "Vec::is_empty")]
455 pub implies: Vec<String>,
456 #[serde(default, skip_serializing_if = "Vec::is_empty")]
457 pub based_on: Vec<String>,
458 #[serde(default, skip_serializing_if = "Vec::is_empty")]
459 pub os_associations: Vec<String>,
460 #[serde(default, skip_serializing_if = "Vec::is_empty")]
461 pub magic_names: Vec<String>,
462
463 #[serde(skip_serializing_if = "Option::is_none")]
465 pub syntax: Option<DcSyntaxRule>,
466 #[serde(
467 default,
468 skip_serializing_if = "Option::is_none",
469 serialize_with = "serialize_opt_shorthand",
470 deserialize_with = "deserialize_opt_shorthand"
471 )]
472 pub closes: Option<ctb_storage_minimal::shorthand::DcShorthand>,
473
474 pub description: String,
476
477 #[serde(skip_serializing_if = "Option::is_none")]
479 pub format: Option<FormatDetails>,
480
481 pub source_file: String,
483 pub line_number: usize,
484}