Skip to main content

opy_rs/manifest/
mod.rs

1//! The OPY semantic compatibility manifest (issue #109).
2//!
3//! This module owns the Wright-authored, reference-validated semantic table
4//! that the frontend resolves builtin names, member functions, receiver
5//! categories, signatures/arity, parameter enum-domain identities, and
6//! non-contextual source aliases against — the authoritative replacement for
7//! the hardcoded `KNOWN_ENUMS` table and the semantic catalog-coverage gap
8//! behind `unknown-action`/`unknown-value`/`unsupported-member` emission
9//! failures.
10//!
11//! * The data lives in [`data/manifest.json`](data/manifest.json) (schema
12//!   v1, per the compatibility-manifest spec).
13//! * Every entry records the pinned-oracle probe that validates it
14//!   (`probes/probes.json`); `probes/validate.py` runs each probe against the
15//!   pinned OverPy 9.7.10 oracle and verifies accept/reject, emission hash,
16//!   and diagnostic category deterministically.
17//! * `catalogId` links each entry to the Workshop emission catalog by
18//!   canonical identity without duplicating localization/output spelling
19//!   data. The wright repository cross-checks every declared id against its
20//!   emission catalog; opy-rs does not copy the catalog itself.
21//!
22//! Ownership boundary: the **function**, **alias**, and **module** tables are
23//! OPY *source-language API* metadata (OverPy's documented language API;
24//! Wright-authored, probe-validated) and are not Workshop content data.
25//! Workshop *content/catalog* data — enum member lists, settings keys,
26//! mode/team/hero/map names — is Workshop-owned and is not carried here:
27//! `param.domain` and the contextual-domain machinery are catalog *identity*
28//! links only (no member validation), and validation that would need the
29//! canonical Workshop enum catalog is `lowering-dependent` (issue #8),
30//! never approximated.
31//!
32//! The manifest is language-compatibility metadata, not runtime content data
33//! (issue #96 stays deferred), and it is Wright-authored data validated
34//! against observed oracle behavior — never a mechanical conversion of
35//! OverPy's GPL-3.0 data files (ADR-0004, `docs/licensing.md` in the wright
36//! repository).
37
38use std::collections::{HashMap, HashSet};
39use std::sync::OnceLock;
40
41use serde::{Deserialize, Serialize};
42
43/// The embedded schema-v1 manifest data.
44pub const MANIFEST_DATA: &str = include_str!("data/manifest.json");
45
46/// The embedded probe evidence record for the manifest data.
47pub const PROBES_DATA: &str = include_str!("probes/probes.json");
48
49/// The pinned reference identity the manifest data is validated against.
50#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
51pub struct Reference {
52    pub name: String,
53    pub version: String,
54    #[serde(rename = "contentCommit")]
55    pub content_commit: String,
56    pub integrity: String,
57}
58
59/// Provenance of the manifest data.
60#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
61pub struct Provenance {
62    pub generator: String,
63    pub license: String,
64    pub reviewed: bool,
65}
66
67/// The kind of a builtin function entry.
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
69#[serde(rename_all = "camelCase")]
70pub enum FunctionKind {
71    /// A generic action (`chaseOverTime(...)` as a statement).
72    Action,
73    /// A generic value (`isGameInProgress()` in an expression).
74    Value,
75    /// An action called on a receiver (`eventPlayer.setMoveSpeed(100)`).
76    MemberAction,
77    /// A value called on a receiver (`eventPlayer.isAlive()`).
78    MemberValue,
79}
80
81/// How the frontend-owned function identity connects to Workshop lowering.
82///
83/// `canonical` entries carry a `catalogId`; the other variants are explicit
84/// reasons why a source-level function does not have a direct catalog entry.
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
86#[serde(rename_all = "kebab-case")]
87pub enum CatalogLink {
88    #[default]
89    Canonical,
90    SpecialLowering,
91    LegacyAlias,
92    CatalogGap,
93}
94
95impl FunctionKind {
96    /// Whether this kind is an action (statement-position builtin).
97    pub fn is_action(self) -> bool {
98        matches!(self, FunctionKind::Action | FunctionKind::MemberAction)
99    }
100
101    /// Whether this kind is a value (expression-position builtin).
102    pub fn is_value(self) -> bool {
103        matches!(self, FunctionKind::Value | FunctionKind::MemberValue)
104    }
105
106    /// Whether this kind is a receiver member function.
107    pub fn is_member(self) -> bool {
108        matches!(self, FunctionKind::MemberAction | FunctionKind::MemberValue)
109    }
110}
111
112/// The declared receiver category of a member function.
113///
114/// `Player` is the metadata category for player-oriented members (the pinned
115/// reference does not type-check those receivers, so the frontend does not
116/// reject them); `Variable` and `String` are enforced where the reference
117/// semantics are clear (`.append` requires an assignable receiver, `.format`
118/// requires a string literal).
119#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
120#[serde(rename_all = "PascalCase")]
121pub enum ReceiverCategory {
122    Player,
123    Variable,
124    String,
125    Vector,
126    Any,
127}
128
129impl ReceiverCategory {
130    /// A human-readable description of the category for diagnostics.
131    pub fn describe(self) -> &'static str {
132        match self {
133            ReceiverCategory::Player => "a player-valued expression",
134            ReceiverCategory::Variable => "an assignable variable",
135            ReceiverCategory::String => "a string literal",
136            ReceiverCategory::Vector => "a vector-valued expression",
137            ReceiverCategory::Any => "any expression",
138        }
139    }
140}
141
142/// A parameter default that the frontend expands: a function call, enum
143/// member (`"MEMBER"`), or scalar (`0.016`).
144#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
145#[serde(untagged)]
146pub enum ParamDefault {
147    Call { call: String },
148    EnumMember(String),
149    Number(f64),
150}
151
152/// One ordered parameter of a function entry.
153#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
154#[serde(rename_all = "camelCase")]
155pub struct Param {
156    pub name: String,
157    /// The enum domain this parameter requires, when it is an enum argument.
158    #[serde(default)]
159    pub domain: Option<String>,
160    /// An explicit default the frontend may expand; see [`ParamDefault`].
161    #[serde(default)]
162    pub default: Option<ParamDefault>,
163    /// Whether the argument is omittable without an emitted expansion
164    /// (`"optional": true`; the reference accepts the short form).
165    #[serde(default)]
166    pub optional: bool,
167    /// Whether the argument must be passed as a keyword (`name = expr`):
168    /// the reference `chase` form requires its 3rd argument to be
169    /// `rate = ...` or `duration = ...` (issue #110).
170    #[serde(default)]
171    pub keyword_only: bool,
172    /// Whether the argument can only be passed positionally (keyword
173    /// binding is rejected): the reference `chase` form's leading arguments
174    /// (issue #110).
175    #[serde(default)]
176    pub positional_only: bool,
177    /// Additional accepted keyword spellings for this parameter (the
178    /// reference `chase` form accepts both `rate` and `duration` for its
179    /// 3rd argument).
180    #[serde(default)]
181    pub alternate_names: Vec<String>,
182    /// Whether the argument must be a variable reference (a global variable
183    /// or a player variable); the chase family requires a variable first
184    /// argument to select the global/player emission form.
185    #[serde(default)]
186    pub variable: bool,
187}
188
189/// A call-context restriction on a function entry.
190#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
191#[serde(rename_all = "camelCase")]
192pub enum FunctionContext {
193    /// Only valid as a `for ... in` iterable (`range`; the pinned reference
194    /// rejects standalone `range` calls).
195    ForIterable,
196}
197
198/// One contextual enum-domain selection: the `chase` dispatch (issue #110).
199///
200/// The reference `chase` form binds its 4th argument as a member of a
201/// merged `ChaseReeval` domain that does not exist as a standalone enum:
202/// the keyword name used for the `by` parameter selects the concrete domain
203/// and the function the call lowers to (`rate` → `ChaseRateReeval` /
204/// `chaseAtRate`, `duration` → `ChaseTimeReeval` / `chaseOverTime`).
205#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
206#[serde(rename_all = "camelCase")]
207pub struct ContextualDomain {
208    /// The contextual (merged) domain name; never resolvable outside the
209    /// declaring function's signature context.
210    pub domain: String,
211    /// The parameter whose bound keyword name selects the option.
212    pub by: String,
213    /// The options keyed by the accepted keyword spellings of the `by`
214    /// parameter.
215    pub options: std::collections::BTreeMap<String, ContextualDomainOption>,
216}
217
218/// One contextual-domain option: the concrete enum domain and the function
219/// name the call lowers to.
220#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
221#[serde(rename_all = "camelCase")]
222pub struct ContextualDomainOption {
223    pub domain: String,
224    pub target: String,
225}
226
227/// One builtin function entry (generic action/value or member function).
228#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
229#[serde(rename_all = "camelCase")]
230pub struct Function {
231    pub id: String,
232    pub kind: FunctionKind,
233    /// The receiver category of member functions.
234    #[serde(default)]
235    pub receiver: Option<ReceiverCategory>,
236    #[serde(default)]
237    pub params: Vec<Param>,
238    /// Whether the argument count is unbounded (`.format` placeholders).
239    #[serde(default)]
240    pub unbounded: bool,
241    /// Whether keyword arguments are accepted (`name = expr`). Defaults to
242    /// `true` (the reference's `parseArgs` applies to every workshop
243    /// function); entries the reference routes around that mechanism
244    /// (`range`, `random.*`, `.format`) declare `"keywordArgs": false`
245    /// (issue #110).
246    #[serde(default = "default_keyword_args")]
247    pub keyword_args: bool,
248    /// The contextual enum-domain dispatch (the `chase` form), when this
249    /// entry has one.
250    #[serde(default)]
251    pub contextual_domain: Option<ContextualDomain>,
252    #[serde(default)]
253    pub context: Option<FunctionContext>,
254    /// The canonical Workshop catalog id this entry emits through; absent
255    /// when emission is special-cased or not yet catalog-covered.
256    #[serde(default)]
257    #[serde(rename = "catalogId")]
258    pub catalog_id: Option<String>,
259    /// The explicit reason a source-level function has no direct catalog id.
260    #[serde(default)]
261    pub catalog_link: CatalogLink,
262    /// The probe ids that validate this entry against the pinned oracle.
263    #[serde(default)]
264    pub evidence: Vec<String>,
265}
266
267impl Function {
268    /// The (minimum, maximum) argument count: the first parameter with a
269    /// default makes every following parameter optional; `unbounded` entries
270    /// accept any count.
271    pub fn arity_bounds(&self) -> (usize, Option<usize>) {
272        if self.unbounded {
273            return (0, None);
274        }
275        let first_default = self
276            .params
277            .iter()
278            .position(|param| param.default.is_some() || param.optional);
279        let min = first_default.unwrap_or(self.params.len());
280        (min, Some(self.params.len()))
281    }
282}
283
284fn default_keyword_args() -> bool {
285    true
286}
287
288/// A non-contextual source alias: a pure name rewrite to a declared entry.
289#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
290#[serde(rename_all = "camelCase")]
291pub struct Alias {
292    pub source: String,
293    pub target: String,
294    pub kind: AliasKind,
295    #[serde(default)]
296    pub evidence: Vec<String>,
297}
298
299/// The alias target class; `functionAlias` targets a generic function,
300/// `memberAlias` a member function.
301#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
302#[serde(rename_all = "camelCase")]
303pub enum AliasKind {
304    FunctionAlias,
305    MemberAlias,
306}
307
308/// One recorded probe in the embedded evidence record.
309#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
310#[serde(rename_all = "camelCase")]
311pub struct Probe {
312    pub id: String,
313    pub source: String,
314    pub sha256: String,
315    pub expect: String,
316    #[serde(default)]
317    pub output_sha256: Option<String>,
318    #[serde(default)]
319    pub diagnostic_contains: Option<String>,
320}
321
322/// A validation failure while loading the manifest.
323#[derive(Debug, Clone, PartialEq, Eq)]
324pub struct ManifestError(pub String);
325
326impl std::fmt::Display for ManifestError {
327    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
328        f.write_str(&self.0)
329    }
330}
331
332impl std::error::Error for ManifestError {}
333
334/// The validated OPY semantic compatibility manifest.
335#[derive(Debug, Clone)]
336pub struct Manifest {
337    pub schema_version: u32,
338    pub reference: Reference,
339    pub functions: Vec<Function>,
340    pub aliases: Vec<Alias>,
341    pub provenance: Provenance,
342    /// The recorded probe evidence (`probes/probes.json`).
343    pub probes: Vec<Probe>,
344    by_function: HashMap<String, usize>,
345    by_member: HashMap<String, usize>,
346    alias_by_source: HashMap<String, usize>,
347    /// The declared enum-domain identities: every `param.domain` and
348    /// contextual option domain in the function table. Identity links only —
349    /// member lists are Workshop-owned catalog content and are not carried
350    /// here (lowering-dependent validation, #8).
351    domain_identities: HashSet<String>,
352}
353
354#[derive(Serialize, Deserialize)]
355#[serde(rename_all = "camelCase")]
356struct ManifestFile {
357    schema_version: u32,
358    reference: Reference,
359    #[serde(default)]
360    functions: Vec<Function>,
361    #[serde(default)]
362    aliases: Vec<Alias>,
363    provenance: Provenance,
364}
365
366#[derive(Serialize, Deserialize)]
367#[serde(rename_all = "camelCase")]
368struct ProbesFile {
369    schema_version: u32,
370    #[serde(default)]
371    probes: Vec<Probe>,
372}
373
374impl Manifest {
375    /// Parse and validate manifest data plus its probe evidence record.
376    pub fn load(manifest_json: &str, probes_json: &str) -> Result<Manifest, ManifestError> {
377        let file: ManifestFile = serde_json::from_str(manifest_json)
378            .map_err(|error| ManifestError(format!("manifest data: {error}")))?;
379        if file.schema_version != 1 {
380            return Err(ManifestError(format!(
381                "unsupported manifest schemaVersion {}",
382                file.schema_version
383            )));
384        }
385        let probes_file: ProbesFile = serde_json::from_str(probes_json)
386            .map_err(|error| ManifestError(format!("probes data: {error}")))?;
387        if probes_file.schema_version != 1 {
388            return Err(ManifestError(format!(
389                "unsupported probes schemaVersion {}",
390                probes_file.schema_version
391            )));
392        }
393        let mut manifest = Manifest {
394            schema_version: file.schema_version,
395            reference: file.reference.clone(),
396            functions: Vec::new(),
397            aliases: Vec::new(),
398            provenance: file.provenance.clone(),
399            probes: probes_file.probes,
400            by_function: HashMap::new(),
401            by_member: HashMap::new(),
402            alias_by_source: HashMap::new(),
403            domain_identities: HashSet::new(),
404        };
405        manifest.validate(file)?;
406        Ok(manifest)
407    }
408
409    fn validate(&mut self, file: ManifestFile) -> Result<(), ManifestError> {
410        // Probe ids must be unique and must record the accept probes the
411        // entries reference.
412        let mut probes: HashMap<&str, &Probe> = HashMap::new();
413        for probe in &self.probes {
414            if probes.insert(&probe.id, probe).is_some() {
415                return Err(ManifestError(format!("duplicate probe id '{}'", probe.id)));
416            }
417        }
418
419        // Functions: unique ids, member-only receiver/kind combinations,
420        // declared enum domains, declared enum-default members, and probe
421        // evidence that records acceptance.
422        for function in &file.functions {
423            if self.by_function.contains_key(&function.id) {
424                return Err(ManifestError(format!(
425                    "duplicate function id '{}'",
426                    function.id
427                )));
428            }
429            match function.kind {
430                FunctionKind::MemberAction | FunctionKind::MemberValue => {
431                    if function.receiver.is_none() {
432                        return Err(ManifestError(format!(
433                            "member function '{}' declares no receiver category",
434                            function.id
435                        )));
436                    }
437                }
438                FunctionKind::Action | FunctionKind::Value => {
439                    if function.receiver.is_some() {
440                        return Err(ManifestError(format!(
441                            "non-member function '{}' declares a receiver category",
442                            function.id
443                        )));
444                    }
445                }
446            }
447            for param in function.params.iter() {
448                if let Some(domain) = &param.domain {
449                    // A parameter may declare the function's own contextual
450                    // domain (`chase`'s `ChaseReeval`): it resolves only in
451                    // this signature's context and is not a standalone
452                    // identity.
453                    let is_contextual = function
454                        .contextual_domain
455                        .as_ref()
456                        .is_some_and(|contextual| &contextual.domain == domain);
457                    if !is_contextual {
458                        self.domain_identities.insert(domain.clone());
459                    }
460                } else if matches!(param.default, Some(ParamDefault::EnumMember(_))) {
461                    return Err(ManifestError(format!(
462                        "function '{}' parameter '{}' has an enum-member default but no \
463                         declared domain",
464                        function.id, param.name
465                    )));
466                }
467                if param.keyword_only && param.positional_only {
468                    return Err(ManifestError(format!(
469                        "function '{}' parameter '{}' cannot be both keyword-only and \
470                         positional-only",
471                        function.id, param.name
472                    )));
473                }
474                for alternate in &param.alternate_names {
475                    if alternate == &param.name {
476                        return Err(ManifestError(format!(
477                            "function '{}' parameter '{}' repeats its name as an \
478                             alternate keyword spelling",
479                            function.id, param.name
480                        )));
481                    }
482                    if function.params.iter().any(|other| {
483                        !std::ptr::eq(other, param)
484                            && (&other.name == alternate
485                                || other.alternate_names.contains(alternate))
486                    }) {
487                        return Err(ManifestError(format!(
488                            "function '{}' alternate keyword spelling '{alternate}' \
489                             collides with another parameter",
490                            function.id
491                        )));
492                    }
493                }
494            }
495            match (&function.catalog_id, function.catalog_link) {
496                (Some(_), CatalogLink::Canonical)
497                | (None, CatalogLink::SpecialLowering)
498                | (None, CatalogLink::LegacyAlias)
499                | (None, CatalogLink::CatalogGap) => {}
500                (Some(id), link) => {
501                    return Err(ManifestError(format!(
502                        "function '{}' has catalogId '{id}' but catalogLink is {:?}",
503                        function.id, link
504                    )));
505                }
506                (None, CatalogLink::Canonical) => {
507                    return Err(ManifestError(format!(
508                        "function '{}' has no catalogId or explicit catalogLink reason",
509                        function.id
510                    )));
511                }
512            }
513            if let Some(contextual) = &function.contextual_domain {
514                let by_param = function
515                    .params
516                    .iter()
517                    .find(|param| param.name == contextual.by)
518                    .ok_or_else(|| {
519                        ManifestError(format!(
520                            "function '{}' contextual domain '{}' references unknown \
521                             selector parameter '{}'",
522                            function.id, contextual.domain, contextual.by
523                        ))
524                    })?;
525                let contextual_param = function
526                    .params
527                    .iter()
528                    .find(|param| param.domain.as_deref() == Some(contextual.domain.as_str()))
529                    .ok_or_else(|| {
530                        ManifestError(format!(
531                            "function '{}' contextual domain '{}' has no parameter \
532                             declaring that domain",
533                            function.id, contextual.domain
534                        ))
535                    })?;
536                let _ = contextual_param;
537                let mut spellings = vec![by_param.name.clone()];
538                spellings.extend(by_param.alternate_names.iter().cloned());
539                for (keyword, option) in &contextual.options {
540                    if !spellings.contains(keyword) {
541                        return Err(ManifestError(format!(
542                            "function '{}' contextual option '{keyword}' is not a \
543                             keyword spelling of selector parameter '{}'",
544                            function.id, by_param.name
545                        )));
546                    }
547                    // The option's concrete domain is a catalog identity link
548                    // (the domain the selected member/emission belongs to);
549                    // member lists are not carried here.
550                    self.domain_identities.insert(option.domain.clone());
551                }
552            }
553            self.check_evidence(&function.id, &function.evidence, &probes)?;
554            if function.kind.is_member() {
555                self.by_member
556                    .insert(function.id.clone(), self.functions.len());
557            } else {
558                self.by_function
559                    .insert(function.id.clone(), self.functions.len());
560            }
561            self.functions.push(function.clone());
562        }
563
564        // Aliases: unique sources, declared targets of the matching class,
565        // no collision with declared function ids.
566        for alias in &file.aliases {
567            if self.alias_by_source.contains_key(&alias.source) {
568                return Err(ManifestError(format!(
569                    "duplicate alias source '{}'",
570                    alias.source
571                )));
572            }
573            if self.by_function.contains_key(&alias.source)
574                || self.by_member.contains_key(&alias.source)
575            {
576                return Err(ManifestError(format!(
577                    "alias source '{}' collides with a declared function",
578                    alias.source
579                )));
580            }
581            match alias.kind {
582                AliasKind::FunctionAlias => {
583                    if self.function(&alias.target).is_none() {
584                        return Err(ManifestError(format!(
585                            "alias '{}' targets '{}' which is not a generic function",
586                            alias.source, alias.target
587                        )));
588                    }
589                }
590                AliasKind::MemberAlias => {
591                    if self.member(&alias.target).is_none() {
592                        return Err(ManifestError(format!(
593                            "alias '{}' targets '{}' which is not a member function",
594                            alias.source, alias.target
595                        )));
596                    }
597                }
598            }
599            self.check_evidence(&alias.source, &alias.evidence, &probes)?;
600            self.alias_by_source
601                .insert(alias.source.clone(), self.aliases.len());
602            self.aliases.push(alias.clone());
603        }
604
605        Ok(())
606    }
607
608    fn check_evidence(
609        &self,
610        owner: &str,
611        evidence: &[String],
612        probes: &HashMap<&str, &Probe>,
613    ) -> Result<(), ManifestError> {
614        if evidence.is_empty() {
615            return Err(ManifestError(format!(
616                "entry '{owner}' records no oracle probe evidence"
617            )));
618        }
619        for probe_id in evidence {
620            let probe = probes.get(probe_id.as_str()).ok_or_else(|| {
621                ManifestError(format!(
622                    "entry '{owner}' references undeclared probe '{probe_id}'"
623                ))
624            })?;
625            if probe.expect != "success" {
626                return Err(ManifestError(format!(
627                    "entry '{owner}' references probe '{probe_id}' which does not record \
628                     oracle acceptance"
629                )));
630            }
631        }
632        Ok(())
633    }
634
635    /// The built-in manifest, loaded once from the embedded data.
636    pub fn builtin() -> Result<&'static Manifest, ManifestError> {
637        static MANIFEST: OnceLock<Result<Manifest, ManifestError>> = OnceLock::new();
638        MANIFEST
639            .get_or_init(|| Manifest::load(MANIFEST_DATA, PROBES_DATA))
640            .as_ref()
641            .map_err(Clone::clone)
642    }
643
644    /// A generic (non-member) function by source name, alias-aware.
645    pub fn resolve_function(&self, name: &str) -> Option<&Function> {
646        self.function(name).or_else(|| {
647            let alias = self.alias_by_source.get(name)?;
648            let alias = &self.aliases[*alias];
649            (alias.kind == AliasKind::FunctionAlias)
650                .then(|| self.function(&alias.target))
651                .flatten()
652        })
653    }
654
655    /// A member function by source name, alias-aware.
656    pub fn resolve_member(&self, name: &str) -> Option<&Function> {
657        self.member(name).or_else(|| {
658            let alias = self.alias_by_source.get(name)?;
659            let alias = &self.aliases[*alias];
660            (alias.kind == AliasKind::MemberAlias)
661                .then(|| self.member(&alias.target))
662                .flatten()
663        })
664    }
665
666    /// The function entry with the given id, if declared.
667    pub fn function(&self, id: &str) -> Option<&Function> {
668        self.by_function.get(id).map(|i| &self.functions[*i])
669    }
670
671    /// The member function entry with the given id, if declared.
672    pub fn member(&self, id: &str) -> Option<&Function> {
673        self.by_member.get(id).map(|i| &self.functions[*i])
674    }
675
676    /// Whether the name is a declared enum-domain identity: a `param.domain`
677    /// or contextual option domain in the function table. These are OPY
678    /// signature metadata (catalog identity links); the domain *member
679    /// lists* are Workshop-owned catalog content and are not carried here,
680    /// so member validation is `lowering-dependent` (issue #8).
681    pub fn domain_identity(&self, name: &str) -> bool {
682        self.domain_identities.contains(name)
683    }
684}
685
686/// Canonicalize manifest data: parse, validate, and re-serialize
687/// deterministically (object keys sorted, stable formatting). Re-running on
688/// the same input produces byte-identical output, so the data is
689/// reproducible and the committed file must equal its canonical form.
690pub fn canonicalize(manifest_json: &str, probes_json: &str) -> Result<String, ManifestError> {
691    Manifest::load(manifest_json, probes_json)?;
692    let value: serde_json::Value = serde_json::from_str(manifest_json)
693        .map_err(|error| ManifestError(format!("manifest data: {error}")))?;
694    serde_json::to_string_pretty(&value)
695        .map(|mut out| {
696            out.push('\n');
697            out
698        })
699        .map_err(|error| ManifestError(format!("cannot serialize manifest: {error}")))
700}
701
702#[cfg(test)]
703mod tests {
704    use super::*;
705
706    #[test]
707    fn builtin_manifest_loads_and_validates() {
708        let manifest = Manifest::builtin().expect("embedded manifest must validate");
709        assert_eq!(manifest.schema_version, 1);
710        assert_eq!(manifest.reference.name, "overpy");
711        assert_eq!(manifest.reference.version, "9.7.10");
712        assert_eq!(
713            manifest.reference.content_commit,
714            "889d9749d1def17f146548cbddb94ea1ab015847"
715        );
716        assert!(!manifest.functions.is_empty());
717        assert!(!manifest.aliases.is_empty());
718        // Enum-domain *identities* come from the function signatures
719        // (param.domain / contextual option domains); member lists are
720        // Workshop-owned catalog content and are not carried here. Every
721        // member entry declares a receiver; every entry has evidence.
722        for domain in ["Invis", "ChaseTimeReeval", "Team", "LosCheck", "Color"] {
723            assert!(manifest.domain_identity(domain), "{domain}");
724        }
725        assert_eq!(
726            manifest
727                .function("chase")
728                .expect("chase entry")
729                .catalog_link,
730            CatalogLink::SpecialLowering
731        );
732        assert_eq!(
733            manifest
734                .member("getHero")
735                .expect("getHero entry")
736                .catalog_link,
737            CatalogLink::Canonical
738        );
739        assert!(
740            !manifest.domain_identity("ChaseReeval"),
741            "contextual domains are not standalone identities"
742        );
743        for function in &manifest.functions {
744            assert!(!function.evidence.is_empty(), "{}", function.id);
745            if function.kind.is_member() {
746                assert!(function.receiver.is_some(), "{}", function.id);
747            }
748        }
749    }
750
751    #[test]
752    fn manifest_data_is_canonical() {
753        // The committed data file must equal its deterministic canonical
754        // rewrite (the `build` path), so the data pipeline is reproducible.
755        let canonical = canonicalize(MANIFEST_DATA, PROBES_DATA).expect("canonicalizes");
756        assert_eq!(canonical, MANIFEST_DATA, "manifest.json must be canonical");
757        // Idempotency: re-canonicalizing the canonical form is byte-stable.
758        assert_eq!(
759            canonicalize(&canonical, PROBES_DATA).expect("re-canonicalizes"),
760            canonical
761        );
762    }
763
764    #[test]
765    fn validation_rejects_duplicates_and_missing_evidence() {
766        fn mutate(mutate: impl FnOnce(&mut ManifestFile)) -> Result<Manifest, ManifestError> {
767            let mut file: ManifestFile = serde_json::from_str(MANIFEST_DATA).unwrap();
768            mutate(&mut file);
769            Manifest::load(&serde_json::to_string(&file).unwrap(), PROBES_DATA)
770        }
771        // duplicate function id
772        let error = mutate(|file| file.functions.push(file.functions[0].clone()))
773            .expect_err("duplicate function id must fail");
774        assert!(error.0.contains("duplicate function id"));
775        // A direct catalog link must be explicit about being canonical.
776        let error = mutate(|file| file.functions[0].catalog_link = CatalogLink::CatalogGap)
777            .expect_err("canonical catalog id must not carry a gap reason");
778        assert!(error.0.contains("catalogLink"));
779        // entry without evidence
780        let error = mutate(|file| file.functions[0].evidence.clear())
781            .expect_err("missing evidence must fail");
782        assert!(error.0.contains("no oracle probe evidence"));
783        // enum-member default without a declared domain is a data-integrity
784        // error (the default cannot be expanded without an identity)
785        let error = mutate(|file| {
786            file.functions[0].params.push(Param {
787                name: "bad".to_string(),
788                domain: None,
789                default: Some(ParamDefault::EnumMember("X".to_string())),
790                optional: false,
791                keyword_only: false,
792                positional_only: false,
793                alternate_names: Vec::new(),
794                variable: false,
795            })
796        })
797        .expect_err("enum default without a domain must fail");
798        assert!(error.0.contains("no declared domain"));
799    }
800
801    #[test]
802    fn arity_bounds_follow_defaults_and_unbounded() {
803        let manifest = Manifest::builtin().expect("builtin");
804        let chase = manifest.function("chaseOverTime").expect("entry");
805        assert_eq!(chase.arity_bounds(), (3, Some(4)));
806        let radius = manifest.function("getPlayersInRadius").expect("entry");
807        assert_eq!(radius.arity_bounds(), (2, Some(4)));
808        let status = manifest.member("setStatusEffect").expect("entry");
809        assert_eq!(status.arity_bounds(), (3, Some(3)));
810        let format = manifest.member("format").expect("entry");
811        assert_eq!(format.arity_bounds(), (0, None));
812        let range = manifest.function("range").expect("entry");
813        assert_eq!(range.arity_bounds(), (1, Some(3)));
814        assert_eq!(range.context, Some(FunctionContext::ForIterable));
815    }
816
817    #[test]
818    fn aliases_resolve_to_declared_targets() {
819        let manifest = Manifest::builtin().expect("builtin");
820        let alias = manifest
821            .resolve_function("stopChasingVariable")
822            .expect("alias");
823        assert_eq!(alias.id, "stopChasingVariable");
824        assert!(alias.kind.is_action());
825        let member = manifest.resolve_member("getCurrentHero").expect("alias");
826        assert_eq!(member.id, "getHero");
827        assert!(member.kind.is_value());
828        // Unknown names stay unresolved.
829        assert!(manifest.resolve_function("frobnicate").is_none());
830        assert!(manifest.resolve_member("frobnicate").is_none());
831    }
832}