Skip to main content

zero_session/
event.rs

1//! Event model — what `events` rows look like in Rust.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6/// Kinds the schema recognises. Mirrors the TUI's `EntryKind` plus
7/// a `mode_change` variant used when the operator switches view.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[serde(rename_all = "snake_case")]
10pub enum EventKind {
11    Prompt,
12    System,
13    Command,
14    Warn,
15    Alert,
16    ModeChange,
17}
18
19impl EventKind {
20    #[must_use]
21    pub const fn as_str(self) -> &'static str {
22        match self {
23            Self::Prompt => "prompt",
24            Self::System => "system",
25            Self::Command => "command",
26            Self::Warn => "warn",
27            Self::Alert => "alert",
28            Self::ModeChange => "mode_change",
29        }
30    }
31
32    /// Parse the SQL enum string back. Returns `None` on unknown
33    /// input — the schema CHECK already rejects bad rows, but the
34    /// Rust-side guard protects against schema drift post-migration.
35    ///
36    /// Deliberately not `FromStr` because the error type (`()`) we
37    /// would return is less useful than `Option`; callers always
38    /// handle "unknown" identically regardless.
39    #[must_use]
40    pub fn parse_str(s: &str) -> Option<Self> {
41        Some(match s {
42            "prompt" => Self::Prompt,
43            "system" => Self::System,
44            "command" => Self::Command,
45            "warn" => Self::Warn,
46            "alert" => Self::Alert,
47            "mode_change" => Self::ModeChange,
48            _ => return None,
49        })
50    }
51}
52
53/// A persisted event as it comes out of the DB. `seq` is the
54/// per-session monotonic counter; ordering by `seq` is the
55/// canonical replay order.
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub struct StoredEvent {
58    pub id: i64,
59    pub session_id: i64,
60    pub seq: i64,
61    pub at: DateTime<Utc>,
62    pub kind: EventKind,
63    pub text: String,
64}
65
66/// Minimal session row.
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct SessionRow {
69    pub id: i64,
70    pub ulid: String,
71    pub started_at: DateTime<Utc>,
72    pub ended_at: Option<DateTime<Utc>>,
73    pub engine_base_url: Option<String>,
74    pub cli_version: String,
75    pub parent_ulid: Option<String>,
76}