Skip to main content

printobserver_core/records/
spool.rs

1//! A registered spool, and how far its remaining figure can be trusted.
2//!
3//! A spool's remaining figure is only worth having when it can be explained, so
4//! a spool carries every reason its figure is in doubt rather than one flag: a
5//! mark is added when something happened that could not be charged exactly,
6//! and is cleared only by the command that settles that doubt. The rule for
7//! which command clears which mark is implemented once, by
8//! [`marks_cleared_by`](crate::filament::marks_cleared_by).
9
10use printobserver_types::schemars::JsonSchema;
11use printobserver_types::serde::de::Error as _;
12use printobserver_types::serde::{Deserialize, Deserializer, Serialize};
13use printobserver_types::{PrintId, Timestamp};
14
15use super::ids::SpoolId;
16
17/// One spool the operator registered, and what is known of it now.
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
19#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
20#[schemars(crate = "printobserver_types::schemars")]
21pub struct SpoolRecord {
22    /// This spool's identifier, minted when it was registered.
23    pub id: SpoolId,
24    /// The operator's own label for the spool; never empty.
25    pub name: String,
26    /// The material, as given and trimmed, such as `PLA`; never empty.
27    pub material: String,
28    /// The filament's colour, when the operator gave one.
29    #[serde(default, skip_serializing_if = "Option::is_none")]
30    pub color: Option<String>,
31    /// The filament's diameter, in millimetres; above zero.
32    pub diameter_mm: f64,
33    /// The material's density, in grams per cubic centimetre; above zero.
34    pub density_g_cm3: f64,
35    /// The net filament on the spool when it was full, in grams; above zero.
36    pub initial_g: f64,
37    /// The filament left, in grams.
38    ///
39    /// Always the initial figure less every charge plus every correction. A
40    /// charge is never refused or clamped, so this may go below zero.
41    pub remaining_g: f64,
42    /// The empty spool's own weight, in grams, when the operator gave it.
43    #[serde(default, skip_serializing_if = "Option::is_none")]
44    pub tare_g: Option<f64>,
45    /// The tool the spool is loaded on, when it is loaded.
46    // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub loaded_on_tool: Option<i64>,
49    /// How far the remaining figure can be trusted.
50    pub trust: SpoolTrust,
51    /// When the spool was registered.
52    pub registered_at: Timestamp,
53    /// When anything about the spool last changed.
54    pub updated_at: Timestamp,
55    /// When the spool was retired, if it has been.
56    // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
57    #[serde(default, skip_serializing_if = "Option::is_none")]
58    pub retired_at: Option<Timestamp>,
59}
60
61/// How far a spool's remaining figure can be trusted.
62#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
63#[serde(
64    crate = "printobserver_types::serde",
65    tag = "state",
66    rename_all = "snake_case",
67    deny_unknown_fields
68)]
69#[schemars(crate = "printobserver_types::schemars")]
70pub enum SpoolTrust {
71    /// Every charge against the spool was taken exactly.
72    Verified,
73    /// At least one thing happened that could not be charged exactly.
74    Unverified {
75        /// Every reason the figure is in doubt, oldest first, at most one per
76        /// distinct reason; never empty.
77        // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
78        #[serde(deserialize_with = "at_least_one_mark")]
79        #[schemars(length(min = 1))]
80        marks: Vec<UnverifiedMark>,
81    },
82}
83
84/// Read an unverified spool's marks, refusing none: a spool holding no mark is
85/// `verified`, and one written as unverified with nothing to say why is not a
86/// state this ledger has.
87fn at_least_one_mark<'de, D: Deserializer<'de>>(
88    deserializer: D,
89) -> Result<Vec<UnverifiedMark>, D::Error> {
90    let marks = Vec::<UnverifiedMark>::deserialize(deserializer)?;
91    if marks.is_empty() {
92        return Err(D::Error::custom(
93            "an unverified spool carries at least one mark; one with none is verified",
94        ));
95    }
96    Ok(marks)
97}
98
99/// One reason a spool's figure is in doubt, and since when.
100#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
101#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
102#[schemars(crate = "printobserver_types::schemars")]
103pub struct UnverifiedMark {
104    /// Why the figure is in doubt.
105    pub reason: UnverifiedReason,
106    /// When the spool was marked for it.
107    pub since: Timestamp,
108}
109
110/// Why a spool's figure is in doubt.
111#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
112#[serde(
113    crate = "printobserver_types::serde",
114    tag = "reason",
115    rename_all = "snake_case",
116    deny_unknown_fields
117)]
118#[schemars(crate = "printobserver_types::schemars")]
119pub enum UnverifiedReason {
120    /// A print ran on the spool's tool and nothing could be charged, because
121    /// the file had no analysis — an SD-card file, for one.
122    UnchargedPrint {
123        /// The print nothing could be charged for.
124        print_id: PrintId,
125    },
126    /// A print this system never saw ran and did not complete.
127    UnaccountedPrint {
128        /// The file that was printed.
129        file_name: String,
130        /// When it was printed.
131        printed_at: Timestamp,
132    },
133    /// A filament change was signalled and nobody has said which spool is now
134    /// loaded. The spool was charged up to a position read when the signal was
135    /// handled.
136    FilamentChangeUnconfirmed {
137        /// The print the change was signalled during.
138        print_id: PrintId,
139    },
140    /// A filament change was signalled, nobody has said which spool is now
141    /// loaded, and no fresh position could be read at the signal: the spool was
142    /// charged only up to the last recorded position, so its figure may be
143    /// overstated.
144    FilamentChangeUnconfirmedPosition {
145        /// The print the change was signalled during.
146        print_id: PrintId,
147    },
148    /// A file's print tally was reset, and it cannot be told which of the
149    /// prints since the re-upload were already settled.
150    TallyReset {
151        /// The file whose tally was reset.
152        file_name: String,
153        /// Where the file lives, in the printer's own vocabulary.
154        origin: String,
155    },
156}
157
158/// The five reasons a spool may be marked for, without their payloads.
159#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
160#[serde(crate = "printobserver_types::serde", rename_all = "snake_case")]
161pub enum UnverifiedReasonKind {
162    /// [`UnverifiedReason::UnchargedPrint`].
163    UnchargedPrint,
164    /// [`UnverifiedReason::UnaccountedPrint`].
165    UnaccountedPrint,
166    /// [`UnverifiedReason::FilamentChangeUnconfirmed`].
167    FilamentChangeUnconfirmed,
168    /// [`UnverifiedReason::FilamentChangeUnconfirmedPosition`].
169    FilamentChangeUnconfirmedPosition,
170    /// [`UnverifiedReason::TallyReset`].
171    TallyReset,
172}
173
174impl UnverifiedReason {
175    /// Which reason this is, without its payload.
176    #[must_use]
177    pub const fn kind(&self) -> UnverifiedReasonKind {
178        match self {
179            Self::UnchargedPrint { .. } => UnverifiedReasonKind::UnchargedPrint,
180            Self::UnaccountedPrint { .. } => UnverifiedReasonKind::UnaccountedPrint,
181            Self::FilamentChangeUnconfirmed { .. } => {
182                UnverifiedReasonKind::FilamentChangeUnconfirmed
183            }
184            Self::FilamentChangeUnconfirmedPosition { .. } => {
185                UnverifiedReasonKind::FilamentChangeUnconfirmedPosition
186            }
187            Self::TallyReset { .. } => UnverifiedReasonKind::TallyReset,
188        }
189    }
190}
191
192/// One mark a spool command cleared, and the spool it was cleared from.
193#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
194#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
195#[schemars(crate = "printobserver_types::schemars")]
196pub struct ClearedMark {
197    /// The spool the mark was cleared from.
198    pub spool_id: SpoolId,
199    /// The reason that no longer stands.
200    pub reason: UnverifiedReason,
201}
202
203/// One spool loaded on one tool, as a print started.
204#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
205#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
206#[schemars(crate = "printobserver_types::schemars")]
207pub struct LoadedSpool {
208    /// The tool, in the printer's own numbering.
209    pub tool: i64,
210    /// The spool loaded on it.
211    pub spool_id: SpoolId,
212}