printobserver_printer_api/
file.rs1use 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#[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 pub tool: i64,
23 pub length_mm: f64,
25 #[serde(default, skip_serializing_if = "Option::is_none")]
28 pub volume_cm3: Option<f64>,
29}
30
31#[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 pub at: Timestamp,
38 pub success: bool,
41}
42
43#[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 pub file_name: String,
50 pub origin: String,
52 #[serde(default, skip_serializing_if = "Option::is_none")]
54 pub size_bytes: Option<i64>,
55 #[serde(default, skip_serializing_if = "Option::is_none")]
60 pub filament: Option<Vec<ToolFilament>>,
61 pub successes: i64,
63 pub failures: i64,
65 #[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#[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 pub min_x: f64,
130 pub min_y: f64,
132 pub max_x: f64,
134 pub max_y: f64,
136}
137
138#[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 pub tallest_mm: f64,
146 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
186pub trait GcodeSink: Send {
192 fn take(&mut self, piece: &[u8]) -> ControlFlow<()>;
195}