Skip to main content

printobserver_core/
context.rs

1//! The one aggregate a caller and the agent both read.
2//!
3//! [`PrintContext`] is the supervision domain's own view of one print: the
4//! record, the printer and job as the printer port last reported them, the
5//! manifest and the bounds in force, the interventions still active, the
6//! events most recently written down — the print's own, and what the printer
7//! watch recorded of the machine since the print opened — and the failure
8//! detector's health. The loop collects it when it handles an
9//! event, holds it for the turn that event prompts, and answers it to the
10//! `context` operation, so what the agent reasons about is the state the event
11//! was handled at rather than whatever the machine has drifted to since.
12//!
13//! It is declared here rather than centrally because every field of it is
14//! this domain's to collect: a domain that adds a fact a turn should see edits
15//! this crate and regenerates the clients, and nothing central.
16
17use crate::records::{EffectiveBounds, Intervention, JobManifest, PrintRecord};
18use crate::watch::DetectorHealth;
19use printobserver_printer_api::{JobSnapshot, PrinterSnapshot};
20use printobserver_types::contract::Sample;
21use printobserver_types::schemars::JsonSchema;
22use printobserver_types::serde::{Deserialize, Serialize};
23use printobserver_types::{EventRecord, ImageRef};
24
25/// Everything a supervision turn is given about one print.
26#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
27#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
28#[schemars(crate = "printobserver_types::schemars")]
29pub struct PrintContext {
30    /// The print itself.
31    pub print: PrintRecord,
32    /// The printer, when a snapshot could be taken.
33    #[serde(default, skip_serializing_if = "Option::is_none")]
34    pub printer: Option<PrinterSnapshot>,
35    /// The job, when a snapshot could be taken.
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub job: Option<JobSnapshot>,
38    /// The manifest, when the print has one.
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub manifest: Option<JobManifest>,
41    /// The limits in force, so that the agent can see them before it asks.
42    pub bounds: EffectiveBounds,
43    /// The interventions still active.
44    pub interventions: Vec<Intervention>,
45    /// A bounded list of this print's events, newest first.
46    pub recent_events: Vec<EventRecord>,
47    /// The most recent image, when there is one.
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    pub latest_image: Option<ImageRef>,
50    /// The failure detector's health, and since when it has been so.
51    #[serde(default)]
52    pub detector: DetectorHealth,
53}
54
55impl Sample for PrintContext {
56    fn sample_full() -> Self {
57        Self {
58            print: PrintRecord::sample_full(),
59            printer: Some(PrinterSnapshot::sample_full()),
60            job: Some(JobSnapshot::sample_full()),
61            manifest: Some(JobManifest::sample_full()),
62            bounds: EffectiveBounds::sample_full(),
63            interventions: vec![Intervention::sample_full()],
64            recent_events: vec![EventRecord::sample_full()],
65            latest_image: Some(ImageRef::sample_full()),
66            detector: DetectorHealth::sample_full(),
67        }
68    }
69
70    fn sample_minimal() -> Self {
71        Self {
72            print: PrintRecord::sample_minimal(),
73            printer: None,
74            job: None,
75            manifest: None,
76            bounds: EffectiveBounds::sample_full(),
77            interventions: vec![],
78            recent_events: vec![],
79            latest_image: None,
80            detector: DetectorHealth::sample_minimal(),
81        }
82    }
83}