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