Skip to main content

printobserver_supervisor_api/
session.rs

1//! The supervision session one print is watched through.
2//!
3//! A session is what [`SupervisorPort::run_turn`](crate::SupervisorPort::run_turn)
4//! opens or continues and what a [`TurnOutcome`](crate::TurnOutcome) answers
5//! with, so it is declared beside the port whose turns run in it; the
6//! supervision domain persists it, and the composition root serves it, but
7//! neither adds a word to it.
8
9use printobserver_types::contract::Sample;
10use printobserver_types::schemars::JsonSchema;
11use printobserver_types::serde::{Deserialize, Serialize};
12use printobserver_types::{PrintId, Timestamp};
13
14/// Whether a turn opened a session or continued one.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
16#[serde(
17    crate = "printobserver_types::serde",
18    rename_all = "snake_case",
19    deny_unknown_fields
20)]
21#[schemars(crate = "printobserver_types::schemars")]
22pub enum SessionPhase {
23    /// This turn opened the session.
24    Created,
25    /// This turn continued a session already open.
26    Continued,
27}
28
29/// The supervision session keyed to one print.
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
31#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
32#[schemars(crate = "printobserver_types::schemars")]
33pub struct SupervisionSession {
34    /// The print this session watches.
35    pub print_id: PrintId,
36    /// The session's own name in the harness.
37    pub session_name: String,
38    /// The identity the harness ran the session under.
39    pub harness_identity: String,
40    /// When the session was opened.
41    pub created_at: Timestamp,
42    /// When the last turn ran.
43    pub last_turn_at: Timestamp,
44    /// When the session was closed, if it was.
45    #[serde(default, skip_serializing_if = "Option::is_none")]
46    pub closed_at: Option<Timestamp>,
47    /// Why it was closed, if it was.
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    pub close_reason: Option<String>,
50}
51
52impl Sample for SessionPhase {
53    fn sample_full() -> Self {
54        Self::Created
55    }
56}
57
58impl Sample for SupervisionSession {
59    fn sample_full() -> Self {
60        Self {
61            print_id: PrintId::sample_full(),
62            session_name: "print-0191f0a0".to_owned(),
63            harness_identity: "printobserver-supervisor".to_owned(),
64            created_at: Timestamp::sample_full(),
65            last_turn_at: later_instant(),
66            closed_at: Some(later_instant()),
67            close_reason: Some("the print ended".to_owned()),
68        }
69    }
70
71    fn sample_minimal() -> Self {
72        Self {
73            closed_at: None,
74            close_reason: None,
75            ..Self::sample_full()
76        }
77    }
78}
79
80/// A later fixed instant than the one the type crate's sample carries.
81fn later_instant() -> Timestamp {
82    "2026-03-01T12:30:00Z"
83        .parse()
84        .expect("a fixed RFC 3339 instant")
85}