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