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;
27use super::policy::{
28    EffectiveBounds, PolicyDecision, RejectionReason, SafetyEnvelope, SpoolField, SpoolRefusalCause,
29};
30use super::preflight::{
31    DoubtedSpool, MaterialComparison, MaterialName, MaterialObjection, MaterialResult, Preflight,
32    PreflightNeed, PreflightObjection, PreflightSpool, PreflightTool, PreflightToolResult,
33    PreflightVerdict, SheetComparison, SheetId, SheetObjection, SheetResult, ShortSpool,
34    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        }
337    }
338
339    fn sample_minimal() -> Self {
340        Self {
341            sheet: None,
342            ..Self::sample_full()
343        }
344    }
345}
346
347impl Sample for SafetyEnvelope {
348    fn sample_full() -> Self {
349        Self {
350            allowed: allowed_ranges(),
351            actions: BTreeMap::from([(
352                ActorClass::Agent,
353                vec![ActionKind::Pause, ActionKind::SetFeedrateFactor],
354            )]),
355            agent_min_interval_s: 60,
356        }
357    }
358}
359
360impl Sample for EffectiveBounds {
361    fn sample_full() -> Self {
362        Self {
363            allowed: allowed_ranges(),
364        }
365    }
366}
367
368impl Sample for RejectionReason {
369    fn sample_full() -> Self {
370        Self::OutOfBounds {
371            adjustable: Adjustable::Feedrate,
372            requested: 4.0,
373            allowed: Range::new(0.5, 1.5),
374        }
375    }
376
377    fn sample_alternates() -> Vec<Self> {
378        vec![
379            Self::InsufficientFilament {
380                tool: 0,
381                spool_id: spool_id(),
382                spool_name: "Galaxy black".to_owned(),
383                required_g: 120.0,
384                buffer_g: 12.0,
385                remaining_g: 80.0,
386                unverified: Some(UnverifiedReason::sample_full()),
387            },
388            Self::InsufficientFilament {
389                tool: 0,
390                spool_id: spool_id(),
391                spool_name: "Galaxy black".to_owned(),
392                required_g: 120.0,
393                buffer_g: 12.0,
394                remaining_g: 80.0,
395                unverified: None,
396            },
397            Self::UnverifiedSpool {
398                tool: 0,
399                spool_id: spool_id(),
400                spool_name: "Galaxy black".to_owned(),
401                reason: UnverifiedReason::sample_full(),
402            },
403            Self::AnalysisUnavailable {
404                file_name: "benchy.gcode".to_owned(),
405                waited_s: 300,
406            },
407            Self::SpoolRefused {
408                cause: SpoolRefusalCause::sample_full(),
409                message: "spool Galaxy black is retired, so it takes no command".to_owned(),
410            },
411            Self::MaterialRefused {
412                objection: MaterialObjection::sample_full(),
413            },
414            Self::SheetRefused {
415                objection: SheetObjection::sample_full(),
416            },
417            Self::SheetNotDeclared,
418            Self::PrintAlreadyEnded,
419            Self::PrintStillActive {
420                state: PrinterState::Paused,
421            },
422            Self::InterventionStillActive {
423                intervention_id: InterventionId::sample_full(),
424            },
425        ]
426    }
427}
428
429impl Sample for SpoolField {
430    fn sample_full() -> Self {
431        Self::GrossG
432    }
433}
434
435impl Sample for SpoolRefusalCause {
436    fn sample_full() -> Self {
437        Self::RetiredSpool {
438            spool_id: spool_id(),
439        }
440    }
441
442    fn sample_minimal() -> Self {
443        Self::DensityRequired {
444            spool_id: None,
445            material: "PEEK".to_owned(),
446        }
447    }
448
449    fn sample_alternates() -> Vec<Self> {
450        vec![
451            Self::UnknownSpool {
452                spool_id: replaced_spool_id(),
453            },
454            Self::NothingToConfirm { tool: 0 },
455            Self::InvalidAdjustmentFigure {
456                field: SpoolField::GrossG,
457                value: -5.0,
458            },
459            Self::TareRequired {
460                spool_id: spool_id(),
461            },
462            Self::DensityRequired {
463                spool_id: Some(spool_id()),
464                material: "PEEK".to_owned(),
465            },
466            Self::MissingField {
467                field: SpoolField::Name,
468            },
469            Self::ConflictingFields {
470                fields: vec![SpoolField::RemainingG, SpoolField::GrossG],
471            },
472            Self::UnknownTool { tool: -1 },
473            Self::NotLoaded {
474                spool_id: spool_id(),
475            },
476            Self::LedgerRefused,
477        ]
478    }
479}
480
481impl Sample for PolicyDecision {
482    fn sample_full() -> Self {
483        Self::Rejected(RejectionReason::sample_full())
484    }
485}
486
487impl Sample for InterventionOutcome {
488    fn sample_full() -> Self {
489        Self::Superseded {
490            by: intervention_id(),
491        }
492    }
493}
494
495impl Sample for Intervention {
496    fn sample_full() -> Self {
497        Self {
498            id: intervention_id(),
499            print_id: PrintId::sample_full(),
500            action_id: action_id(),
501            adjustable: Adjustable::Feedrate,
502            prior_value: Some(1.0),
503            applied_value: 0.8,
504            applied_at: Timestamp::sample_full(),
505            expires_at: later_instant(),
506            restored_at: Some(later_instant()),
507            outcome: InterventionOutcome::Restored,
508        }
509    }
510
511    fn sample_minimal() -> Self {
512        Self {
513            prior_value: None,
514            restored_at: None,
515            outcome: InterventionOutcome::StillActive,
516            ..Self::sample_full()
517        }
518    }
519}
520
521impl Sample for UnverifiedReason {
522    fn sample_full() -> Self {
523        Self::UnchargedPrint {
524            print_id: PrintId::sample_full(),
525        }
526    }
527
528    fn sample_alternates() -> Vec<Self> {
529        vec![
530            Self::UnaccountedPrint {
531                file_name: "benchy.gcode".to_owned(),
532                printed_at: Timestamp::sample_full(),
533            },
534            Self::FilamentChangeUnconfirmed {
535                print_id: PrintId::sample_full(),
536            },
537            Self::FilamentChangeUnconfirmedPosition {
538                print_id: PrintId::sample_full(),
539            },
540            Self::TallyReset {
541                file_name: "benchy.gcode".to_owned(),
542                origin: "local".to_owned(),
543            },
544        ]
545    }
546}
547
548impl Sample for UnverifiedMark {
549    fn sample_full() -> Self {
550        Self {
551            reason: UnverifiedReason::sample_full(),
552            since: Timestamp::sample_full(),
553        }
554    }
555}
556
557impl Sample for UnverifiedMarks {
558    fn sample_full() -> Self {
559        Self::new(vec![
560            UnverifiedMark::sample_full(),
561            UnverifiedMark {
562                reason: UnverifiedReason::FilamentChangeUnconfirmed {
563                    print_id: PrintId::sample_full(),
564                },
565                since: later_instant(),
566            },
567        ])
568        .expect("two marks")
569    }
570}
571
572impl Sample for SpoolTrust {
573    fn sample_full() -> Self {
574        Self::Unverified {
575            marks: UnverifiedMarks::sample_full(),
576        }
577    }
578
579    fn sample_alternates() -> Vec<Self> {
580        vec![Self::Verified]
581    }
582}
583
584impl Sample for SpoolRecord {
585    fn sample_full() -> Self {
586        Self {
587            id: spool_id(),
588            name: "Galaxy black".to_owned(),
589            material: "PLA".to_owned(),
590            color: Some("black".to_owned()),
591            diameter_mm: 1.75,
592            density_g_cm3: 1.24,
593            initial_g: 1_000.0,
594            remaining_g: 742.5,
595            tare_g: Some(250.0),
596            lifecycle: SpoolLifecycle::sample_full(),
597            trust: SpoolTrust::sample_full(),
598            registered_at: Timestamp::sample_full(),
599            updated_at: later_instant(),
600        }
601    }
602
603    fn sample_minimal() -> Self {
604        Self {
605            color: None,
606            tare_g: None,
607            lifecycle: SpoolLifecycle::Shelved,
608            trust: SpoolTrust::Verified,
609            ..Self::sample_full()
610        }
611    }
612
613    fn sample_alternates() -> Vec<Self> {
614        vec![Self {
615            remaining_g: 12.5,
616            lifecycle: SpoolLifecycle::Retired {
617                at: later_instant(),
618            },
619            ..Self::sample_full()
620        }]
621    }
622}
623
624impl Sample for SpoolLifecycle {
625    fn sample_full() -> Self {
626        Self::Loaded { tool: 0 }
627    }
628
629    fn sample_alternates() -> Vec<Self> {
630        vec![
631            Self::Shelved,
632            Self::Retired {
633                at: later_instant(),
634            },
635        ]
636    }
637}
638
639impl Sample for ClearedMark {
640    fn sample_full() -> Self {
641        Self {
642            spool_id: replaced_spool_id(),
643            reason: UnverifiedReason::FilamentChangeUnconfirmed {
644                print_id: PrintId::sample_full(),
645            },
646        }
647    }
648}
649
650impl Sample for LoadedSpool {
651    fn sample_full() -> Self {
652        Self {
653            tool: 0,
654            spool_id: spool_id(),
655        }
656    }
657}
658
659impl Sample for PrintOutcome {
660    fn sample_full() -> Self {
661        Self::Completed
662    }
663
664    fn sample_alternates() -> Vec<Self> {
665        vec![Self::Cancelled, Self::Failed, Self::Interrupted]
666    }
667}
668
669impl Sample for ChargeMethod {
670    fn sample_full() -> Self {
671        Self::GcodeExtrusion
672    }
673}
674
675impl Sample for ChargeKey {
676    fn sample_full() -> Self {
677        Self::Print {
678            print_id: PrintId::sample_full(),
679            tool: 0,
680            segment: 1,
681        }
682    }
683
684    fn sample_alternates() -> Vec<Self> {
685        vec![Self::Unobserved {
686            origin: "local".to_owned(),
687            file_name: "benchy.gcode".to_owned(),
688            generation: 2,
689            print_number: 7,
690            tool: 0,
691        }]
692    }
693}
694
695impl Sample for FilePosition {
696    fn sample_full() -> Self {
697        Self {
698            bytes: 1_761_607,
699            size_bytes: Some(4_194_304),
700            observed_at: Timestamp::sample_full(),
701        }
702    }
703
704    fn sample_minimal() -> Self {
705        Self {
706            size_bytes: None,
707            ..Self::sample_full()
708        }
709    }
710}
711
712impl Sample for PrintSegmentKey {
713    fn sample_full() -> Self {
714        Self {
715            print_id: PrintId::sample_full(),
716            tool: 0,
717            segment: 1,
718        }
719    }
720}
721
722impl Sample for ChargedFilament {
723    fn sample_full() -> Self {
724        Self {
725            spool_id: spool_id(),
726            grams: 2.98,
727            diameter_mm: 1.75,
728            density_g_cm3: 1.24,
729            remaining_before_g: 745.48,
730            remaining_after_g: 742.5,
731        }
732    }
733}
734
735impl Sample for PendingCharge {
736    fn sample_full() -> Self {
737        Self {
738            charge_key: PrintSegmentKey::sample_full(),
739            length_mm: 1_204.5,
740            method: ChargeMethod::GcodeExtrusion,
741            analysed_length_mm: Some(5_120.0),
742            position: Some(FilePosition::sample_full()),
743            note: Some("fed after the filament change at layer 12".to_owned()),
744            pended_at: later_instant(),
745        }
746    }
747
748    fn sample_minimal() -> Self {
749        Self {
750            method: ChargeMethod::FileFraction,
751            analysed_length_mm: None,
752            position: None,
753            note: None,
754            ..Self::sample_full()
755        }
756    }
757}
758
759impl Sample for PendingTaken {
760    fn sample_full() -> Self {
761        Self {
762            charge_key: ChargeKey::sample_full(),
763            print_id: PrintId::sample_full(),
764        }
765    }
766}
767
768impl Sample for PreflightVerdict {
769    fn sample_full() -> Self {
770        Self::Insufficient
771    }
772}
773
774impl Sample for PreflightSpool {
775    fn sample_full() -> Self {
776        Self {
777            id: spool_id(),
778            name: "Galaxy black".to_owned(),
779        }
780    }
781}
782
783impl Sample for PreflightNeed {
784    fn sample_full() -> Self {
785        Self {
786            required_g: 120.0,
787            buffer_g: 12.0,
788        }
789    }
790}
791
792impl Sample for ShortSpool {
793    fn sample_full() -> Self {
794        Self {
795            spool: PreflightSpool::sample_full(),
796            remaining_g: 80.0,
797            need: PreflightNeed::sample_full(),
798            marks: vec![UnverifiedMark::sample_full()],
799        }
800    }
801
802    fn sample_minimal() -> Self {
803        Self {
804            marks: Vec::new(),
805            ..Self::sample_full()
806        }
807    }
808}
809
810impl Sample for DoubtedSpool {
811    fn sample_full() -> Self {
812        Self {
813            spool: PreflightSpool::sample_full(),
814            remaining_g: 742.5,
815            need: Some(PreflightNeed::sample_full()),
816            marks: UnverifiedMarks::sample_full(),
817        }
818    }
819
820    fn sample_minimal() -> Self {
821        Self {
822            need: None,
823            ..Self::sample_full()
824        }
825    }
826}
827
828impl Sample for UnanalysedSpool {
829    fn sample_full() -> Self {
830        Self {
831            spool: PreflightSpool::sample_full(),
832            remaining_g: 742.5,
833        }
834    }
835}
836
837impl Sample for PreflightObjection {
838    fn sample_full() -> Self {
839        Self::Insufficient(ShortSpool::sample_full())
840    }
841
842    fn sample_minimal() -> Self {
843        Self::NoAnalysis(UnanalysedSpool::sample_full())
844    }
845
846    fn sample_alternates() -> Vec<Self> {
847        vec![
848            Self::Insufficient(ShortSpool::sample_minimal()),
849            Self::UnverifiedSpool(DoubtedSpool::sample_full()),
850        ]
851    }
852}
853
854impl Sample for PreflightToolResult {
855    fn sample_full() -> Self {
856        Self::Insufficient(ShortSpool::sample_full())
857    }
858
859    fn sample_minimal() -> Self {
860        Self::Unchecked
861    }
862
863    fn sample_alternates() -> Vec<Self> {
864        vec![
865            Self::Passed {
866                spool: PreflightSpool::sample_full(),
867                remaining_g: 742.5,
868                need: PreflightNeed::sample_full(),
869            },
870            Self::UnverifiedSpool(DoubtedSpool::sample_minimal()),
871            Self::NoAnalysis(UnanalysedSpool::sample_full()),
872            Self::Overridden {
873                objection: PreflightObjection::sample_full(),
874            },
875            Self::Warned {
876                objection: PreflightObjection::NoAnalysis(UnanalysedSpool::sample_full()),
877            },
878        ]
879    }
880}
881
882impl Sample for PreflightTool {
883    fn sample_full() -> Self {
884        Self {
885            tool: 0,
886            result: PreflightToolResult::sample_full(),
887        }
888    }
889
890    fn sample_minimal() -> Self {
891        Self {
892            tool: 1,
893            result: PreflightToolResult::Unchecked,
894        }
895    }
896}
897
898impl Sample for Preflight {
899    fn sample_full() -> Self {
900        Self {
901            file_name: "benchy.gcode".to_owned(),
902            tools: vec![
903                PreflightTool::sample_full(),
904                PreflightTool::sample_minimal(),
905            ],
906            verdict: PreflightVerdict::Insufficient,
907            material: Some(MaterialResult::sample_full()),
908            sheet: Some(SheetResult::sample_full()),
909            checked_at: Timestamp::sample_full(),
910        }
911    }
912
913    fn sample_minimal() -> Self {
914        Self {
915            material: None,
916            sheet: None,
917            ..Self::sample_full()
918        }
919    }
920}
921
922/// A material, by its name.
923fn material(name: &str) -> MaterialName {
924    MaterialName::try_from(name.to_owned()).expect("a material named by something")
925}
926
927/// A sheet, by its identifier.
928fn sheet(id: &str) -> SheetId {
929    SheetId::try_from(id.to_owned()).expect("a sheet named by something")
930}
931
932impl Sample for MaterialName {
933    fn sample_full() -> Self {
934        material("PLA")
935    }
936}
937
938impl Sample for SheetId {
939    fn sample_full() -> Self {
940        sheet("smooth-pei")
941    }
942}
943
944impl Sample for MaterialComparison {
945    fn sample_full() -> Self {
946        Self {
947            tool: 0,
948            spool: PreflightSpool::sample_full(),
949            loaded: material("PETG"),
950            sliced_for: material("PLA"),
951        }
952    }
953}
954
955impl Sample for UncomparedMaterial {
956    fn sample_full() -> Self {
957        Self {
958            tool: 0,
959            spool: PreflightSpool::sample_full(),
960            loaded: material("PETG"),
961            file_name: FileName::sample_full(),
962        }
963    }
964}
965
966impl Sample for MaterialObjection {
967    fn sample_full() -> Self {
968        Self::Mismatch(MaterialComparison::sample_full())
969    }
970
971    fn sample_alternates() -> Vec<Self> {
972        vec![
973            Self::Undeclared(UncomparedMaterial::sample_full()),
974            Self::Unread {
975                tool: 0,
976                spool: PreflightSpool::sample_full(),
977                loaded: material("PETG"),
978                file_name: FileName::sample_full(),
979                detail: "the printer could not be reached".to_owned(),
980            },
981        ]
982    }
983}
984
985impl Sample for MaterialResult {
986    fn sample_full() -> Self {
987        Self::Refused {
988            objection: MaterialObjection::sample_full(),
989        }
990    }
991
992    fn sample_minimal() -> Self {
993        Self::Unchecked
994    }
995
996    fn sample_alternates() -> Vec<Self> {
997        vec![
998            Self::Passed(MaterialComparison {
999                loaded: material("PLA"),
1000                ..MaterialComparison::sample_full()
1001            }),
1002            Self::Overridden {
1003                objection: MaterialObjection::sample_full(),
1004            },
1005            Self::Warned {
1006                objection: MaterialObjection::Undeclared(UncomparedMaterial::sample_full()),
1007            },
1008        ]
1009    }
1010}
1011
1012impl Sample for SheetComparison {
1013    fn sample_full() -> Self {
1014        Self {
1015            sheet: sheet("smooth-pei"),
1016            material: material("PETG"),
1017            allowed: vec![sheet("textured-pei")],
1018        }
1019    }
1020}
1021
1022impl Sample for SheetObjection {
1023    fn sample_full() -> Self {
1024        Self::NotAllowed(SheetComparison::sample_full())
1025    }
1026
1027    fn sample_alternates() -> Vec<Self> {
1028        vec![
1029            Self::UnlistedMaterial {
1030                sheet: sheet("smooth-pei"),
1031                material: material("ASA"),
1032            },
1033            Self::UnknownSheet {
1034                sheet: sheet("glass"),
1035                material: material("PLA"),
1036                known: vec![sheet("smooth-pei"), sheet("textured-pei")],
1037            },
1038        ]
1039    }
1040}
1041
1042impl Sample for SheetResult {
1043    fn sample_full() -> Self {
1044        Self::Refused {
1045            objection: SheetObjection::sample_full(),
1046        }
1047    }
1048
1049    fn sample_minimal() -> Self {
1050        Self::Unchecked
1051    }
1052
1053    fn sample_alternates() -> Vec<Self> {
1054        vec![
1055            Self::Passed(SheetComparison {
1056                sheet: sheet("textured-pei"),
1057                ..SheetComparison::sample_full()
1058            }),
1059            Self::Undeclared,
1060            Self::Overridden {
1061                objection: SheetObjection::sample_full(),
1062            },
1063            Self::Warned {
1064                objection: SheetObjection::sample_full(),
1065            },
1066        ]
1067    }
1068}
1069
1070impl Sample for SpoolCommand {
1071    fn sample_full() -> Self {
1072        Self::RegisterSpool {
1073            name: "Galaxy black".to_owned(),
1074            material: "PLA".to_owned(),
1075            color: Some("black".to_owned()),
1076            diameter_mm: Some(1.75),
1077            density_g_cm3: Some(1.24),
1078            initial_g: 1_000.0,
1079            remaining_g: Some(742.5),
1080            tare_g: Some(250.0),
1081            reason: "a new spool went on the shelf".to_owned(),
1082            actor: Actor::Operator,
1083        }
1084    }
1085
1086    fn sample_minimal() -> Self {
1087        Self::RegisterSpool {
1088            name: "Galaxy black".to_owned(),
1089            material: "PLA".to_owned(),
1090            color: None,
1091            diameter_mm: None,
1092            density_g_cm3: None,
1093            initial_g: 1_000.0,
1094            remaining_g: None,
1095            tare_g: None,
1096            reason: "a new spool went on the shelf".to_owned(),
1097            actor: Actor::Operator,
1098        }
1099    }
1100
1101    fn sample_alternates() -> Vec<Self> {
1102        let reason = || "the spool on the shelf changed".to_owned();
1103        vec![
1104            Self::LoadSpool {
1105                spool_id: spool_id(),
1106                tool: 0,
1107                reason: reason(),
1108                actor: Actor::Operator,
1109            },
1110            Self::UnloadSpool {
1111                spool_id: spool_id(),
1112                reason: reason(),
1113                actor: Actor::Operator,
1114            },
1115            Self::AdjustSpool {
1116                spool_id: spool_id(),
1117                remaining_g: Some(700.0),
1118                gross_g: None,
1119                reason: reason(),
1120                actor: Actor::Operator,
1121            },
1122            Self::AdjustSpool {
1123                spool_id: spool_id(),
1124                remaining_g: None,
1125                gross_g: Some(950.0),
1126                reason: reason(),
1127                actor: Actor::sample_full(),
1128            },
1129            Self::RetireSpool {
1130                spool_id: spool_id(),
1131                reason: reason(),
1132                actor: Actor::Operator,
1133            },
1134        ]
1135    }
1136}