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::charge::{
20    ChargeKey, ChargeMethod, ChargedFilament, FilePosition, PendingCharge, PendingTaken,
21    PrintOutcome, PrintSegmentKey,
22};
23use super::ids::{ActionId, InterventionId, SpoolId};
24use super::image::ImageRecord;
25use super::intervention::{Intervention, InterventionOutcome};
26use super::manifest::{JobManifest, SegmentSpool};
27use super::policy::{
28    EffectiveBounds, PolicyDecision, RejectionReason, SafetyEnvelope, SpoolField, SpoolRefusalCause,
29};
30use super::preflight::{
31    DoubtedSpool, MaterialComparison, MaterialName, MaterialObjection, MaterialResult, Preflight,
32    PreflightNeed, PreflightObjection, PreflightSegment, PreflightSpool, PreflightTool,
33    PreflightToolResult, PreflightVerdict, SheetComparison, SheetId, SheetObjection, SheetResult,
34    ShortSpool, UnanalysedSpool, UncomparedMaterial,
35};
36use super::print::{ManifestNarrowing, PrintRecord};
37use super::spool::{
38    ClearedMark, LoadedSpool, SpoolLifecycle, SpoolRecord, SpoolTrust, UnverifiedMark,
39    UnverifiedMarks, UnverifiedReason,
40};
41use super::spool_command::SpoolCommand;
42
43/// A later fixed instant than the one the type crate's sample carries.
44fn later_instant() -> Timestamp {
45    "2026-03-01T12:30:00Z"
46        .parse()
47        .expect("a fixed RFC 3339 instant")
48}
49
50/// A fixed action identifier.
51fn action_id() -> ActionId {
52    "0191f0a0-0000-7000-8000-000000000004"
53        .parse()
54        .expect("a fixed version 7 identifier")
55}
56
57/// A fixed intervention identifier.
58fn intervention_id() -> InterventionId {
59    "0191f0a0-0000-7000-8000-000000000005"
60        .parse()
61        .expect("a fixed version 7 identifier")
62}
63
64/// A fixed spool identifier.
65fn spool_id() -> SpoolId {
66    "0191f0a0-0000-7000-8000-000000000006"
67        .parse()
68        .expect("a fixed version 7 identifier")
69}
70
71/// A second fixed spool identifier, for the spool a swap replaced.
72fn replaced_spool_id() -> SpoolId {
73    "0191f0a0-0000-7000-8000-000000000007"
74        .parse()
75        .expect("a fixed version 7 identifier")
76}
77
78impl Sample for SpoolId {
79    fn sample_full() -> Self {
80        spool_id()
81    }
82}
83
84impl Sample for ActionId {
85    fn sample_full() -> Self {
86        action_id()
87    }
88}
89
90impl Sample for InterventionId {
91    fn sample_full() -> Self {
92        intervention_id()
93    }
94}
95
96impl Sample for ManifestNarrowing {
97    fn sample_full() -> Self {
98        Self {
99            adjustable: Adjustable::Feedrate,
100            requested: Range::new(0.2, 4.0),
101            applied: Range::new(0.5, 1.5),
102        }
103    }
104}
105
106impl Sample for PrintRecord {
107    fn sample_full() -> Self {
108        Self {
109            id: PrintId::sample_full(),
110            provider_print_id: Some(4211),
111            file_name: Some("benchy.gcode".to_owned()),
112            state: PrinterState::Printing,
113            opened_at: Timestamp::sample_full(),
114            job_started_at: Some(Timestamp::sample_full()),
115            job_print_time_s: Some(3024),
116            ended_at: Some(later_instant()),
117            end_reason: Some("finished".to_owned()),
118            narrowings: vec![ManifestNarrowing::sample_full()],
119        }
120    }
121
122    fn sample_minimal() -> Self {
123        Self {
124            id: PrintId::sample_full(),
125            provider_print_id: None,
126            file_name: None,
127            state: PrinterState::Printing,
128            opened_at: Timestamp::sample_full(),
129            job_started_at: None,
130            job_print_time_s: None,
131            ended_at: None,
132            end_reason: None,
133            narrowings: vec![],
134        }
135    }
136}
137
138impl Sample for ImageRecord {
139    fn sample_full() -> Self {
140        Self {
141            id: ImageId::sample_full(),
142            print_id: Some(PrintId::sample_full()),
143            event_id: EventId::sample_full(),
144            source_url: Some("https://detector.example/snapshots/1.jpg".to_owned()),
145            fetched_at: Timestamp::sample_full(),
146            content_type: "image/jpeg".to_owned(),
147            byte_len: 51_200,
148            sha256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".to_owned(),
149            relative_path: "images/0191f0a0/1.jpg".to_owned(),
150        }
151    }
152
153    fn sample_minimal() -> Self {
154        Self {
155            print_id: None,
156            source_url: None,
157            ..Self::sample_full()
158        }
159    }
160}
161
162impl Sample for Actor {
163    fn sample_full() -> Self {
164        Self::Agent {
165            session_name: "print-0191f0a0".to_owned(),
166        }
167    }
168}
169
170impl Sample for ActorClass {
171    fn sample_full() -> Self {
172        Self::Agent
173    }
174}
175
176impl Sample for ActionKind {
177    fn sample_full() -> Self {
178        Self::SetFeedrateFactor
179    }
180}
181
182impl Sample for AcknowledgementDisposition {
183    fn sample_full() -> Self {
184        Self::Watch
185    }
186}
187
188impl Sample for ExecutionOutcome {
189    fn sample_full() -> Self {
190        Self::Failed {
191            reason: "the printer refused the command".to_owned(),
192        }
193    }
194}
195
196impl Sample for PrintAction {
197    fn sample_full() -> Self {
198        Self::SetFeedrateFactor {
199            factor: 0.8,
200            duration_s: Some(600),
201            reason: "the first layer is going down fast".to_owned(),
202            actor: Actor::sample_full(),
203        }
204    }
205
206    fn sample_minimal() -> Self {
207        Self::SetFeedrateFactor {
208            factor: 0.8,
209            duration_s: None,
210            reason: "the first layer is going down fast".to_owned(),
211            actor: Actor::sample_full(),
212        }
213    }
214
215    fn sample_alternates() -> Vec<Self> {
216        let reason = || "the print needs it".to_owned();
217        vec![
218            Self::Pause {
219                reason: reason(),
220                actor: Actor::sample_full(),
221            },
222            Self::Resume {
223                reason: reason(),
224                actor: Actor::sample_full(),
225            },
226            Self::Cancel {
227                reason: reason(),
228                actor: Actor::sample_full(),
229            },
230            Self::StartPrint {
231                file_name: FileName::sample_full(),
232                manifest: JobManifest::sample_full(),
233                override_filament_check: true,
234                override_material_check: true,
235                override_sheet_check: true,
236                reason: reason(),
237                actor: Actor::sample_full(),
238            },
239            Self::SetFlowrateFactor {
240                factor: 1.05,
241                duration_s: Some(300),
242                reason: reason(),
243                actor: Actor::sample_full(),
244            },
245            Self::SetToolTargetC {
246                tool: 0,
247                target_c: 215.0,
248                duration_s: Some(300),
249                reason: reason(),
250                actor: Actor::sample_full(),
251            },
252            Self::SetBedTargetC {
253                target_c: 60.0,
254                duration_s: Some(300),
255                reason: reason(),
256                actor: Actor::sample_full(),
257            },
258            Self::SetFanPercent {
259                percent: 80.0,
260                duration_s: Some(300),
261                reason: reason(),
262                actor: Actor::sample_full(),
263            },
264            Self::AcknowledgeFailure {
265                event_id: EventId::sample_full(),
266                disposition: AcknowledgementDisposition::Watch,
267                reason: reason(),
268                actor: Actor::Operator,
269            },
270            Self::ClosePrint {
271                outcome: PrintOutcome::Interrupted,
272                reason: "the printer was switched off mid-print".to_owned(),
273                actor: Actor::Operator,
274            },
275        ]
276    }
277}
278
279impl Sample for ActionRequest {
280    fn sample_full() -> Self {
281        Self {
282            action: PrintAction::sample_full(),
283            actor: Actor::sample_full(),
284            requested_at: Timestamp::sample_full(),
285        }
286    }
287
288    fn sample_minimal() -> Self {
289        Self {
290            action: PrintAction::sample_minimal(),
291            ..Self::sample_full()
292        }
293    }
294}
295
296impl Sample for ActionRecord {
297    fn sample_full() -> Self {
298        Self {
299            id: action_id(),
300            print_id: PrintId::sample_full(),
301            request: ActionRequest::sample_full(),
302            decision: PolicyDecision::Accepted,
303            executed_at: Some(later_instant()),
304            outcome: Some(ExecutionOutcome::Succeeded),
305        }
306    }
307
308    fn sample_minimal() -> Self {
309        Self {
310            request: ActionRequest::sample_minimal(),
311            executed_at: None,
312            outcome: None,
313            ..Self::sample_full()
314        }
315    }
316}
317
318/// The allowed map both the manifest and the envelope carry.
319fn allowed_ranges() -> BTreeMap<Adjustable, Range> {
320    BTreeMap::from([
321        (Adjustable::Feedrate, Range::new(0.5, 1.5)),
322        (Adjustable::ToolTarget { tool: 0 }, Range::new(190.0, 230.0)),
323    ])
324}
325
326impl Sample for JobManifest {
327    fn sample_full() -> Self {
328        Self {
329            file_name: "benchy.gcode".to_owned(),
330            material: "PLA".to_owned(),
331            nozzle_diameter_mm: 0.4,
332            slicer_profile: "0.2mm standard".to_owned(),
333            sheet: Some("smooth-pei".to_owned()),
334            allowed: allowed_ranges(),
335            metadata: BTreeMap::from([("layer_height_mm".to_owned(), "0.2".to_owned())]),
336            segment_spools: vec![SegmentSpool::sample_full()],
337        }
338    }
339
340    fn sample_minimal() -> Self {
341        Self {
342            sheet: None,
343            segment_spools: Vec::new(),
344            ..Self::sample_full()
345        }
346    }
347}
348
349impl Sample for SegmentSpool {
350    fn sample_full() -> Self {
351        Self {
352            segment: 1,
353            spool_id: spool_id(),
354        }
355    }
356}
357
358impl Sample for SafetyEnvelope {
359    fn sample_full() -> Self {
360        Self {
361            allowed: allowed_ranges(),
362            actions: BTreeMap::from([(
363                ActorClass::Agent,
364                vec![ActionKind::Pause, ActionKind::SetFeedrateFactor],
365            )]),
366            agent_min_interval_s: 60,
367        }
368    }
369}
370
371impl Sample for EffectiveBounds {
372    fn sample_full() -> Self {
373        Self {
374            allowed: allowed_ranges(),
375        }
376    }
377}
378
379impl Sample for RejectionReason {
380    fn sample_full() -> Self {
381        Self::OutOfBounds {
382            adjustable: Adjustable::Feedrate,
383            requested: 4.0,
384            allowed: Range::new(0.5, 1.5),
385        }
386    }
387
388    fn sample_alternates() -> Vec<Self> {
389        vec![
390            Self::InsufficientFilament {
391                tool: 0,
392                spool_id: spool_id(),
393                spool_name: "Galaxy black".to_owned(),
394                required_g: 120.0,
395                buffer_g: 12.0,
396                remaining_g: 80.0,
397                unverified: Some(UnverifiedReason::sample_full()),
398                segment: Some(1),
399            },
400            Self::InsufficientFilament {
401                tool: 0,
402                spool_id: spool_id(),
403                spool_name: "Galaxy black".to_owned(),
404                required_g: 120.0,
405                buffer_g: 12.0,
406                remaining_g: 80.0,
407                unverified: None,
408                segment: None,
409            },
410            Self::UnverifiedSpool {
411                tool: 0,
412                spool_id: spool_id(),
413                spool_name: "Galaxy black".to_owned(),
414                reason: UnverifiedReason::sample_full(),
415                segment: Some(2),
416            },
417            Self::AnalysisUnavailable {
418                file_name: "benchy.gcode".to_owned(),
419                waited_s: 300,
420            },
421            Self::SpoolRefused {
422                cause: SpoolRefusalCause::sample_full(),
423                message: "spool Galaxy black is retired, so it takes no command".to_owned(),
424            },
425            Self::MaterialRefused {
426                objection: MaterialObjection::sample_full(),
427            },
428            Self::SheetRefused {
429                objection: SheetObjection::sample_full(),
430            },
431            Self::SheetNotDeclared,
432            Self::PrintAlreadyEnded,
433            Self::PrintStillActive {
434                state: PrinterState::Paused,
435            },
436            Self::InterventionStillActive {
437                intervention_id: InterventionId::sample_full(),
438            },
439            Self::PlannedChangeWaiting {
440                change_index: 0,
441                layer: 12,
442                z_mm: 2.4,
443                color: Some("#FF0000".to_owned()),
444            },
445            Self::PlannedChangeWaiting {
446                change_index: 1,
447                layer: 24,
448                z_mm: 4.8,
449                color: None,
450            },
451        ]
452    }
453}
454
455impl Sample for SpoolField {
456    fn sample_full() -> Self {
457        Self::GrossG
458    }
459}
460
461impl Sample for SpoolRefusalCause {
462    fn sample_full() -> Self {
463        Self::RetiredSpool {
464            spool_id: spool_id(),
465        }
466    }
467
468    fn sample_minimal() -> Self {
469        Self::DensityRequired {
470            spool_id: None,
471            material: "PEEK".to_owned(),
472        }
473    }
474
475    fn sample_alternates() -> Vec<Self> {
476        vec![
477            Self::UnknownSpool {
478                spool_id: replaced_spool_id(),
479            },
480            Self::NothingToConfirm { tool: 0 },
481            Self::InvalidAdjustmentFigure {
482                field: SpoolField::GrossG,
483                value: -5.0,
484            },
485            Self::TareRequired {
486                spool_id: spool_id(),
487            },
488            Self::DensityRequired {
489                spool_id: Some(spool_id()),
490                material: "PEEK".to_owned(),
491            },
492            Self::MissingField {
493                field: SpoolField::Name,
494            },
495            Self::ConflictingFields {
496                fields: vec![SpoolField::RemainingG, SpoolField::GrossG],
497            },
498            Self::UnknownTool { tool: -1 },
499            Self::NotLoaded {
500                spool_id: spool_id(),
501            },
502            Self::LedgerRefused,
503        ]
504    }
505}
506
507impl Sample for PolicyDecision {
508    fn sample_full() -> Self {
509        Self::Rejected(RejectionReason::sample_full())
510    }
511}
512
513impl Sample for InterventionOutcome {
514    fn sample_full() -> Self {
515        Self::Superseded {
516            by: intervention_id(),
517        }
518    }
519}
520
521impl Sample for Intervention {
522    fn sample_full() -> Self {
523        Self {
524            id: intervention_id(),
525            print_id: PrintId::sample_full(),
526            action_id: action_id(),
527            adjustable: Adjustable::Feedrate,
528            prior_value: Some(1.0),
529            applied_value: 0.8,
530            applied_at: Timestamp::sample_full(),
531            expires_at: later_instant(),
532            restored_at: Some(later_instant()),
533            outcome: InterventionOutcome::Restored,
534        }
535    }
536
537    fn sample_minimal() -> Self {
538        Self {
539            prior_value: None,
540            restored_at: None,
541            outcome: InterventionOutcome::StillActive,
542            ..Self::sample_full()
543        }
544    }
545}
546
547impl Sample for UnverifiedReason {
548    fn sample_full() -> Self {
549        Self::UnchargedPrint {
550            print_id: PrintId::sample_full(),
551        }
552    }
553
554    fn sample_alternates() -> Vec<Self> {
555        vec![
556            Self::UnaccountedPrint {
557                file_name: "benchy.gcode".to_owned(),
558                printed_at: Timestamp::sample_full(),
559            },
560            Self::FilamentChangeUnconfirmed {
561                print_id: PrintId::sample_full(),
562            },
563            Self::FilamentChangeUnconfirmedPosition {
564                print_id: PrintId::sample_full(),
565            },
566            Self::TallyReset {
567                file_name: "benchy.gcode".to_owned(),
568                origin: "local".to_owned(),
569            },
570        ]
571    }
572}
573
574impl Sample for UnverifiedMark {
575    fn sample_full() -> Self {
576        Self {
577            reason: UnverifiedReason::sample_full(),
578            since: Timestamp::sample_full(),
579        }
580    }
581}
582
583impl Sample for UnverifiedMarks {
584    fn sample_full() -> Self {
585        Self::new(vec![
586            UnverifiedMark::sample_full(),
587            UnverifiedMark {
588                reason: UnverifiedReason::FilamentChangeUnconfirmed {
589                    print_id: PrintId::sample_full(),
590                },
591                since: later_instant(),
592            },
593        ])
594        .expect("two marks")
595    }
596}
597
598impl Sample for SpoolTrust {
599    fn sample_full() -> Self {
600        Self::Unverified {
601            marks: UnverifiedMarks::sample_full(),
602        }
603    }
604
605    fn sample_alternates() -> Vec<Self> {
606        vec![Self::Verified]
607    }
608}
609
610impl Sample for SpoolRecord {
611    fn sample_full() -> Self {
612        Self {
613            id: spool_id(),
614            name: "Galaxy black".to_owned(),
615            material: "PLA".to_owned(),
616            color: Some("black".to_owned()),
617            diameter_mm: 1.75,
618            density_g_cm3: 1.24,
619            initial_g: 1_000.0,
620            remaining_g: 742.5,
621            tare_g: Some(250.0),
622            lifecycle: SpoolLifecycle::sample_full(),
623            trust: SpoolTrust::sample_full(),
624            registered_at: Timestamp::sample_full(),
625            updated_at: later_instant(),
626        }
627    }
628
629    fn sample_minimal() -> Self {
630        Self {
631            color: None,
632            tare_g: None,
633            lifecycle: SpoolLifecycle::Shelved,
634            trust: SpoolTrust::Verified,
635            ..Self::sample_full()
636        }
637    }
638
639    fn sample_alternates() -> Vec<Self> {
640        vec![Self {
641            remaining_g: 12.5,
642            lifecycle: SpoolLifecycle::Retired {
643                at: later_instant(),
644            },
645            ..Self::sample_full()
646        }]
647    }
648}
649
650impl Sample for SpoolLifecycle {
651    fn sample_full() -> Self {
652        Self::Loaded { tool: 0 }
653    }
654
655    fn sample_alternates() -> Vec<Self> {
656        vec![
657            Self::Shelved,
658            Self::Retired {
659                at: later_instant(),
660            },
661        ]
662    }
663}
664
665impl Sample for ClearedMark {
666    fn sample_full() -> Self {
667        Self {
668            spool_id: replaced_spool_id(),
669            reason: UnverifiedReason::FilamentChangeUnconfirmed {
670                print_id: PrintId::sample_full(),
671            },
672        }
673    }
674}
675
676impl Sample for LoadedSpool {
677    fn sample_full() -> Self {
678        Self {
679            tool: 0,
680            spool_id: spool_id(),
681        }
682    }
683}
684
685impl Sample for PrintOutcome {
686    fn sample_full() -> Self {
687        Self::Completed
688    }
689
690    fn sample_alternates() -> Vec<Self> {
691        vec![Self::Cancelled, Self::Failed, Self::Interrupted]
692    }
693}
694
695impl Sample for ChargeMethod {
696    fn sample_full() -> Self {
697        Self::GcodeExtrusion
698    }
699}
700
701impl Sample for ChargeKey {
702    fn sample_full() -> Self {
703        Self::Print {
704            print_id: PrintId::sample_full(),
705            tool: 0,
706            segment: 1,
707        }
708    }
709
710    fn sample_alternates() -> Vec<Self> {
711        vec![Self::Unobserved {
712            origin: "local".to_owned(),
713            file_name: "benchy.gcode".to_owned(),
714            generation: 2,
715            print_number: 7,
716            tool: 0,
717        }]
718    }
719}
720
721impl Sample for FilePosition {
722    fn sample_full() -> Self {
723        Self {
724            bytes: 1_761_607,
725            size_bytes: Some(4_194_304),
726            observed_at: Timestamp::sample_full(),
727        }
728    }
729
730    fn sample_minimal() -> Self {
731        Self {
732            size_bytes: None,
733            ..Self::sample_full()
734        }
735    }
736}
737
738impl Sample for PrintSegmentKey {
739    fn sample_full() -> Self {
740        Self {
741            print_id: PrintId::sample_full(),
742            tool: 0,
743            segment: 1,
744        }
745    }
746}
747
748impl Sample for ChargedFilament {
749    fn sample_full() -> Self {
750        Self {
751            spool_id: spool_id(),
752            grams: 2.98,
753            diameter_mm: 1.75,
754            density_g_cm3: 1.24,
755            remaining_before_g: 745.48,
756            remaining_after_g: 742.5,
757        }
758    }
759}
760
761impl Sample for PendingCharge {
762    fn sample_full() -> Self {
763        Self {
764            charge_key: PrintSegmentKey::sample_full(),
765            length_mm: 1_204.5,
766            method: ChargeMethod::GcodeExtrusion,
767            analysed_length_mm: Some(5_120.0),
768            position: Some(FilePosition::sample_full()),
769            note: Some("fed after the filament change at layer 12".to_owned()),
770            pended_at: later_instant(),
771        }
772    }
773
774    fn sample_minimal() -> Self {
775        Self {
776            method: ChargeMethod::FileFraction,
777            analysed_length_mm: None,
778            position: None,
779            note: None,
780            ..Self::sample_full()
781        }
782    }
783}
784
785impl Sample for PendingTaken {
786    fn sample_full() -> Self {
787        Self {
788            charge_key: ChargeKey::sample_full(),
789            print_id: PrintId::sample_full(),
790        }
791    }
792}
793
794impl Sample for PreflightVerdict {
795    fn sample_full() -> Self {
796        Self::Insufficient
797    }
798}
799
800impl Sample for PreflightSpool {
801    fn sample_full() -> Self {
802        Self {
803            id: spool_id(),
804            name: "Galaxy black".to_owned(),
805        }
806    }
807}
808
809impl Sample for PreflightNeed {
810    fn sample_full() -> Self {
811        Self {
812            required_g: 120.0,
813            buffer_g: 12.0,
814        }
815    }
816}
817
818impl Sample for ShortSpool {
819    fn sample_full() -> Self {
820        Self {
821            spool: PreflightSpool::sample_full(),
822            remaining_g: 80.0,
823            need: PreflightNeed::sample_full(),
824            marks: vec![UnverifiedMark::sample_full()],
825        }
826    }
827
828    fn sample_minimal() -> Self {
829        Self {
830            marks: Vec::new(),
831            ..Self::sample_full()
832        }
833    }
834}
835
836impl Sample for DoubtedSpool {
837    fn sample_full() -> Self {
838        Self {
839            spool: PreflightSpool::sample_full(),
840            remaining_g: 742.5,
841            need: Some(PreflightNeed::sample_full()),
842            marks: UnverifiedMarks::sample_full(),
843        }
844    }
845
846    fn sample_minimal() -> Self {
847        Self {
848            need: None,
849            ..Self::sample_full()
850        }
851    }
852}
853
854impl Sample for UnanalysedSpool {
855    fn sample_full() -> Self {
856        Self {
857            spool: PreflightSpool::sample_full(),
858            remaining_g: 742.5,
859        }
860    }
861}
862
863impl Sample for PreflightObjection {
864    fn sample_full() -> Self {
865        Self::Insufficient(ShortSpool::sample_full())
866    }
867
868    fn sample_minimal() -> Self {
869        Self::NoAnalysis(UnanalysedSpool::sample_full())
870    }
871
872    fn sample_alternates() -> Vec<Self> {
873        vec![
874            Self::Insufficient(ShortSpool::sample_minimal()),
875            Self::UnverifiedSpool(DoubtedSpool::sample_full()),
876        ]
877    }
878}
879
880impl Sample for PreflightToolResult {
881    fn sample_full() -> Self {
882        Self::Insufficient(ShortSpool::sample_full())
883    }
884
885    fn sample_minimal() -> Self {
886        Self::Unchecked
887    }
888
889    fn sample_alternates() -> Vec<Self> {
890        vec![
891            Self::Passed {
892                spool: PreflightSpool::sample_full(),
893                remaining_g: 742.5,
894                need: PreflightNeed::sample_full(),
895            },
896            Self::UnverifiedSpool(DoubtedSpool::sample_minimal()),
897            Self::NoAnalysis(UnanalysedSpool::sample_full()),
898            Self::Overridden {
899                objection: PreflightObjection::sample_full(),
900            },
901            Self::Warned {
902                objection: PreflightObjection::NoAnalysis(UnanalysedSpool::sample_full()),
903            },
904        ]
905    }
906}
907
908impl Sample for PreflightTool {
909    fn sample_full() -> Self {
910        Self {
911            tool: 0,
912            result: PreflightToolResult::sample_full(),
913        }
914    }
915
916    fn sample_minimal() -> Self {
917        Self {
918            tool: 1,
919            result: PreflightToolResult::Unchecked,
920        }
921    }
922}
923
924impl Sample for Preflight {
925    fn sample_full() -> Self {
926        Self {
927            file_name: "benchy.gcode".to_owned(),
928            tools: vec![
929                PreflightTool::sample_full(),
930                PreflightTool::sample_minimal(),
931            ],
932            verdict: PreflightVerdict::Insufficient,
933            material: Some(MaterialResult::sample_full()),
934            sheet: Some(SheetResult::sample_full()),
935            segments: vec![
936                PreflightSegment::sample_full(),
937                PreflightSegment {
938                    segment: 1,
939                    color: None,
940                    filament_mm: 812.4,
941                    required_g: None,
942                    spool_id: None,
943                },
944            ],
945            checked_at: Timestamp::sample_full(),
946        }
947    }
948
949    fn sample_minimal() -> Self {
950        Self {
951            material: None,
952            sheet: None,
953            segments: Vec::new(),
954            ..Self::sample_full()
955        }
956    }
957}
958
959impl Sample for PreflightSegment {
960    fn sample_full() -> Self {
961        Self {
962            segment: 0,
963            color: Some("#FFFFFF".to_owned()),
964            filament_mm: 301.7,
965            required_g: Some(0.9),
966            spool_id: Some(spool_id()),
967        }
968    }
969}
970
971/// A material, by its name.
972fn material(name: &str) -> MaterialName {
973    MaterialName::try_from(name.to_owned()).expect("a material named by something")
974}
975
976/// A sheet, by its identifier.
977fn sheet(id: &str) -> SheetId {
978    SheetId::try_from(id.to_owned()).expect("a sheet named by something")
979}
980
981impl Sample for MaterialName {
982    fn sample_full() -> Self {
983        material("PLA")
984    }
985}
986
987impl Sample for SheetId {
988    fn sample_full() -> Self {
989        sheet("smooth-pei")
990    }
991}
992
993impl Sample for MaterialComparison {
994    fn sample_full() -> Self {
995        Self {
996            tool: 0,
997            spool: PreflightSpool::sample_full(),
998            loaded: material("PETG"),
999            sliced_for: material("PLA"),
1000        }
1001    }
1002}
1003
1004impl Sample for UncomparedMaterial {
1005    fn sample_full() -> Self {
1006        Self {
1007            tool: 0,
1008            spool: PreflightSpool::sample_full(),
1009            loaded: material("PETG"),
1010            file_name: FileName::sample_full(),
1011        }
1012    }
1013}
1014
1015impl Sample for MaterialObjection {
1016    fn sample_full() -> Self {
1017        Self::Mismatch(MaterialComparison::sample_full())
1018    }
1019
1020    fn sample_alternates() -> Vec<Self> {
1021        vec![
1022            Self::Undeclared(UncomparedMaterial::sample_full()),
1023            Self::Unread {
1024                tool: 0,
1025                spool: PreflightSpool::sample_full(),
1026                loaded: material("PETG"),
1027                file_name: FileName::sample_full(),
1028                detail: "the printer could not be reached".to_owned(),
1029            },
1030        ]
1031    }
1032}
1033
1034impl Sample for MaterialResult {
1035    fn sample_full() -> Self {
1036        Self::Refused {
1037            objection: MaterialObjection::sample_full(),
1038        }
1039    }
1040
1041    fn sample_minimal() -> Self {
1042        Self::Unchecked
1043    }
1044
1045    fn sample_alternates() -> Vec<Self> {
1046        vec![
1047            Self::Passed(MaterialComparison {
1048                loaded: material("PLA"),
1049                ..MaterialComparison::sample_full()
1050            }),
1051            Self::Overridden {
1052                objection: MaterialObjection::sample_full(),
1053            },
1054            Self::Warned {
1055                objection: MaterialObjection::Undeclared(UncomparedMaterial::sample_full()),
1056            },
1057        ]
1058    }
1059}
1060
1061impl Sample for SheetComparison {
1062    fn sample_full() -> Self {
1063        Self {
1064            sheet: sheet("smooth-pei"),
1065            material: material("PETG"),
1066            allowed: vec![sheet("textured-pei")],
1067        }
1068    }
1069}
1070
1071impl Sample for SheetObjection {
1072    fn sample_full() -> Self {
1073        Self::NotAllowed(SheetComparison::sample_full())
1074    }
1075
1076    fn sample_alternates() -> Vec<Self> {
1077        vec![
1078            Self::UnlistedMaterial {
1079                sheet: sheet("smooth-pei"),
1080                material: material("ASA"),
1081            },
1082            Self::UnknownSheet {
1083                sheet: sheet("glass"),
1084                material: material("PLA"),
1085                known: vec![sheet("smooth-pei"), sheet("textured-pei")],
1086            },
1087        ]
1088    }
1089}
1090
1091impl Sample for SheetResult {
1092    fn sample_full() -> Self {
1093        Self::Refused {
1094            objection: SheetObjection::sample_full(),
1095        }
1096    }
1097
1098    fn sample_minimal() -> Self {
1099        Self::Unchecked
1100    }
1101
1102    fn sample_alternates() -> Vec<Self> {
1103        vec![
1104            Self::Passed(SheetComparison {
1105                sheet: sheet("textured-pei"),
1106                ..SheetComparison::sample_full()
1107            }),
1108            Self::Undeclared,
1109            Self::Overridden {
1110                objection: SheetObjection::sample_full(),
1111            },
1112            Self::Warned {
1113                objection: SheetObjection::sample_full(),
1114            },
1115        ]
1116    }
1117}
1118
1119impl Sample for SpoolCommand {
1120    fn sample_full() -> Self {
1121        Self::RegisterSpool {
1122            name: "Galaxy black".to_owned(),
1123            material: "PLA".to_owned(),
1124            color: Some("black".to_owned()),
1125            diameter_mm: Some(1.75),
1126            density_g_cm3: Some(1.24),
1127            initial_g: 1_000.0,
1128            remaining_g: Some(742.5),
1129            tare_g: Some(250.0),
1130            reason: "a new spool went on the shelf".to_owned(),
1131            actor: Actor::Operator,
1132        }
1133    }
1134
1135    fn sample_minimal() -> Self {
1136        Self::RegisterSpool {
1137            name: "Galaxy black".to_owned(),
1138            material: "PLA".to_owned(),
1139            color: None,
1140            diameter_mm: None,
1141            density_g_cm3: None,
1142            initial_g: 1_000.0,
1143            remaining_g: None,
1144            tare_g: None,
1145            reason: "a new spool went on the shelf".to_owned(),
1146            actor: Actor::Operator,
1147        }
1148    }
1149
1150    fn sample_alternates() -> Vec<Self> {
1151        let reason = || "the spool on the shelf changed".to_owned();
1152        vec![
1153            Self::LoadSpool {
1154                spool_id: spool_id(),
1155                tool: 0,
1156                reason: reason(),
1157                actor: Actor::Operator,
1158            },
1159            Self::UnloadSpool {
1160                spool_id: spool_id(),
1161                reason: reason(),
1162                actor: Actor::Operator,
1163            },
1164            Self::AdjustSpool {
1165                spool_id: spool_id(),
1166                remaining_g: Some(700.0),
1167                gross_g: None,
1168                reason: reason(),
1169                actor: Actor::Operator,
1170            },
1171            Self::AdjustSpool {
1172                spool_id: spool_id(),
1173                remaining_g: None,
1174                gross_g: Some(950.0),
1175                reason: reason(),
1176                actor: Actor::sample_full(),
1177            },
1178            Self::RetireSpool {
1179                spool_id: spool_id(),
1180                reason: reason(),
1181                actor: Actor::Operator,
1182            },
1183        ]
1184    }
1185}