Skip to main content

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