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}
70
71/// A test attached to a SigId (spec §4.4).
72#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
73pub struct Test {
74    pub id: String,
75    pub kind: String,
76    pub input: serde_json::Value,
77    pub expected_output: serde_json::Value,
78    #[serde(default)]
79    pub effects_allowed: Vec<String>,
80}
81
82/// A spec attached to a SigId (spec §4.4). Kept opaque here — the
83/// spec-checker (M10) interprets its body.
84#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
85pub struct Spec {
86    pub id: String,
87    pub kind: String,
88    pub body: serde_json::Value,
89}