Skip to main content

lex_store/
model.rs

1//! Persisted store records.
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
6#[serde(rename_all = "snake_case")]
7pub enum StageStatus {
8    Draft,
9    Active,
10    Deprecated,
11    Tombstone,
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
15pub struct Transition {
16    pub stage_id: String,
17    pub from: StageStatus,
18    pub to: StageStatus,
19    pub at: u64,            // unix seconds
20    #[serde(default, skip_serializing_if = "Option::is_none")]
21    pub reason: Option<String>,
22}
23
24/// Per-`SigId` lifecycle log: append-only list of state transitions for
25/// every implementation that's ever been published under this signature.
26#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
27pub struct Lifecycle {
28    pub sig_id: String,
29    pub transitions: Vec<Transition>,
30}
31
32impl Lifecycle {
33    /// Current status of a given implementation.
34    pub fn status_of(&self, stage_id: &str) -> Option<StageStatus> {
35        self.transitions
36            .iter()
37            .rev()
38            .find(|t| t.stage_id == stage_id)
39            .map(|t| t.to)
40    }
41
42    /// The currently-Active StageId for this signature, if any.
43    pub fn current_active(&self) -> Option<&str> {
44        // Walk transitions chronologically; track latest status per stage.
45        use indexmap::IndexMap;
46        let mut latest: IndexMap<&str, StageStatus> = IndexMap::new();
47        for t in &self.transitions {
48            latest.insert(&t.stage_id, t.to);
49        }
50        latest
51            .into_iter()
52            .find(|(_, s)| *s == StageStatus::Active)
53            .map(|(id, _)| id)
54    }
55}
56
57/// Per-implementation metadata (`<StageId>.metadata.json`).
58#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
59pub struct Metadata {
60    pub stage_id: String,
61    pub sig_id: String,
62    /// Human-friendly name (e.g. "factorial"). Lives here, not in the
63    /// implementation hash, so renames don't change StageId.
64    pub name: String,
65    pub published_at: u64,
66    /// Free-form notes (e.g. "fixes overflow on n>20").
67    #[serde(default, skip_serializing_if = "Option::is_none")]
68    pub note: Option<String>,
69    /// The declaration's own `#` comments, in source order, each including
70    /// its leading `#`.
71    ///
72    /// `lex-syntax` parses and attaches comments per declaration, and the
73    /// canonicalizer strips them so they never participate in a SigId or
74    /// StageId. That exclusion is right and load-bearing: editing a comment
75    /// must not read as a code change, or exact replay would demand a model
76    /// reproduce prose verbatim.
77    ///
78    /// But the op-log stored only the canonical (stripped) AST, and a registry
79    /// archive is *rendered* from that log — so every hosted package was served
80    /// with all documentation removed. `lex-ocpi` lost 2051 comment lines,
81    /// `lex-agent` 519; the module header explaining a package's schema and
82    /// purpose simply vanished between GitHub and the registry.
83    ///
84    /// They live here for exactly the reason `name` does: outside the hash, so
85    /// they survive the round trip without disturbing content addressing.
86    #[serde(default, skip_serializing_if = "Vec::is_empty")]
87    pub doc: Vec<String>,
88    /// Optional Ed25519 signature over the UTF-8 bytes of `stage_id`
89    /// (#227). Set on publish when the caller provides a signing key;
90    /// consumers verify via [`lex_vcs::verify_stage_id`]. Absence
91    /// means "unsigned" — policy decides whether to accept it.
92    #[serde(default, skip_serializing_if = "Option::is_none")]
93    pub signature: Option<lex_vcs::Signature>,
94}
95
96/// A test attached to a SigId (spec §4.4).
97#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
98pub struct Test {
99    pub id: String,
100    pub kind: String,
101    pub input: serde_json::Value,
102    pub expected_output: serde_json::Value,
103    #[serde(default)]
104    pub effects_allowed: Vec<String>,
105}
106
107/// A spec attached to a SigId (spec §4.4). Kept opaque here — the
108/// spec-checker (M10) interprets its body.
109#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
110pub struct Spec {
111    pub id: String,
112    pub kind: String,
113    pub body: serde_json::Value,
114}