Skip to main content

workshop_rs/catalog/
mod.rs

1//! The canonical Workshop catalog.
2//!
3//! The catalog is the locale-independent semantic identity layer between
4//! textual Workshop spellings and WIR. Every builtin has a canonical `id` and
5//! a [`Kind`]; locale tables map canonical identities to client spellings and
6//! back, so parser, emitter, analyzer, and tooling never embed
7//! locale-specific strings as identity.
8//!
9//! Locale coverage is data ([ADR-0001](https://github.com/wrightkit/workshop-rs/blob/main/docs/adr/0001-catalog-boundaries.md)):
10//! the primary locale (the first declared one, `en-US`) is complete — every
11//! entry and enum member carries a primary-locale alias — while additional
12//! declared locales may be partially covered. Missing target-locale mappings
13//! fail explicitly at conversion/emission time; the catalog reports exact
14//! per-locale coverage machine-readably ([`Catalog::locale_coverage`],
15//! [`Catalog::identity`]).
16//!
17//! The catalog dataset declares its own `version` and a deterministic content
18//! `digest` (sha256) recomputed by the catalog pipeline
19//! (`workshop-catalog-gen build`); [`Catalog::load`] rejects a digest
20//! mismatch, so dataset changes are deliberate and reproducible.
21
22pub mod detect;
23
24use std::collections::{BTreeMap, HashMap};
25
26use serde::{Deserialize, Deserializer, Serialize};
27
28use crate::core::signatures::ExpectedDomain;
29
30/// The embedded catalog data.
31pub const CATALOG_DATA: &str = include_str!("data/catalog.json");
32
33/// A normalized Workshop client locale, e.g. `en-US`.
34#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
35pub struct Locale(String);
36
37impl Locale {
38    /// Build a locale from a client spelling, normalized to lowercase.
39    pub fn new(value: &str) -> Locale {
40        Locale(value.trim().to_ascii_lowercase())
41    }
42
43    /// The normalized locale string.
44    pub fn as_str(&self) -> &str {
45        &self.0
46    }
47}
48
49impl<'de> Deserialize<'de> for Locale {
50    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
51    where
52        D: Deserializer<'de>,
53    {
54        let value = String::deserialize(deserializer)?;
55        Ok(Self::new(&value))
56    }
57}
58
59impl std::fmt::Display for Locale {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        f.write_str(&self.0)
62    }
63}
64
65/// The kind of a catalog builtin.
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
67pub enum Kind {
68    /// A structural keyword (If, End, Set Global Variable, …).
69    Structural = 0,
70    /// An action function.
71    Action = 1,
72    /// A value function.
73    Value = 2,
74    /// An event.
75    Event = 3,
76    /// An operator token (comparison operators).
77    Operator = 4,
78    /// An enumerated value domain.
79    Enum = 5,
80    /// A settings entry.
81    Setting = 6,
82}
83
84impl Kind {
85    pub const NUM_KINDS: usize = 7;
86
87    pub const fn as_index(self) -> usize {
88        self as usize
89    }
90
91    pub fn as_str(self) -> &'static str {
92        match self {
93            Kind::Structural => "structural",
94            Kind::Action => "action",
95            Kind::Value => "value",
96            Kind::Event => "event",
97            Kind::Operator => "operator",
98            Kind::Enum => "enum",
99            Kind::Setting => "setting",
100        }
101    }
102}
103
104/// Literal substitutions accepted at one parameter position. The authored
105/// literal is kept in WIR; these facts only decide acceptance.
106///
107/// These are deliberately per-parameter facts. They do not establish a
108/// global relationship between Workshop booleans, numbers, arrays, strings,
109/// vectors, or null.
110#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
111#[serde(rename_all = "camelCase")]
112#[non_exhaustive]
113pub struct ParamCoercions {
114    /// Accept `False` as numeric zero.
115    #[serde(default)]
116    pub false_as_number: bool,
117    /// Accept `True` as numeric one.
118    #[serde(default)]
119    pub true_as_number: bool,
120    /// Accept numeric zero as `Null`.
121    #[serde(default)]
122    pub zero_as_null: bool,
123    /// Accept `Vector(0, 0, 0)` as `Null`.
124    #[serde(default)]
125    pub null_vector_as_null: bool,
126    /// Accept `Empty Array` as an empty string.
127    #[serde(default)]
128    pub empty_array_as_string: bool,
129}
130
131/// One catalog builtin.
132#[derive(Debug, Clone)]
133pub struct CatalogEntry {
134    pub id: String,
135    pub kind: Kind,
136    /// Parameter names, when the catalog documents them.
137    pub(crate) params: Vec<String>,
138    /// Reviewed semantic names when they differ from the catalog parameters.
139    pub(crate) param_names: Option<Vec<String>>,
140    /// Reviewed localized spellings for each parameter, parallel to `params`.
141    pub(crate) param_aliases: Vec<HashMap<Locale, Vec<String>>>,
142    /// The canonical enum domain expected at each parameter position, when
143    /// the parameter takes an enumerated value (parallel to `params`).
144    /// `None` for non-enum parameters and for parameters whose accepted
145    /// values span multiple canonical domains. In particular, a filtered
146    /// rule event's `Player` parameter accepts `EventPlayer` members or
147    /// canonical `Hero` members; the WIR [`crate::wir::EventTarget`] carries
148    /// that union explicitly.
149    pub(crate) param_domains: Vec<Option<String>>,
150    /// Default value per parameter position (parallel to `params`),
151    /// resolved when a call omits the argument. See the catalog data
152    /// provenance for the value syntax and source.
153    pub(crate) param_defaults: Vec<Option<String>>,
154    /// Source-backed semantic type per parameter position. `None` means
155    /// the available sources do not establish a narrower type.
156    pub(crate) param_types: Vec<Option<String>>,
157    /// Contextual literal substitutions per parameter position.
158    pub(crate) param_coercions: Vec<Option<ParamCoercions>>,
159    /// Source-backed return type for Value entries. Actions must leave this
160    /// unset; an absent value remains unresolved.
161    pub(crate) return_type: Option<String>,
162    /// Reevaluation coverage for the action's `*Reeval` parameter, keyed by
163    /// enum member id: which parameter positions the selected reevaluation
164    /// member keeps re-evaluating. `None` when the action has no `*Reeval`
165    /// parameter or coverage is not reviewed. Members absent from the map
166    /// are unknown, not empty — an empty vector is a reviewed "reevaluates
167    /// nothing" claim.
168    pub(crate) reevaluation_coverage: Option<BTreeMap<String, Vec<usize>>>,
169    /// Whether the final declared parameter repeats for additional arguments.
170    pub(crate) variadic: bool,
171    pub(crate) aliases: HashMap<Locale, Vec<String>>,
172}
173
174/// A locale-independent identity for a preset used by the Workshop `String`
175/// value. Unlike a custom `Value::String`, this identity must resolve through
176/// reviewed client-locale aliases before it can be parsed or emitted.
177#[derive(Debug, Clone)]
178pub struct LocalizedStringEntry {
179    pub id: String,
180    pub(crate) aliases: HashMap<Locale, Vec<String>>,
181}
182
183fn spelling<'a>(aliases: &'a HashMap<Locale, Vec<String>>, locale: &Locale) -> Option<&'a str> {
184    aliases
185        .get(locale)
186        .and_then(|spellings| spellings.first())
187        .map(String::as_str)
188}
189
190fn spellings_for<'a>(aliases: &'a HashMap<Locale, Vec<String>>, locale: &Locale) -> &'a [String] {
191    aliases.get(locale).map(Vec::as_slice).unwrap_or_default()
192}
193
194impl LocalizedStringEntry {
195    /// The deterministic emitted spelling in `locale`, when mapped.
196    pub fn spelling(&self, locale: &Locale) -> Option<&str> {
197        spelling(&self.aliases, locale)
198    }
199
200    /// All reviewed spellings accepted for this locale.
201    pub fn spellings(&self, locale: &Locale) -> &[String] {
202        spellings_for(&self.aliases, locale)
203    }
204}
205
206impl CatalogEntry {
207    /// The localized spelling of this builtin in `locale`, when declared.
208    pub fn spelling(&self, locale: &Locale) -> Option<&str> {
209        spelling(&self.aliases, locale)
210    }
211
212    /// Every reviewed localized spelling of this builtin, with the first
213    /// spelling reserved for deterministic emission.
214    pub fn spellings(&self, locale: &Locale) -> &[String] {
215        spellings_for(&self.aliases, locale)
216    }
217
218    /// Resolve a canonical or reviewed localized parameter spelling to its
219    /// unambiguous declared position.
220    pub fn resolve_param(&self, locale: &Locale, spelling: &str) -> Option<usize> {
221        let matches = self
222            .params
223            .iter()
224            .enumerate()
225            .filter(|(index, canonical)| {
226                canonical == &spelling
227                    || self
228                        .param_aliases
229                        .get(*index)
230                        .and_then(|aliases| aliases.get(locale))
231                        .is_some_and(|aliases| aliases.iter().any(|alias| alias == spelling))
232            })
233            .map(|(index, _)| index)
234            .collect::<Vec<_>>();
235        (matches.len() == 1).then(|| matches[0])
236    }
237
238    /// The canonical parameter names in declaration order.
239    pub fn params(&self) -> &[String] {
240        &self.params
241    }
242
243    /// The number of declared arguments for this builtin.
244    pub fn param_count(&self) -> usize {
245        self.params.len()
246    }
247
248    /// The reviewed semantic name for an argument position, when declared.
249    pub fn param_name(&self, index: usize) -> Option<&str> {
250        let names = self.param_names.as_deref().unwrap_or(&self.params);
251        param_at(names, index, self.variadic).map(String::as_str)
252    }
253
254    /// The number of arguments that must be present when trailing defaults
255    /// are applied. A missing default in the middle of a signature remains a
256    /// required position; defaults only make the suffix optional.
257    pub fn required_param_count(&self) -> usize {
258        (0..self.params.len())
259            .rev()
260            .find(|index| {
261                self.param_defaults
262                    .get(*index)
263                    .and_then(Option::as_ref)
264                    .is_none()
265            })
266            .map_or(0, |index| index + 1)
267    }
268
269    /// Whether any declared parameter has a default value.
270    pub fn has_param_defaults(&self) -> bool {
271        self.param_defaults.iter().any(Option::is_some)
272    }
273
274    /// The default value for an argument position, when declared.
275    pub fn param_default(&self, index: usize) -> Option<&str> {
276        param_at(&self.param_defaults, index, self.variadic).and_then(Option::as_deref)
277    }
278
279    /// The declared enum domain for an argument position, when one exists.
280    pub fn param_domain(&self, index: usize) -> Option<&str> {
281        param_at(&self.param_domains, index, self.variadic).and_then(Option::as_deref)
282    }
283
284    /// The parameter positions the given `*Reeval` enum member keeps
285    /// re-evaluating on this action, when the action declares reviewed
286    /// reevaluation coverage. `Some(&[])` means the member re-evaluates no
287    /// parameter of this action; `None` means the member's coverage is
288    /// unknown for this action and callers must stay conservative.
289    ///
290    /// `member` is the canonical enum member id (e.g. `COLOR`), the same
291    /// spelling a parsed `Value::Enum` carries.
292    pub fn reevaluation_coverage(&self, member: &str) -> Option<&[usize]> {
293        self.reevaluation_coverage
294            .as_ref()
295            .and_then(|coverage| coverage.get(member).map(Vec::as_slice))
296    }
297
298    /// Whether this action declares reviewed per-parameter reevaluation
299    /// coverage for its `*Reeval` parameter.
300    pub fn has_reevaluation_coverage(&self) -> bool {
301        self.reevaluation_coverage.is_some()
302    }
303
304    /// The source-backed semantic type for an argument position, when
305    /// available. Enum domains remain exposed separately by `param_domain`.
306    pub fn param_type(&self, index: usize) -> Option<&str> {
307        param_at(&self.param_types, index, self.variadic).and_then(Option::as_deref)
308    }
309
310    /// The contextual literal substitutions for an argument position.
311    pub fn param_coercions(&self, index: usize) -> Option<&ParamCoercions> {
312        param_at(&self.param_coercions, index, self.variadic).and_then(Option::as_ref)
313    }
314
315    /// The source-backed return type of a Value, when available.
316    pub fn return_type(&self) -> Option<&str> {
317        self.return_type.as_deref()
318    }
319
320    /// Whether the final declared parameter repeats for additional arguments.
321    pub fn is_variadic(&self) -> bool {
322        self.variadic
323    }
324
325    /// Whether the surface signature's leading `(player, variable)`
326    /// parameters fold into one `PlayerVariable` WIR argument. The declared
327    /// shape is a `Player|Array` parameter directly followed by a `Variable`
328    /// parameter (`stopChasingPlayerVariable`, `chasePlayerVariableAtRate`,
329    /// `chasePlayerVariableOverTime`, `setPlayerVariableAtIndex`,
330    /// `modifyPlayerVariableAtIndex`).
331    pub(crate) fn folds_player_variable_pair(&self) -> bool {
332        self.param_type(0) == Some("Player|Array") && self.param_type(1) == Some("Variable")
333    }
334
335    /// Whether `args`, a WIR argument list for this entry, actually carries
336    /// the folded pair: the signature is foldable and WIR arg 0 is a
337    /// `PlayerVariable` value. An unparseable or hand-built argument list
338    /// that never folded still positions its arguments one-to-one.
339    pub(crate) fn wir_args_folded(
340        &self,
341        program: &crate::wir::Program,
342        args: &[crate::wir::ValueId],
343    ) -> bool {
344        self.folds_player_variable_pair()
345            && matches!(
346                args.first()
347                    .and_then(|arg| program.values.get(*arg))
348                    .map(|node| &node.value),
349                Some(crate::wir::Value::PlayerVariable { .. })
350            )
351    }
352
353    /// The surface parameter index that `args[wir_index]` supplies for this
354    /// entry's signature. Positions map one-to-one unless `args` carries the
355    /// folded `(player, variable)` pair ([`wir_args_folded`]): the folded
356    /// `PlayerVariable` then sits at the `Variable` position and supplies
357    /// the `Player` position through its inner `player` field, and every
358    /// later WIR argument maps to param `wir_index + 1`.
359    pub(crate) fn wir_arg_param_index(
360        &self,
361        program: &crate::wir::Program,
362        args: &[crate::wir::ValueId],
363        wir_index: usize,
364    ) -> usize {
365        wir_index + usize::from(self.wir_args_folded(program, args))
366    }
367}
368
369fn param_at<T>(values: &[T], index: usize, variadic: bool) -> Option<&T> {
370    values
371        .get(index)
372        .or_else(|| variadic.then(|| values.last()).flatten())
373}
374
375/// One enum member within a domain.
376#[derive(Debug, Clone)]
377pub struct EnumMember {
378    pub member: String,
379    pub(crate) aliases: HashMap<Locale, Vec<String>>,
380}
381
382impl EnumMember {
383    /// The localized spelling of this member in `locale`, when declared.
384    pub fn spelling(&self, locale: &Locale) -> Option<&str> {
385        spelling(&self.aliases, locale)
386    }
387
388    /// Every reviewed localized spelling of this enum member, with the first
389    /// spelling reserved for deterministic emission.
390    pub fn spellings(&self, locale: &Locale) -> &[String] {
391        spellings_for(&self.aliases, locale)
392    }
393}
394
395/// One enum value domain (e.g. `Color`, `Beam`).
396#[derive(Debug, Clone)]
397pub struct EnumDomain {
398    pub domain: String,
399    pub(crate) aliases: HashMap<Locale, Vec<String>>,
400    pub members: Vec<EnumMember>,
401}
402
403impl EnumDomain {
404    pub fn spelling(&self, locale: &Locale) -> Option<&str> {
405        spelling(&self.aliases, locale)
406    }
407}
408
409/// Target-format metadata recorded in the catalog.
410#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
411#[non_exhaustive]
412pub struct TargetMeta {
413    pub game: String,
414    pub format: String,
415    pub surface: String,
416}
417
418/// Provenance of the catalog data.
419#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
420#[serde(rename_all = "camelCase")]
421#[non_exhaustive]
422pub struct Provenance {
423    pub generator: String,
424    pub generator_version: String,
425    pub source: String,
426    pub license: String,
427    pub reviewed: bool,
428    /// Additional immutable observations that qualify the dataset source,
429    /// including reviewed spelling conflicts retained as parse aliases.
430    #[serde(default, skip_serializing_if = "Vec::is_empty")]
431    pub source_notes: Vec<String>,
432}
433
434/// Per-locale mapping coverage: how many canonical entries (builtins,
435/// localized preset identities, and enum members) carry a mapping for the
436/// locale out of the declared total.
437#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
438#[non_exhaustive]
439pub struct LocaleCoverage {
440    pub locale: Locale,
441    /// Canonical entries with a declared mapping in this locale.
442    pub mapped: usize,
443    /// Canonical entries (builtins and enum members) declared by the catalog.
444    pub total: usize,
445}
446
447/// The machine-readable catalog identity (ADR-0001 Decision 5): the four
448/// identities that evolve independently — implementation version, catalog
449/// dataset version plus content digest, locale coverage, and target evidence
450/// — plus the data provenance record. Serialized with the ADR's kebab-case
451/// identity names.
452#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
453#[serde(rename_all = "kebab-case")]
454#[non_exhaustive]
455pub struct CatalogIdentity {
456    /// The `workshop-rs` package version (semver); bumped by code changes.
457    pub implementation_version: String,
458    /// The catalog dataset version; bumped by any dataset change.
459    pub catalog_version: String,
460    /// The deterministic content digest (sha256 hex) computed by the
461    /// pipeline; `None` when the data does not declare one.
462    pub catalog_digest: Option<String>,
463    /// Declared locales with per-locale mapping counts.
464    pub locale_coverage: Vec<LocaleCoverage>,
465    /// The declared target surface.
466    pub target: TargetMeta,
467    /// The provenance record of the catalog data.
468    pub provenance: Provenance,
469}
470
471pub(crate) type MemberIndexMap = HashMap<String, HashMap<String, (usize, usize)>>;
472
473/// The validated canonical Workshop catalog.
474#[derive(Debug, Clone)]
475pub struct Catalog {
476    detection_index: Option<detect::AliasIndex>,
477    pub(crate) schema_version: u32,
478    /// The declared locales, normalized; the first one is the primary
479    /// locale and must be fully covered.
480    pub(crate) locales: Vec<Locale>,
481    pub(crate) target: TargetMeta,
482    pub(crate) provenance: Provenance,
483    /// The catalog dataset version (ADR-0001 `catalog-version`).
484    pub(crate) catalog_version: String,
485    /// The declared content digest (sha256 hex), verified at load when
486    /// present (ADR-0001 `catalog-version`).
487    pub(crate) catalog_digest: Option<String>,
488    pub(crate) entries: Vec<CatalogEntry>,
489    pub(crate) localized_strings: Vec<LocalizedStringEntry>,
490    pub(crate) enums: Vec<EnumDomain>,
491    pub(crate) by_id: [HashMap<String, usize>; Kind::NUM_KINDS],
492    pub(crate) alias_to_entry: HashMap<Locale, [HashMap<String, usize>; Kind::NUM_KINDS]>,
493    pub(crate) localized_string_by_id: HashMap<String, usize>,
494    pub(crate) localized_string_alias: HashMap<Locale, HashMap<String, usize>>,
495    pub(crate) enum_by_domain: HashMap<String, usize>,
496    pub(crate) enum_alias_to_domain: HashMap<Locale, HashMap<String, String>>,
497    pub(crate) enum_alias_to_member: HashMap<Locale, MemberIndexMap>,
498    pub(crate) bare_member_index: HashMap<Locale, HashMap<String, Vec<(String, String)>>>,
499}
500
501/// Catalog loading and digest machinery lives in `load.rs`.
502mod load;
503
504pub use load::{build_canonical, canonicalize, content_digest};
505
506impl Catalog {
507    pub(crate) fn schema_version(&self) -> u32 {
508        self.schema_version
509    }
510
511    /// The declared locales, normalized; the first one is the primary locale.
512    pub fn locales(&self) -> &[Locale] {
513        &self.locales
514    }
515
516    /// The primary locale: the first declared one, whose mapping surface is
517    /// complete (`en-US` in the committed catalog).
518    pub fn primary_locale(&self) -> &Locale {
519        &self.locales[0]
520    }
521
522    /// Whether a locale is declared by the catalog.
523    pub fn supports(&self, locale: &Locale) -> bool {
524        self.locales.contains(locale)
525    }
526
527    /// The catalog dataset version (ADR-0001 `catalog-version`).
528    pub fn catalog_version(&self) -> &str {
529        &self.catalog_version
530    }
531
532    /// The declared content digest (sha256 hex) of the catalog dataset,
533    /// verified at load; `None` for data that declares none.
534    pub fn catalog_digest(&self) -> Option<&str> {
535        self.catalog_digest.as_deref()
536    }
537
538    /// The `workshop-rs` package version (ADR-0001 `implementation-version`).
539    pub fn implementation_version() -> &'static str {
540        env!("CARGO_PKG_VERSION")
541    }
542
543    /// The machine-readable catalog identity: implementation version, catalog
544    /// version + digest, locale coverage, target evidence, and provenance.
545    pub fn identity(&self) -> CatalogIdentity {
546        CatalogIdentity {
547            implementation_version: Self::implementation_version().to_string(),
548            catalog_version: self.catalog_version.clone(),
549            catalog_digest: self.catalog_digest.clone(),
550            locale_coverage: self
551                .locales
552                .iter()
553                .map(|locale| self.locale_coverage(locale))
554                .collect(),
555            target: self.target.clone(),
556            provenance: self.provenance.clone(),
557        }
558    }
559
560    /// The mapping coverage of one declared locale: mapped entries out of the
561    /// declared total (builtins, localized preset identities, and enum members).
562    /// The primary locale is
563    /// always complete; other locales may be partially covered.
564    pub fn locale_coverage(&self, locale: &Locale) -> LocaleCoverage {
565        let member_total: usize = self.enums.iter().map(|domain| domain.members.len()).sum();
566        let total = self.entries.len() + self.localized_strings.len() + member_total;
567        let mapped = self
568            .entries
569            .iter()
570            .filter(|entry| entry.aliases.contains_key(locale))
571            .count()
572            + self
573                .localized_strings
574                .iter()
575                .filter(|entry| entry.aliases.contains_key(locale))
576                .count()
577            + self
578                .enums
579                .iter()
580                .flat_map(|domain| &domain.members)
581                .filter(|member| member.aliases.contains_key(locale))
582                .count();
583        LocaleCoverage {
584            locale: locale.clone(),
585            mapped,
586            total,
587        }
588    }
589
590    /// The mapping coverage of every declared locale, in declaration order.
591    pub fn locale_coverage_all(&self) -> Vec<LocaleCoverage> {
592        self.locales
593            .iter()
594            .map(|locale| self.locale_coverage(locale))
595            .collect()
596    }
597
598    /// The builtin with the given canonical id and kind.
599    pub fn entry(&self, kind: Kind, id: &str) -> Option<&CatalogEntry> {
600        self.by_id[kind.as_index()]
601            .get(id)
602            .map(|i| &self.entries[*i])
603    }
604
605    /// Resolve a localized spelling to its canonical builtin.
606    pub fn resolve(&self, kind: Kind, locale: &Locale, spelling: &str) -> Option<&CatalogEntry> {
607        self.alias_to_entry
608            .get(locale)
609            .and_then(|by_kind| by_kind[kind.as_index()].get(spelling))
610            .map(|i| &self.entries[*i])
611    }
612
613    /// The localized spelling of a canonical builtin id.
614    pub fn spelling(&self, kind: Kind, locale: &Locale, id: &str) -> Option<&str> {
615        self.entry(kind, id)?.spelling(locale)
616    }
617
618    /// Every entry of a kind, in catalog order.
619    pub fn entries_of(&self, kind: Kind) -> impl Iterator<Item = &CatalogEntry> {
620        self.entries.iter().filter(move |entry| entry.kind == kind)
621    }
622
623    /// Resolve a localized preset spelling to its stable identity.
624    pub fn resolve_localized_string(
625        &self,
626        locale: &Locale,
627        spelling: &str,
628    ) -> Option<&LocalizedStringEntry> {
629        self.localized_string_alias
630            .get(locale)
631            .and_then(|map| map.get(spelling))
632            .map(|index| &self.localized_strings[*index])
633    }
634
635    /// Resolve the emitted spelling of a localized preset identity.
636    pub fn localized_string_spelling(&self, locale: &Locale, id: &str) -> Option<&str> {
637        self.localized_string_by_id
638            .get(id)
639            .and_then(|i| self.localized_strings.get(*i))
640            .and_then(|entry| entry.spelling(locale))
641    }
642
643    /// Every reviewed localized preset identity, in catalog order.
644    pub fn localized_strings(&self) -> impl Iterator<Item = &LocalizedStringEntry> {
645        self.localized_strings.iter()
646    }
647
648    /// The total number of builtin entries.
649    pub fn entry_count(&self) -> usize {
650        self.entries.len()
651    }
652
653    /// The number of enum domains.
654    pub fn enum_domains_count(&self) -> usize {
655        self.enums.len()
656    }
657
658    /// The enum domain with the given name.
659    pub fn enum_domain(&self, domain: &str) -> Option<&EnumDomain> {
660        self.enum_by_domain.get(domain).map(|i| &self.enums[*i])
661    }
662
663    /// Resolve a localized enum-domain spelling to its canonical domain id.
664    pub fn resolve_enum_domain(&self, locale: &Locale, spelling: &str) -> Option<&str> {
665        self.enum_by_domain
666            .get_key_value(spelling)
667            .map(|(domain, _)| domain.as_str())
668            .or_else(|| {
669                self.enum_alias_to_domain
670                    .get(locale)
671                    .and_then(|map| map.get(spelling))
672                    .map(String::as_str)
673            })
674    }
675
676    /// Every enum domain, in catalog order.
677    pub fn enum_domains(&self) -> impl Iterator<Item = &EnumDomain> {
678        self.enums.iter()
679    }
680
681    /// Resolve a localized enum member spelling to `(domain, canonical member)`.
682    pub fn resolve_enum_member(
683        &self,
684        domain: &str,
685        locale: &Locale,
686        spelling: &str,
687    ) -> Option<(String, String)> {
688        let &(domain_index, member_index) = self
689            .enum_alias_to_member
690            .get(locale)?
691            .get(domain)?
692            .get(spelling)?;
693        Some((
694            domain.to_string(),
695            self.enums[domain_index].members[member_index]
696                .member
697                .clone(),
698        ))
699    }
700
701    /// The localized spelling of a canonical enum member.
702    pub fn enum_spelling(&self, domain: &str, locale: &Locale, member: &str) -> Option<&str> {
703        let domain_index = self.enum_by_domain.get(domain)?;
704        let domain = &self.enums[*domain_index];
705        domain
706            .members
707            .iter()
708            .find(|candidate| candidate.member == member)?
709            .spelling(locale)
710    }
711
712    /// Resolve a canonical enum member through the locale boundary, including
713    /// reviewed partial locale spellings that are not yet part of the full
714    /// catalog locale set.
715    pub fn localized_enum_spelling(
716        &self,
717        domain: &str,
718        locale: &Locale,
719        member: &str,
720    ) -> Option<&str> {
721        self.enum_spelling(domain, locale, member).or_else(|| {
722            (domain == "Color" && member == "WHITE").then_some(match locale.as_str() {
723                "de-de" => "Weiß",
724                "es-es" | "es-mx" => "Blanco",
725                "fr-fr" => "Blanc",
726                "it-it" => "Bianco",
727                "ja-jp" => "白",
728                "ko-kr" => "흰색",
729                "pl-pl" => "Biały",
730                "pt-br" => "Branco",
731                "ru-ru" => "Белый",
732                "th-th" => "สีขาว",
733                "tr-tr" => "Beyaz",
734                "zh-tw" => "白色",
735                _ => return None,
736            })
737        })
738    }
739
740    /// Every `(domain, canonical member)` match for a bare (domain-less)
741    /// localized member spelling. Returns all matches so callers can report
742    /// ambiguity; a well-formed catalog has at most one meaningful match for
743    /// a given spelling.
744    pub fn bare_member_matches(&self, locale: &Locale, spelling: &str) -> Vec<(String, String)> {
745        self.bare_member_index
746            .get(locale)
747            .and_then(|map| map.get(spelling))
748            .cloned()
749            .unwrap_or_default()
750    }
751
752    /// The canonical ids and `locale` spellings of `kind` entries, for
753    /// nearest-candidate diagnostics that reject in the canonical space
754    /// (validation and emission, where the rejected name is an id or an
755    /// authored spelling).
756    pub(crate) fn canonical_spellings<'a>(
757        &'a self,
758        kind: Kind,
759        locale: &'a Locale,
760    ) -> impl Iterator<Item = &'a str> {
761        self.entries_of(kind).flat_map(move |entry| {
762            std::iter::once(entry.id.as_str())
763                .chain(entry.spellings(locale).iter().map(String::as_str))
764        })
765    }
766
767    /// The emitted form of `member_spelling` in `domain` under `locale`:
768    /// constructor-form domains (`Hero`, `Button`, `Color`, `Map`) write
769    /// `Domain(Member)` in every position and locale; every other domain
770    /// writes the member bare (docs/wrapper-forms.md).
771    pub(crate) fn enum_member_form(
772        &self,
773        domain: &str,
774        member_spelling: &str,
775        locale: &Locale,
776    ) -> String {
777        if matches!(domain, "Hero" | "Button" | "Color" | "Map") {
778            let domain_display = self
779                .enum_domain(domain)
780                .and_then(|entry| entry.spelling(locale))
781                .unwrap_or(domain);
782            format!("{domain_display}({member_spelling})")
783        } else {
784            member_spelling.to_string()
785        }
786    }
787
788    /// The canonical ids and `locale` spellings of `domain` members, for
789    /// nearest-candidate diagnostics that reject in the canonical space.
790    pub(crate) fn canonical_member_spellings<'a>(
791        &'a self,
792        domain: &'a str,
793        locale: &'a Locale,
794    ) -> impl Iterator<Item = &'a str> {
795        self.enum_domain(domain)
796            .into_iter()
797            .flat_map(move |domain| {
798                domain.members.iter().flat_map(move |member| {
799                    std::iter::once(member.member.as_str())
800                        .chain(member.spellings(locale).iter().map(String::as_str))
801                })
802            })
803    }
804}
805
806/// The catalog is the canonical source of expected enum domains for the
807/// Workshop surface it documents: `expected_domain(catalog_id, arg_index)`
808/// answers the domain declared for that parameter position (e.g. `createHudText`
809/// argument 9 is `HudReeval`), so the Workshop parser can resolve bare enum
810/// members that are ambiguous across domains (e.g. `Visible To and String`).
811/// Positions without a documented domain answer `None`.
812impl ExpectedDomain for Catalog {
813    fn expected_domain(&self, catalog_id: &str, arg_index: usize) -> Option<&str> {
814        for kind in [Kind::Action, Kind::Value] {
815            if let Some(entry) = self.entry(kind, catalog_id) {
816                if let Some(domain) = entry.param_domain(arg_index) {
817                    return Some(domain);
818                }
819            }
820        }
821        None
822    }
823}