Skip to main content

printobserver_core/records/
samples.rs

1//! The canonical values of every record this domain declares.
2//!
3//! One value carrying every field, one carrying every optional field absent,
4//! and one per further arm of a union: what the schema set and the round-trip
5//! corpus are walked over. They are here rather than beside each record so
6//! that the records read as the data they are, and so that the fixed
7//! identifiers and instants the samples share are written once.
8
9use std::collections::BTreeMap;
10
11use printobserver_printer_api::{Adjustable, PrinterState};
12use printobserver_types::contract::Sample;
13use printobserver_types::{EventId, FileName, ImageId, PrintId, Range, Timestamp};
14
15use super::action::{
16    AcknowledgementDisposition, ActionKind, ActionRecord, ActionRequest, Actor, ActorClass,
17    ExecutionOutcome, PrintAction,
18};
19use super::ids::{ActionId, InterventionId};
20use super::image::ImageRecord;
21use super::intervention::{Intervention, InterventionOutcome};
22use super::manifest::JobManifest;
23use super::policy::{EffectiveBounds, PolicyDecision, RejectionReason, SafetyEnvelope};
24use super::print::{ManifestNarrowing, PrintRecord};
25
26/// A later fixed instant than the one the type crate's sample carries.
27fn later_instant() -> Timestamp {
28    "2026-03-01T12:30:00Z"
29        .parse()
30        .expect("a fixed RFC 3339 instant")
31}
32
33/// A fixed action identifier.
34fn action_id() -> ActionId {
35    "0191f0a0-0000-7000-8000-000000000004"
36        .parse()
37        .expect("a fixed version 7 identifier")
38}
39
40/// A fixed intervention identifier.
41fn intervention_id() -> InterventionId {
42    "0191f0a0-0000-7000-8000-000000000005"
43        .parse()
44        .expect("a fixed version 7 identifier")
45}
46
47impl Sample for ActionId {
48    fn sample_full() -> Self {
49        action_id()
50    }
51}
52
53impl Sample for InterventionId {
54    fn sample_full() -> Self {
55        intervention_id()
56    }
57}
58
59impl Sample for ManifestNarrowing {
60    fn sample_full() -> Self {
61        Self {
62            adjustable: Adjustable::Feedrate,
63            requested: Range::new(0.2, 4.0),
64            applied: Range::new(0.5, 1.5),
65        }
66    }
67}
68
69impl Sample for PrintRecord {
70    fn sample_full() -> Self {
71        Self {
72            id: PrintId::sample_full(),
73            provider_print_id: Some(4211),
74            file_name: Some("benchy.gcode".to_owned()),
75            state: PrinterState::Printing,
76            opened_at: Timestamp::sample_full(),
77            ended_at: Some(later_instant()),
78            end_reason: Some("finished".to_owned()),
79            narrowings: vec![ManifestNarrowing::sample_full()],
80        }
81    }
82
83    fn sample_minimal() -> Self {
84        Self {
85            id: PrintId::sample_full(),
86            provider_print_id: None,
87            file_name: None,
88            state: PrinterState::Printing,
89            opened_at: Timestamp::sample_full(),
90            ended_at: None,
91            end_reason: None,
92            narrowings: vec![],
93        }
94    }
95}
96
97impl Sample for ImageRecord {
98    fn sample_full() -> Self {
99        Self {
100            id: ImageId::sample_full(),
101            print_id: PrintId::sample_full(),
102            event_id: EventId::sample_full(),
103            source_url: Some("https://detector.example/snapshots/1.jpg".to_owned()),
104            fetched_at: Timestamp::sample_full(),
105            content_type: "image/jpeg".to_owned(),
106            byte_len: 51_200,
107            sha256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".to_owned(),
108            relative_path: "images/0191f0a0/1.jpg".to_owned(),
109        }
110    }
111
112    fn sample_minimal() -> Self {
113        Self {
114            source_url: None,
115            ..Self::sample_full()
116        }
117    }
118}
119
120impl Sample for Actor {
121    fn sample_full() -> Self {
122        Self::Agent {
123            session_name: "print-0191f0a0".to_owned(),
124        }
125    }
126}
127
128impl Sample for ActorClass {
129    fn sample_full() -> Self {
130        Self::Agent
131    }
132}
133
134impl Sample for ActionKind {
135    fn sample_full() -> Self {
136        Self::SetFeedrateFactor
137    }
138}
139
140impl Sample for AcknowledgementDisposition {
141    fn sample_full() -> Self {
142        Self::Watch
143    }
144}
145
146impl Sample for ExecutionOutcome {
147    fn sample_full() -> Self {
148        Self::Failed {
149            reason: "the printer refused the command".to_owned(),
150        }
151    }
152}
153
154impl Sample for PrintAction {
155    fn sample_full() -> Self {
156        Self::SetFeedrateFactor {
157            factor: 0.8,
158            duration_s: Some(600),
159            reason: "the first layer is going down fast".to_owned(),
160            actor: Actor::sample_full(),
161        }
162    }
163
164    fn sample_minimal() -> Self {
165        Self::SetFeedrateFactor {
166            factor: 0.8,
167            duration_s: None,
168            reason: "the first layer is going down fast".to_owned(),
169            actor: Actor::sample_full(),
170        }
171    }
172
173    fn sample_alternates() -> Vec<Self> {
174        let reason = || "the print needs it".to_owned();
175        vec![
176            Self::Pause {
177                reason: reason(),
178                actor: Actor::sample_full(),
179            },
180            Self::Resume {
181                reason: reason(),
182                actor: Actor::sample_full(),
183            },
184            Self::Cancel {
185                reason: reason(),
186                actor: Actor::sample_full(),
187            },
188            Self::StartPrint {
189                file_name: FileName::sample_full(),
190                manifest: JobManifest::sample_full(),
191                reason: reason(),
192                actor: Actor::sample_full(),
193            },
194            Self::SetFlowrateFactor {
195                factor: 1.05,
196                duration_s: Some(300),
197                reason: reason(),
198                actor: Actor::sample_full(),
199            },
200            Self::SetToolTargetC {
201                tool: 0,
202                target_c: 215.0,
203                duration_s: Some(300),
204                reason: reason(),
205                actor: Actor::sample_full(),
206            },
207            Self::SetBedTargetC {
208                target_c: 60.0,
209                duration_s: Some(300),
210                reason: reason(),
211                actor: Actor::sample_full(),
212            },
213            Self::SetFanPercent {
214                percent: 80.0,
215                duration_s: Some(300),
216                reason: reason(),
217                actor: Actor::sample_full(),
218            },
219            Self::AcknowledgeFailure {
220                event_id: EventId::sample_full(),
221                disposition: AcknowledgementDisposition::Watch,
222                reason: reason(),
223                actor: Actor::Operator,
224            },
225        ]
226    }
227}
228
229impl Sample for ActionRequest {
230    fn sample_full() -> Self {
231        Self {
232            action: PrintAction::sample_full(),
233            actor: Actor::sample_full(),
234            requested_at: Timestamp::sample_full(),
235        }
236    }
237
238    fn sample_minimal() -> Self {
239        Self {
240            action: PrintAction::sample_minimal(),
241            ..Self::sample_full()
242        }
243    }
244}
245
246impl Sample for ActionRecord {
247    fn sample_full() -> Self {
248        Self {
249            id: action_id(),
250            print_id: PrintId::sample_full(),
251            request: ActionRequest::sample_full(),
252            decision: PolicyDecision::Accepted,
253            executed_at: Some(later_instant()),
254            outcome: Some(ExecutionOutcome::Succeeded),
255        }
256    }
257
258    fn sample_minimal() -> Self {
259        Self {
260            request: ActionRequest::sample_minimal(),
261            executed_at: None,
262            outcome: None,
263            ..Self::sample_full()
264        }
265    }
266}
267
268/// The allowed map both the manifest and the envelope carry.
269fn allowed_ranges() -> BTreeMap<Adjustable, Range> {
270    BTreeMap::from([
271        (Adjustable::Feedrate, Range::new(0.5, 1.5)),
272        (Adjustable::ToolTarget { tool: 0 }, Range::new(190.0, 230.0)),
273    ])
274}
275
276impl Sample for JobManifest {
277    fn sample_full() -> Self {
278        Self {
279            file_name: "benchy.gcode".to_owned(),
280            material: "PLA".to_owned(),
281            nozzle_diameter_mm: 0.4,
282            slicer_profile: "0.2mm standard".to_owned(),
283            allowed: allowed_ranges(),
284            metadata: BTreeMap::from([("layer_height_mm".to_owned(), "0.2".to_owned())]),
285        }
286    }
287}
288
289impl Sample for SafetyEnvelope {
290    fn sample_full() -> Self {
291        Self {
292            allowed: allowed_ranges(),
293            actions: BTreeMap::from([(
294                ActorClass::Agent,
295                vec![ActionKind::Pause, ActionKind::SetFeedrateFactor],
296            )]),
297            agent_min_interval_s: 60,
298        }
299    }
300}
301
302impl Sample for EffectiveBounds {
303    fn sample_full() -> Self {
304        Self {
305            allowed: allowed_ranges(),
306        }
307    }
308}
309
310impl Sample for RejectionReason {
311    fn sample_full() -> Self {
312        Self::OutOfBounds {
313            adjustable: Adjustable::Feedrate,
314            requested: 4.0,
315            allowed: Range::new(0.5, 1.5),
316        }
317    }
318}
319
320impl Sample for PolicyDecision {
321    fn sample_full() -> Self {
322        Self::Rejected(RejectionReason::sample_full())
323    }
324}
325
326impl Sample for InterventionOutcome {
327    fn sample_full() -> Self {
328        Self::Superseded {
329            by: intervention_id(),
330        }
331    }
332}
333
334impl Sample for Intervention {
335    fn sample_full() -> Self {
336        Self {
337            id: intervention_id(),
338            print_id: PrintId::sample_full(),
339            action_id: action_id(),
340            adjustable: Adjustable::Feedrate,
341            prior_value: Some(1.0),
342            applied_value: 0.8,
343            applied_at: Timestamp::sample_full(),
344            expires_at: later_instant(),
345            restored_at: Some(later_instant()),
346            outcome: InterventionOutcome::Restored,
347        }
348    }
349
350    fn sample_minimal() -> Self {
351        Self {
352            prior_value: None,
353            restored_at: None,
354            outcome: InterventionOutcome::StillActive,
355            ..Self::sample_full()
356        }
357    }
358}