printobserver_printer_api/lib.rs
1//! `printobserver-printer-api`.
2//!
3//! Owns: the port the physical printer speaks through — the trait for reading
4//! printer and job state and for asking the machine to do one of the bounded
5//! things the action vocabulary names, plus that port's own error type — and
6//! the printer domain's vocabulary: the state a printer reports ([`state`]),
7//! the closed set of things an adjustment may change ([`adjustable`]), the
8//! snapshots those reads answer ([`snapshot`]) and the plausibility ranges
9//! their reported numbers are read against ([`ranges`]). A change to what a
10//! printer reports edits this crate and the adapters behind it, and nothing
11//! central.
12//!
13//! May depend on: `printobserver-types` only. A port that named an
14//! implementation would stop being a port.
15//!
16//! # What this port does not expose
17//!
18//! There is no method that accepts a command string, and that absence is the
19//! contract rather than an omission: no method here admits a byte sequence at
20//! all, and the only string anywhere in the closure reachable from a method's
21//! parameters is the one [`FileName`] wraps, which exactly one method —
22//! [`PrinterPort::start`] — takes. Every value a method takes is already
23//! bounded by the time it arrives: policy has ruled on it before it gets here.
24//!
25//! # Why the methods answer a boxed future
26//!
27//! Every method is asynchronous, and the trait is dyn-compatible and shareable
28//! across threads, because the supervision core holds every port behind
29//! `Arc<dyn Port>`. An `async fn` in a trait is not dyn-compatible, so each
30//! method answers a [`BoxFuture`] instead: the same asynchrony, in the one
31//! shape a trait object can carry.
32
33pub mod adjustable;
34pub mod ranges;
35pub mod snapshot;
36pub mod state;
37
38use core::future::Future;
39use core::pin::Pin;
40
41use printobserver_types::FileName;
42
43pub use adjustable::{Adjustable, AdjustableError};
44pub use ranges::{
45 COMPLETION_RANGE, FAN_PERCENT_RANGE, FEEDRATE_FACTOR_RANGE, FLOWRATE_FACTOR_RANGE,
46 HEATER_ACTUAL_C_RANGE, HEATER_OFFSET_C_RANGE, HEATER_TARGET_C_RANGE, RANGED_FIELDS,
47 RangedField,
48};
49pub use snapshot::{HeaterSnapshot, JobSnapshot, PrinterSnapshot};
50pub use state::PrinterState;
51
52/// A future this port's methods answer with, in the one shape a trait object
53/// can carry.
54pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
55
56/// Why the printer did not do what it was asked.
57#[derive(Debug, Clone, PartialEq, Eq)]
58pub enum PrinterError {
59 /// The printer could not be reached at all.
60 Unreachable {
61 /// What went wrong reaching it.
62 detail: String,
63 },
64 /// The printer refused the credentials.
65 Unauthorized {
66 /// What the printer said about them.
67 detail: String,
68 },
69 /// The printer refused the request, in its own words.
70 Refused {
71 /// The status the source answered with.
72 status: u16,
73 /// What the source said.
74 detail: String,
75 },
76 /// The printer is not in a state this request is valid from.
77 StateConflict {
78 /// What the source said about the state.
79 detail: String,
80 },
81 /// This printer cannot express that adjustable at all.
82 Unsupported {
83 /// The adjustable this printer does not have.
84 adjustable: Adjustable,
85 },
86 /// The printer answered something this port could not read.
87 Malformed {
88 /// What could not be read.
89 detail: String,
90 },
91}
92
93impl core::fmt::Display for PrinterError {
94 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
95 match self {
96 Self::Unreachable { detail } => {
97 write!(formatter, "the printer is unreachable: {detail}")
98 }
99 Self::Unauthorized { detail } => {
100 write!(formatter, "the printer refused the credentials: {detail}")
101 }
102 Self::Refused { status, detail } => {
103 write!(
104 formatter,
105 "the printer refused the request with {status}: {detail}"
106 )
107 }
108 Self::StateConflict { detail } => {
109 write!(
110 formatter,
111 "the printer is not in a state this is valid from: {detail}"
112 )
113 }
114 Self::Unsupported { adjustable } => {
115 write!(formatter, "this printer has no {adjustable}")
116 }
117 Self::Malformed { detail } => {
118 write!(
119 formatter,
120 "the printer answered something unreadable: {detail}"
121 )
122 }
123 }
124 }
125}
126
127impl core::error::Error for PrinterError {}
128
129/// The port the physical printer speaks through.
130///
131/// Every method is asynchronous, the trait is dyn-compatible, and it is
132/// shareable across threads, because the supervision core holds it behind
133/// `Arc<dyn PrinterPort>`.
134pub trait PrinterPort: Send + Sync {
135 /// Read the printer's current state.
136 fn snapshot(&self) -> BoxFuture<'_, Result<PrinterSnapshot, PrinterError>>;
137
138 /// Read the job the printer reports it is running.
139 fn job(&self) -> BoxFuture<'_, Result<JobSnapshot, PrinterError>>;
140
141 /// Start a print of a named file.
142 fn start(&self, file_name: FileName) -> BoxFuture<'_, Result<(), PrinterError>>;
143
144 /// Pause the print.
145 fn pause(&self) -> BoxFuture<'_, Result<(), PrinterError>>;
146
147 /// Resume the print.
148 fn resume(&self) -> BoxFuture<'_, Result<(), PrinterError>>;
149
150 /// Cancel the print.
151 fn cancel(&self) -> BoxFuture<'_, Result<(), PrinterError>>;
152
153 /// Set the feedrate multiplier, where one means one hundred percent.
154 fn set_feedrate_factor(&self, factor: f64) -> BoxFuture<'_, Result<(), PrinterError>>;
155
156 /// Set the flowrate multiplier, where one means one hundred percent.
157 fn set_flowrate_factor(&self, factor: f64) -> BoxFuture<'_, Result<(), PrinterError>>;
158
159 /// Set one tool's target temperature, in degrees Celsius.
160 fn set_tool_target_c(
161 &self,
162 tool: i64,
163 target_c: f64,
164 ) -> BoxFuture<'_, Result<(), PrinterError>>;
165
166 /// Set the bed's target temperature, in degrees Celsius.
167 fn set_bed_target_c(&self, target_c: f64) -> BoxFuture<'_, Result<(), PrinterError>>;
168
169 /// Set the part-cooling fan, in percent.
170 fn set_fan_percent(&self, percent: f64) -> BoxFuture<'_, Result<(), PrinterError>>;
171}