Skip to main content

printobserver_core/records/
spool_command.rs

1//! The closed vocabulary of things an actor may do to the spool ledger.
2//!
3//! Each command is an action of its own kind, so a safety envelope grants it
4//! and the history records it exactly as it does a print action, and none of
5//! them asks anything of the machine.
6
7use printobserver_types::schemars::JsonSchema;
8use printobserver_types::serde::{Deserialize, Serialize};
9
10use super::action::{ActionKind, Actor};
11use super::ids::SpoolId;
12
13/// The whole vocabulary of the spool ledger, and there is no other.
14#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
15#[serde(
16    crate = "printobserver_types::serde",
17    tag = "command",
18    rename_all = "snake_case",
19    deny_unknown_fields
20)]
21#[schemars(crate = "printobserver_types::schemars")]
22pub enum SpoolCommand {
23    /// Register a spool.
24    RegisterSpool {
25        /// The operator's own label for it.
26        name: String,
27        /// The material, such as `PLA`.
28        material: String,
29        /// The filament's colour.
30        #[serde(default, skip_serializing_if = "Option::is_none")]
31        color: Option<String>,
32        /// The filament's diameter, in millimetres; 1.75 when absent.
33        #[serde(default, skip_serializing_if = "Option::is_none")]
34        diameter_mm: Option<f64>,
35        /// The material's density, in grams per cubic centimetre; the known
36        /// density of the material when absent, and a material with none known
37        /// is refused.
38        #[serde(default, skip_serializing_if = "Option::is_none")]
39        density_g_cm3: Option<f64>,
40        /// The net filament on the spool when full, in grams.
41        initial_g: f64,
42        /// The filament on it now, in grams; the full figure when absent.
43        #[serde(default, skip_serializing_if = "Option::is_none")]
44        remaining_g: Option<f64>,
45        /// The empty spool's own weight, in grams.
46        #[serde(default, skip_serializing_if = "Option::is_none")]
47        tare_g: Option<f64>,
48        /// Why the actor is asking for this.
49        reason: String,
50        /// Who is asking.
51        actor: Actor,
52    },
53    /// Load a spool onto a tool.
54    ///
55    /// Onto an occupied tool it is a swap, and the spool there is unloaded in
56    /// the same write. Naming the spool already on the tool confirms it was
57    /// never swapped, which is accepted only while that tool holds a pending
58    /// charge or that spool holds a filament-change mark.
59    LoadSpool {
60        /// The spool to load.
61        spool_id: SpoolId,
62        /// The tool, in the printer's own numbering.
63        tool: i64,
64        /// Why the actor is asking for this.
65        reason: String,
66        /// Who is asking.
67        actor: Actor,
68    },
69    /// Unload a spool from its tool.
70    UnloadSpool {
71        /// The spool to unload.
72        spool_id: SpoolId,
73        /// Why the actor is asking for this.
74        reason: String,
75        /// Who is asking.
76        actor: Actor,
77    },
78    /// Correct a spool's remaining figure, from exactly one of the two; a
79    /// command naming both or neither is refused as `spool_refused`.
80    // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
81    AdjustSpool {
82        /// The spool to correct.
83        spool_id: SpoolId,
84        /// The filament left, in grams.
85        #[serde(default, skip_serializing_if = "Option::is_none")]
86        remaining_g: Option<f64>,
87        /// A scale reading of the spool, in grams, which needs its tare.
88        #[serde(default, skip_serializing_if = "Option::is_none")]
89        gross_g: Option<f64>,
90        /// Why the actor is asking for this.
91        reason: String,
92        /// Who is asking.
93        actor: Actor,
94    },
95    /// Retire a spool, after which it refuses every command.
96    RetireSpool {
97        /// The spool to retire.
98        spool_id: SpoolId,
99        /// Why the actor is asking for this.
100        reason: String,
101        /// Who is asking.
102        actor: Actor,
103    },
104}
105
106impl SpoolCommand {
107    /// Which action this is, without its payload.
108    #[must_use]
109    pub const fn kind(&self) -> ActionKind {
110        match self {
111            Self::RegisterSpool { .. } => ActionKind::RegisterSpool,
112            Self::LoadSpool { .. } => ActionKind::LoadSpool,
113            Self::UnloadSpool { .. } => ActionKind::UnloadSpool,
114            Self::AdjustSpool { .. } => ActionKind::AdjustSpool,
115            Self::RetireSpool { .. } => ActionKind::RetireSpool,
116        }
117    }
118
119    /// Why the actor asked for this.
120    #[must_use]
121    pub fn reason(&self) -> &str {
122        match self {
123            Self::RegisterSpool { reason, .. }
124            | Self::LoadSpool { reason, .. }
125            | Self::UnloadSpool { reason, .. }
126            | Self::AdjustSpool { reason, .. }
127            | Self::RetireSpool { reason, .. } => reason,
128        }
129    }
130
131    /// Who asked for this.
132    #[must_use]
133    pub const fn actor(&self) -> &Actor {
134        match self {
135            Self::RegisterSpool { actor, .. }
136            | Self::LoadSpool { actor, .. }
137            | Self::UnloadSpool { actor, .. }
138            | Self::AdjustSpool { actor, .. }
139            | Self::RetireSpool { actor, .. } => actor,
140        }
141    }
142}