Skip to main content

printobserver_printer_api/
file.rs

1//! What a printer knows of the files it holds, and of the filament they use.
2//!
3//! These are what the three file reads of
4//! [`PrinterPort`](crate::PrinterPort) answer — the facts of one file, the
5//! files that carry a print tally, and the filament a file's own moves extrude
6//! up to a position in it — declared beside the port whose methods carry them.
7//! Every length is the filament fed in, in millimetres, per tool.
8
9use core::ops::ControlFlow;
10
11use printobserver_types::Timestamp;
12use printobserver_types::contract::Sample;
13use printobserver_types::schemars::JsonSchema;
14use printobserver_types::serde::{Deserialize, Serialize};
15
16/// The filament one tool feeds, as a length and, when it is known, a volume.
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
18#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
19#[schemars(crate = "printobserver_types::schemars")]
20pub struct ToolFilament {
21    /// The tool, in the printer's own numbering.
22    pub tool: i64,
23    /// The length of filament fed, in millimetres.
24    pub length_mm: f64,
25    /// The volume of filament fed, in cubic centimetres, when the source
26    /// reported one.
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub volume_cm3: Option<f64>,
29}
30
31/// The last time a file was printed, and whether that print completed.
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
33#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
34#[schemars(crate = "printobserver_types::schemars")]
35pub struct FilePrint {
36    /// When it was printed.
37    pub at: Timestamp,
38    /// Whether that print completed.
39    // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
40    pub success: bool,
41}
42
43/// One file the printer holds, as its source describes it.
44#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
45#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
46#[schemars(crate = "printobserver_types::schemars")]
47pub struct FileFacts {
48    /// The file's name, as the source reported it.
49    pub file_name: String,
50    /// Where the file lives, in the source's own vocabulary.
51    pub origin: String,
52    /// The file's size in bytes, when the source reported it.
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub size_bytes: Option<i64>,
55    /// The filament the source's analysis of the file says each tool uses.
56    ///
57    /// Absent when the file has no analysis: one still pending, or a file the
58    /// source never analyses, such as one on an SD card.
59    #[serde(default, skip_serializing_if = "Option::is_none")]
60    pub filament: Option<Vec<ToolFilament>>,
61    /// How many prints of the file completed, by the source's own tally.
62    pub successes: i64,
63    /// How many prints of the file did not, by the source's own tally.
64    pub failures: i64,
65    /// The file's most recent print, when it has been printed.
66    #[serde(default, skip_serializing_if = "Option::is_none")]
67    pub last_print: Option<FilePrint>,
68}
69
70impl Sample for ToolFilament {
71    fn sample_full() -> Self {
72        Self {
73            tool: 0,
74            length_mm: 1_000.0,
75            volume_cm3: Some(2.41),
76        }
77    }
78
79    fn sample_minimal() -> Self {
80        Self {
81            volume_cm3: None,
82            ..Self::sample_full()
83        }
84    }
85}
86
87impl Sample for FilePrint {
88    fn sample_full() -> Self {
89        Self {
90            at: Timestamp::sample_full(),
91            success: true,
92        }
93    }
94}
95
96impl Sample for FileFacts {
97    fn sample_full() -> Self {
98        Self {
99            file_name: "benchy.gcode".to_owned(),
100            origin: "local".to_owned(),
101            size_bytes: Some(4_194_304),
102            filament: Some(vec![ToolFilament::sample_full()]),
103            successes: 3,
104            failures: 1,
105            last_print: Some(FilePrint::sample_full()),
106        }
107    }
108
109    fn sample_minimal() -> Self {
110        Self {
111            file_name: "benchy.gcode".to_owned(),
112            origin: "sdcard".to_owned(),
113            size_bytes: None,
114            filament: None,
115            successes: 0,
116            failures: 0,
117            last_print: None,
118        }
119    }
120}
121
122/// The XY rectangle every extruding move of a file lies inside, in
123/// millimetres.
124#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
125#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
126#[schemars(crate = "printobserver_types::schemars")]
127pub struct Footprint {
128    /// The smallest X.
129    pub min_x: f64,
130    /// The smallest Y.
131    pub min_y: f64,
132    /// The largest X.
133    pub max_x: f64,
134    /// The largest Y.
135    pub max_y: f64,
136}
137
138/// What a file printed up to a byte position in it: the highest Z any
139/// extruding move reached, and where in XY it laid material.
140#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
141#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
142#[schemars(crate = "printobserver_types::schemars")]
143pub struct PrintedExtent {
144    /// The highest Z an extruding move started or ended at, in millimetres.
145    pub tallest_mm: f64,
146    /// The XY rectangle of every extruding move.
147    pub footprint: Footprint,
148}
149
150impl Sample for Footprint {
151    fn sample_full() -> Self {
152        Self {
153            min_x: 90.0,
154            min_y: 90.0,
155            max_x: 110.0,
156            max_y: 110.0,
157        }
158    }
159
160    fn sample_minimal() -> Self {
161        Self {
162            min_x: 0.0,
163            min_y: 0.0,
164            max_x: 0.0,
165            max_y: 0.0,
166        }
167    }
168}
169
170impl Sample for PrintedExtent {
171    fn sample_full() -> Self {
172        Self {
173            tallest_mm: 6.0,
174            footprint: Footprint::sample_full(),
175        }
176    }
177
178    fn sample_minimal() -> Self {
179        Self {
180            tallest_mm: 0.2,
181            footprint: Footprint::sample_minimal(),
182        }
183    }
184}
185
186/// Where [`PrinterPort::read_gcode`](crate::PrinterPort::read_gcode) streams a
187/// file's G-code to, a piece at a time.
188///
189/// The bytes travel from the printer to the sink and never the other way, so a
190/// sink is no way for anything to reach a machine.
191pub trait GcodeSink: Send {
192    /// Take the next piece of the file, in order; `Break` stops the read, and
193    /// nothing more of the file is asked for.
194    fn take(&mut self, piece: &[u8]) -> ControlFlow<()>;
195}