1#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use std::sync::LazyLock;
30
31use anyhow::{Result, anyhow};
32
33use crate::dc_def::DcDefn;
34
35static ALL_DC_DEFNS: LazyLock<Vec<DcDefn>> = LazyLock::new(|| {
36 #[expect(
37 clippy::expect_used,
38 reason = "Bundled runtime seed graph (data/graph/85899_34592.ctbnbdl) is validated at build time and must load"
39 )]
40 let index = crate::seed_graph::SeedGraphIndex::shared()
41 .expect("Bundled runtime seed graph must be present");
42 #[expect(
43 clippy::expect_used,
44 reason = "Bundled runtime seed graph Dc definitions are validated at build time"
45 )]
46 index.reconstruct_dc_defns().expect(
47 "Bundled runtime seed graph Dc definitions must reconstruct cleanly",
48 )
49});
50
51static ALL_FORMAT_DEFNS: LazyLock<Vec<DcDefn>> = LazyLock::new(|| {
52 #[expect(
53 clippy::expect_used,
54 reason = "Bundled runtime seed graph (data/graph/85899_34592.ctbnbdl) is validated at build time and must load"
55 )]
56 let index = crate::seed_graph::SeedGraphIndex::shared()
57 .expect("Bundled runtime seed graph must be present");
58 #[expect(
59 clippy::expect_used,
60 reason = "Bundled runtime seed graph format definitions are validated at build time"
61 )]
62 index
63 .reconstruct_format_defns()
64 .expect("Bundled runtime seed graph format definitions must reconstruct cleanly")
65});
66
67static ALL_EITE_ROWS: LazyLock<Vec<Vec<String>>> = LazyLock::new(|| {
68 get_all_dc_defns()
69 .iter()
70 .filter_map(|defn| {
71 let short_id = defn.short_id?;
72 let name = if defn.is_deprecated {
73 format!("!{}", defn.name)
74 } else {
75 defn.name.clone()
76 };
77 let casing = defn
79 .casing_partner
80 .map_or_else(String::new, |c| c.to_string());
81 let mut traits = Vec::new();
82 if let Some(ident) = &defn.ident {
83 traits.push(format!("@ident({ident:?})"));
84 }
85 for imp in &defn.implies {
86 traits.push(format!("@implies({imp})"));
87 }
88 if let Some(closes) = &defn.closes {
89 traits.push(format!("@closes({closes})"));
90 }
91 if let Some(syntax) = &defn.syntax {
92 traits.push(syntax.raw.clone());
93 }
94 for decomp in &defn.decompositions {
95 traits.push(decomp.clone());
96 }
97 for xref in &defn.cross_references {
98 traits.push(format!(">{xref}"));
99 }
100 for (_, fa) in &defn.formal_aliases {
101 traits.push(format!("[{fa}]"));
102 }
103 for alias in &defn.aliases {
104 traits.push(alias.clone());
105 }
106 Some(vec![
107 short_id.to_string(),
108 name,
109 defn.combining_class.to_string(),
110 defn.bidi_class.as_str().to_string(),
111 casing,
112 defn.general_category.as_str().to_string(),
113 defn.script.clone(),
114 traits.join(", "),
115 defn.description.clone(),
116 ])
117 })
118 .collect()
119});
120
121pub fn get_all_dc_defns() -> &'static [DcDefn] {
123 &ALL_DC_DEFNS
124}
125
126pub fn get_all_format_defns() -> &'static [DcDefn] {
128 &ALL_FORMAT_DEFNS
129}
130
131pub fn get_dc_defn(dc_id: u128) -> Option<&'static DcDefn> {
133 let defns = get_all_dc_defns();
134 defns
135 .binary_search_by_key(&dc_id, |d| d.dc_id)
136 .ok()
137 .and_then(|idx| defns.get(idx))
138}
139
140pub fn get_short_dc_defn(short_id: usize) -> Option<&'static DcDefn> {
142 let short_u32 = u32::try_from(short_id).ok()?;
143 get_dc_defn(crate::dc::short_to_long_dc(short_u32))
144}
145
146pub fn maximum_known_short_dc() -> usize {
148 #[expect(
149 clippy::expect_used,
150 reason = "Embedded Dc category tables always define short Dcs (0..=561)"
151 )]
152 get_all_dc_defns()
153 .iter()
154 .filter_map(|d| d.short_id)
155 .max()
156 .expect("Embedded Dc definitions must contain short Dcs")
157}
158
159pub fn get_dc_count() -> usize {
161 get_eite_dc_data_rows().len()
162}
163
164pub fn get_dc_name(dc_id: u128) -> Result<String> {
166 get_dc_defn(dc_id)
167 .map(|d| d.name.clone())
168 .ok_or_else(|| anyhow!("Unknown Dc ID: {dc_id}"))
169}
170
171pub fn get_short_dc_name(short_id: u32) -> Result<String> {
173 get_dc_name(crate::dc::short_to_long_dc(short_id))
174}
175
176pub fn get_eite_dc_data_rows() -> &'static [Vec<String>] {
179 &ALL_EITE_ROWS
180}
181
182#[cfg(test)]
183#[allow(
184 clippy::panic,
185 clippy::expect_used,
186 clippy::unwrap_used,
187 clippy::unwrap_in_result,
188 clippy::panic_in_result_fn,
189 clippy::indexing_slicing,
190 clippy::arithmetic_side_effects,
191 reason = "Standard repository test boilerplate"
192)]
193mod tests {
194 use super::*;
195
196 #[crate::ctb_test]
197 fn test_lookup_basic() {
198 let dc0 = get_short_dc_defn(0).expect("Dc 0 exists");
199 assert_eq!(dc0.name, "Null (short Dc)");
200 assert_eq!(dc0.category, "controls");
201
202 let dc0_long = get_dc_defn(crate::dc::short_to_long_dc(0))
203 .expect("Dc 0 long ID exists");
204 assert_eq!(dc0_long.name, "Null (short Dc)");
205
206 let dc6 = get_short_dc_defn(6).expect("Dc 6 exists");
207 assert_eq!(dc6.name, "Begin number");
208
209 let max_dc = maximum_known_short_dc();
210 assert!(max_dc >= 308);
211 assert!(get_dc_count() <= max_dc + 1);
212
213 let dc308 = get_short_dc_defn(308).expect("Dc 308 exists");
214 assert_eq!(dc308.name, "Next number is a long (global graph) Dc");
215 assert_eq!(
216 get_short_dc_name(308).unwrap(),
217 "Next number is a long (global graph) Dc"
218 );
219 assert_eq!(
220 get_dc_name(crate::dc::short_to_long_dc(308)).unwrap(),
221 "Next number is a long (global graph) Dc"
222 );
223
224 let u10 = get_dc_defn(10).expect("Unicode clarification 10 exists");
225 assert_eq!(u10.name, "<control, Unicode-semantic-defined> LINE FEED");
226 assert_eq!(u10.category, "unicode-clarifications");
227 assert_eq!(
228 get_dc_name(10).unwrap(),
229 "<control, Unicode-semantic-defined> LINE FEED"
230 );
231 }
232
233 #[crate::ctb_test]
234 fn test_eite_dc_data_rows() {
235 let rows = get_eite_dc_data_rows();
236 assert_eq!(rows.len(), get_dc_count());
237
238 let row0 = &rows[0];
239 assert_eq!(row0.len(), 9);
240 assert_eq!(row0[0], "0");
241 assert_eq!(row0[1], "!Null (short Dc)");
242
243 let row308 = &rows[308];
244 assert_eq!(row308.len(), 9);
245 assert_eq!(row308[0], "308");
246 assert_eq!(row308[1], "Next number is a long (global graph) Dc");
247 }
248
249 #[crate::ctb_test]
250 fn test_seed_graph_dc_defns_match_embedded_csv_defns() {
251 let from_graph = get_all_dc_defns();
252 let mut report = crate::report::ValidationReport::new();
253 let known_format_ids = std::collections::HashSet::new();
254 let dc_dir = crate::get_dc_categories_dir_path();
255 let mut from_csv = crate::validation::validate_all_dc_files_from_disk(
256 &dc_dir,
257 &known_format_ids,
258 None,
259 None,
260 None,
261 None,
262 None,
263 &mut report,
264 );
265 from_csv.sort_by_key(|d| d.dc_id);
266
267 assert_eq!(from_graph.len(), from_csv.len());
268 for (g, c) in from_graph.iter().zip(from_csv.iter()) {
269 assert_eq!(g.dc_id, c.dc_id);
270 assert_eq!(
271 g.short_id, c.short_id,
272 "short_id mismatch on {}",
273 c.dc_id
274 );
275 assert_eq!(g.ident, c.ident, "ident mismatch on {}", c.dc_id);
276 assert_eq!(
277 g.stable_ident, c.stable_ident,
278 "stable_ident mismatch on {}",
279 c.dc_id
280 );
281 assert_eq!(g.name, c.name, "name mismatch on {}", c.dc_id);
282 assert_eq!(
283 g.category, c.category,
284 "category mismatch on {}",
285 c.dc_id
286 );
287 assert_eq!(
288 g.combining_class, c.combining_class,
289 "combining_class mismatch on {}",
290 c.dc_id
291 );
292 assert_eq!(
293 g.bidi_class, c.bidi_class,
294 "bidi_class mismatch on {}",
295 c.dc_id
296 );
297 assert_eq!(
298 g.casing_partner, c.casing_partner,
299 "casing_partner mismatch on {}",
300 c.dc_id
301 );
302 assert_eq!(
303 g.general_category, c.general_category,
304 "general_category mismatch on {}",
305 c.dc_id
306 );
307 assert_eq!(g.script, c.script, "script mismatch on {}", c.dc_id);
308 assert_eq!(
309 g.is_deprecated, c.is_deprecated,
310 "is_deprecated mismatch on {}",
311 c.dc_id
312 );
313 assert_eq!(g.closes, c.closes, "closes mismatch on {}", c.dc_id);
314 assert_eq!(
315 g.description, c.description,
316 "description mismatch on {}",
317 c.dc_id
318 );
319 assert_eq!(
320 g.syntax.as_ref().map(|r| (&r.pattern, &r.action)),
321 c.syntax.as_ref().map(|r| (&r.pattern, &r.action)),
322 "syntax AST mismatch on {}",
323 c.dc_id
324 );
325 assert_eq!(
326 g.format, c.format,
327 "format details mismatch on {}",
328 c.dc_id
329 );
330 }
331 }
332
333 #[crate::ctb_test]
334 fn test_seed_graph_format_defns_match_embedded_csv_defns() {
335 let from_graph = get_all_format_defns();
336 let mut report = crate::report::ValidationReport::new();
337 let fmt_dir = crate::get_formats_categories_dir_path();
338 let mut from_csv = crate::format::validate_all_format_files_from_disk(
339 &fmt_dir,
340 None,
341 &mut report,
342 );
343 from_csv.sort_by_key(|d| d.dc_id);
344
345 assert_eq!(from_graph.len(), from_csv.len());
346 let split_set =
347 |opt: &Option<String>| -> std::collections::BTreeSet<String> {
348 opt.as_deref()
349 .map(|s| {
350 s.split(',')
351 .map(str::trim)
352 .filter(|x| !x.is_empty())
353 .map(str::to_string)
354 .collect()
355 })
356 .unwrap_or_default()
357 };
358 let first_item = |opt: &Option<String>| -> Option<String> {
359 opt.as_deref()
360 .and_then(|s| {
361 s.split(',').map(str::trim).find(|x| !x.is_empty())
362 })
363 .map(str::to_string)
364 };
365
366 for (g, c) in from_graph.iter().zip(from_csv.iter()) {
367 assert_eq!(g.dc_id, c.dc_id);
368 assert_eq!(
369 g.short_id, c.short_id,
370 "short_id mismatch on f{:?}",
371 c.short_id
372 );
373 assert_eq!(g.ident, c.ident, "ident mismatch on f{:?}", c.short_id);
374 assert_eq!(
375 g.stable_ident, c.stable_ident,
376 "stable_ident mismatch on f{:?}",
377 c.short_id
378 );
379 assert_eq!(g.name, c.name, "name mismatch on f{:?}", c.short_id);
380 assert_eq!(g.title, c.title, "title mismatch on f{:?}", c.short_id);
381 assert_eq!(
382 g.category, c.category,
383 "category mismatch on f{:?}",
384 c.short_id
385 );
386 assert_eq!(
387 g.script, c.script,
388 "script mismatch on f{:?}",
389 c.short_id
390 );
391 assert_eq!(
392 g.is_deprecated, c.is_deprecated,
393 "is_deprecated mismatch on f{:?}",
394 c.short_id
395 );
396 assert_eq!(
397 g.closes, c.closes,
398 "closes mismatch on f{:?}",
399 c.short_id
400 );
401 assert_eq!(
402 g.description, c.description,
403 "description mismatch on f{:?}",
404 c.short_id
405 );
406 assert_eq!(
407 g.syntax.as_ref().map(|r| (&r.pattern, &r.action)),
408 c.syntax.as_ref().map(|r| (&r.pattern, &r.action)),
409 "syntax AST mismatch on f{:?}",
410 c.short_id
411 );
412 let gf = g.format.as_ref().expect("graph format details");
413 let cf = c.format.as_ref().expect("csv format details");
414 assert_eq!(
415 gf.format_spec, cf.format_spec,
416 "format_spec mismatch on f{:?}",
417 c.short_id
418 );
419 assert_eq!(
420 gf.chain, cf.chain,
421 "chain mismatch on f{:?}",
422 c.short_id
423 );
424 assert_eq!(
425 first_item(&gf.extensions),
426 first_item(&cf.extensions),
427 "primary extension mismatch on f{:?}",
428 c.short_id
429 );
430 assert_eq!(
431 split_set(&gf.extensions),
432 split_set(&cf.extensions),
433 "extensions set mismatch on f{:?}",
434 c.short_id
435 );
436 assert_eq!(
437 first_item(&gf.mime),
438 first_item(&cf.mime),
439 "primary mime mismatch on f{:?}",
440 c.short_id
441 );
442 assert_eq!(
443 split_set(&gf.mime),
444 split_set(&cf.mime),
445 "mime set mismatch on f{:?}",
446 c.short_id
447 );
448 assert_eq!(
449 first_item(&gf.uti),
450 first_item(&cf.uti),
451 "primary uti mismatch on f{:?}",
452 c.short_id
453 );
454 assert_eq!(
455 split_set(&gf.uti),
456 split_set(&cf.uti),
457 "uti set mismatch on f{:?}",
458 c.short_id
459 );
460 assert_eq!(
461 gf.apple_type, cf.apple_type,
462 "apple_type mismatch on f{:?}",
463 c.short_id
464 );
465 assert_eq!(
466 first_item(&gf.nicknames),
467 first_item(&cf.nicknames),
468 "primary nickname mismatch on f{:?}",
469 c.short_id
470 );
471 assert_eq!(
472 split_set(&gf.nicknames),
473 split_set(&cf.nicknames),
474 "nicknames set mismatch on f{:?}",
475 c.short_id
476 );
477 assert_eq!(
478 gf.import_support, cf.import_support,
479 "import_support mismatch on f{:?}",
480 c.short_id
481 );
482 assert_eq!(
483 gf.export_support, cf.export_support,
484 "export_support mismatch on f{:?}",
485 c.short_id
486 );
487 assert_eq!(
488 gf.tests, cf.tests,
489 "tests mismatch on f{:?}",
490 c.short_id
491 );
492 assert_eq!(
493 gf.variant_types, cf.variant_types,
494 "variant_types mismatch on f{:?}",
495 c.short_id
496 );
497 assert_eq!(
498 gf.default_line_ending, cf.default_line_ending,
499 "default_line_ending mismatch on f{:?}",
500 c.short_id
501 );
502 assert_eq!(
503 gf.vary_as, cf.vary_as,
504 "vary_as mismatch on f{:?}",
505 c.short_id
506 );
507 assert_eq!(
508 gf.varies, cf.varies,
509 "varies mismatch on f{:?}",
510 c.short_id
511 );
512 }
513 }
514}