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    /// Optional Ed25519 signature over the UTF-8 bytes of `stage_id`
70    /// (#227). Set on publish when the caller provides a signing key;
71    /// consumers verify via [`lex_vcs::verify_stage_id`]. Absence
72    /// means "unsigned" — policy decides whether to accept it.
73    #[serde(default, skip_serializing_if = "Option::is_none")]
74    pub signature: Option<lex_vcs::Signature>,
75}
76
77/// A test attached to a SigId (spec §4.4).
78#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
79pub struct Test {
80    pub id: String,
81    pub kind: String,
82    pub input: serde_json::Value,
83    pub expected_output: serde_json::Value,
84    #[serde(default)]
85    pub effects_allowed: Vec<String>,
86}
87
88/// A spec attached to a SigId (spec §4.4). Kept opaque here — the
89/// spec-checker (M10) interprets its body.
90#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
91pub struct Spec {
92    pub id: String,
93    pub kind: String,
94    pub body: serde_json::Value,
95}