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