1use printobserver_types::schemars::JsonSchema;
5use printobserver_types::serde::{Deserialize, Serialize};
6use printobserver_types::{FileName, Timestamp};
7
8use super::ids::SpoolId;
9use super::spool::{UnverifiedMark, UnverifiedMarks};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
13#[serde(crate = "printobserver_types::serde", rename_all = "snake_case")]
14#[schemars(crate = "printobserver_types::schemars")]
15pub enum PreflightVerdict {
16 Passed,
18 Unchecked,
20 Insufficient,
22 UnverifiedSpool,
24 NoAnalysis,
28 Overridden,
30 Warned,
33}
34
35#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
37#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
38#[schemars(crate = "printobserver_types::schemars")]
39pub struct PreflightTool {
40 pub tool: i64,
42 pub result: PreflightToolResult,
44}
45
46#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
49#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
50#[schemars(crate = "printobserver_types::schemars")]
51pub struct PreflightSpool {
52 pub id: SpoolId,
54 pub name: String,
56}
57
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
61#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
62#[schemars(crate = "printobserver_types::schemars")]
63pub struct PreflightNeed {
64 pub required_g: f64,
67 pub buffer_g: f64,
69}
70
71#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
73#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
74#[schemars(crate = "printobserver_types::schemars")]
75pub struct ShortSpool {
76 pub spool: PreflightSpool,
78 pub remaining_g: f64,
80 pub need: PreflightNeed,
82 #[serde(default, skip_serializing_if = "Vec::is_empty")]
85 pub marks: Vec<UnverifiedMark>,
86}
87
88#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
90#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
91#[schemars(crate = "printobserver_types::schemars")]
92pub struct DoubtedSpool {
93 pub spool: PreflightSpool,
95 pub remaining_g: f64,
97 #[serde(default, skip_serializing_if = "Option::is_none")]
99 pub need: Option<PreflightNeed>,
100 pub marks: UnverifiedMarks,
102}
103
104#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
106#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
107#[schemars(crate = "printobserver_types::schemars")]
108pub struct UnanalysedSpool {
109 pub spool: PreflightSpool,
111 pub remaining_g: f64,
113}
114
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
117#[serde(
118 crate = "printobserver_types::serde",
119 tag = "verdict",
120 rename_all = "snake_case",
121 deny_unknown_fields
122)]
123#[schemars(crate = "printobserver_types::schemars")]
124pub enum PreflightObjection {
125 Insufficient(ShortSpool),
127 UnverifiedSpool(DoubtedSpool),
129 NoAnalysis(UnanalysedSpool),
131}
132
133impl PreflightObjection {
134 #[must_use]
136 pub const fn verdict(&self) -> PreflightVerdict {
137 match self {
138 Self::Insufficient(_) => PreflightVerdict::Insufficient,
139 Self::UnverifiedSpool(_) => PreflightVerdict::UnverifiedSpool,
140 Self::NoAnalysis(_) => PreflightVerdict::NoAnalysis,
141 }
142 }
143}
144
145#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
148#[serde(
149 crate = "printobserver_types::serde",
150 tag = "verdict",
151 rename_all = "snake_case",
152 deny_unknown_fields
153)]
154#[schemars(crate = "printobserver_types::schemars")]
155pub enum PreflightToolResult {
156 Passed {
159 spool: PreflightSpool,
161 remaining_g: f64,
163 need: PreflightNeed,
165 },
166 Unchecked,
168 Insufficient(ShortSpool),
170 UnverifiedSpool(DoubtedSpool),
172 NoAnalysis(UnanalysedSpool),
175 Overridden {
178 objection: PreflightObjection,
180 },
181 Warned {
184 objection: PreflightObjection,
186 },
187}
188
189impl PreflightToolResult {
190 #[must_use]
192 pub const fn verdict(&self) -> PreflightVerdict {
193 match self {
194 Self::Passed { .. } => PreflightVerdict::Passed,
195 Self::Unchecked => PreflightVerdict::Unchecked,
196 Self::Insufficient(_) => PreflightVerdict::Insufficient,
197 Self::UnverifiedSpool(_) => PreflightVerdict::UnverifiedSpool,
198 Self::NoAnalysis(_) => PreflightVerdict::NoAnalysis,
199 Self::Overridden { .. } => PreflightVerdict::Overridden,
200 Self::Warned { .. } => PreflightVerdict::Warned,
201 }
202 }
203}
204
205#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
207#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
208#[schemars(crate = "printobserver_types::schemars")]
209pub struct Preflight {
210 pub file_name: String,
212 pub tools: Vec<PreflightTool>,
214 pub verdict: PreflightVerdict,
220 #[serde(default, skip_serializing_if = "Option::is_none")]
223 pub material: Option<MaterialResult>,
224 #[serde(default, skip_serializing_if = "Option::is_none")]
228 pub sheet: Option<SheetResult>,
229 #[serde(default, skip_serializing_if = "Vec::is_empty")]
234 pub segments: Vec<PreflightSegment>,
235 pub checked_at: Timestamp,
237}
238
239#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
242#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
243#[schemars(
244 crate = "printobserver_types::schemars",
245 transform = crate::gcode_index::null_is_sent
246)]
247pub struct PreflightSegment {
248 pub segment: u32,
250 pub color: Option<String>,
252 pub filament_mm: f64,
254 pub required_g: Option<f64>,
257 pub spool_id: Option<SpoolId>,
260}
261
262fn named_text(text: &str, what: &str) -> Result<String, String> {
264 let trimmed = text.trim();
265 if trimmed.is_empty() {
266 return Err(format!("{what} is named by something other than space"));
267 }
268 Ok(trimmed.to_owned())
269}
270
271#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
274#[serde(
275 crate = "printobserver_types::serde",
276 try_from = "String",
277 into = "String"
278)]
279pub struct MaterialName(String);
280
281impl JsonSchema for MaterialName {
282 fn schema_name() -> std::borrow::Cow<'static, str> {
283 std::borrow::Cow::Borrowed("MaterialName")
284 }
285
286 fn json_schema(
287 _generator: &mut printobserver_types::schemars::SchemaGenerator,
288 ) -> printobserver_types::schemars::Schema {
289 printobserver_types::schemars::json_schema!({
290 "type": "string",
291 "title": "MaterialName",
292 "description": "A filament material as a spool's registration or a slicer's settings name it, such as `PLA`: never blank, and trimmed of surrounding space as it is read.",
293 "minLength": 1,
294 "pattern": "\\S"
295 })
296 }
297}
298
299impl MaterialName {
300 #[must_use]
302 pub fn as_str(&self) -> &str {
303 &self.0
304 }
305
306 #[must_use]
308 pub fn same(&self, other: &Self) -> bool {
309 self.0.to_lowercase() == other.0.to_lowercase()
310 }
311}
312
313impl TryFrom<String> for MaterialName {
314 type Error = String;
315
316 fn try_from(text: String) -> Result<Self, Self::Error> {
317 named_text(&text, "a material").map(Self)
318 }
319}
320
321impl From<MaterialName> for String {
322 fn from(name: MaterialName) -> Self {
323 name.0
324 }
325}
326
327impl core::fmt::Display for MaterialName {
328 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
329 formatter.write_str(&self.0)
330 }
331}
332
333#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
336#[serde(
337 crate = "printobserver_types::serde",
338 try_from = "String",
339 into = "String"
340)]
341pub struct SheetId(String);
342
343impl JsonSchema for SheetId {
344 fn schema_name() -> std::borrow::Cow<'static, str> {
345 std::borrow::Cow::Borrowed("SheetId")
346 }
347
348 fn json_schema(
349 _generator: &mut printobserver_types::schemars::SchemaGenerator,
350 ) -> printobserver_types::schemars::Schema {
351 printobserver_types::schemars::json_schema!({
352 "type": "string",
353 "title": "SheetId",
354 "description": "A build sheet's identifier as the sheet table names it, such as `smooth-pei`: never blank, and trimmed of surrounding space as it is read.",
355 "minLength": 1,
356 "pattern": "\\S"
357 })
358 }
359}
360
361impl SheetId {
362 #[must_use]
364 pub fn as_str(&self) -> &str {
365 &self.0
366 }
367
368 #[must_use]
370 pub fn same(&self, other: &Self) -> bool {
371 self.0.to_lowercase() == other.0.to_lowercase()
372 }
373}
374
375impl TryFrom<String> for SheetId {
376 type Error = String;
377
378 fn try_from(text: String) -> Result<Self, Self::Error> {
379 named_text(&text, "a sheet").map(Self)
380 }
381}
382
383impl From<SheetId> for String {
384 fn from(sheet: SheetId) -> Self {
385 sheet.0
386 }
387}
388
389impl core::fmt::Display for SheetId {
390 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
391 formatter.write_str(&self.0)
392 }
393}
394
395#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
398#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
399#[schemars(crate = "printobserver_types::schemars")]
400pub struct MaterialComparison {
401 pub tool: i64,
403 pub spool: PreflightSpool,
405 pub loaded: MaterialName,
407 pub sliced_for: MaterialName,
409}
410
411#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
413#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
414#[schemars(crate = "printobserver_types::schemars")]
415pub struct UncomparedMaterial {
416 pub tool: i64,
418 pub spool: PreflightSpool,
420 pub loaded: MaterialName,
422 pub file_name: FileName,
424}
425
426#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
428#[serde(
429 crate = "printobserver_types::serde",
430 tag = "cause",
431 rename_all = "snake_case",
432 deny_unknown_fields
433)]
434#[schemars(crate = "printobserver_types::schemars")]
435pub enum MaterialObjection {
436 Mismatch(MaterialComparison),
438 Undeclared(UncomparedMaterial),
441 Unread {
443 tool: i64,
445 spool: PreflightSpool,
447 loaded: MaterialName,
449 file_name: FileName,
451 detail: String,
453 },
454}
455
456#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
459#[serde(
460 crate = "printobserver_types::serde",
461 tag = "verdict",
462 rename_all = "snake_case",
463 deny_unknown_fields
464)]
465#[schemars(crate = "printobserver_types::schemars")]
466pub enum MaterialResult {
467 Passed(MaterialComparison),
469 Unchecked,
472 Refused {
474 objection: MaterialObjection,
476 },
477 Overridden {
480 objection: MaterialObjection,
482 },
483 Warned {
486 objection: MaterialObjection,
488 },
489}
490
491#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
494#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
495#[schemars(crate = "printobserver_types::schemars")]
496pub struct SheetComparison {
497 pub sheet: SheetId,
499 pub material: MaterialName,
501 pub allowed: Vec<SheetId>,
503}
504
505#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
507#[serde(
508 crate = "printobserver_types::serde",
509 tag = "cause",
510 rename_all = "snake_case",
511 deny_unknown_fields
512)]
513#[schemars(crate = "printobserver_types::schemars")]
514pub enum SheetObjection {
515 NotAllowed(SheetComparison),
517 UnlistedMaterial {
519 sheet: SheetId,
521 material: MaterialName,
523 },
524 UnknownSheet {
526 sheet: SheetId,
528 material: MaterialName,
530 known: Vec<SheetId>,
532 },
533}
534
535#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
538#[serde(
539 crate = "printobserver_types::serde",
540 tag = "verdict",
541 rename_all = "snake_case",
542 deny_unknown_fields
543)]
544#[schemars(crate = "printobserver_types::schemars")]
545pub enum SheetResult {
546 Passed(SheetComparison),
548 Unchecked,
551 Undeclared,
554 Refused {
556 objection: SheetObjection,
558 },
559 Overridden {
562 objection: SheetObjection,
564 },
565 Warned {
568 objection: SheetObjection,
570 },
571}