Skip to main content

printobserver_printer_api/
push.rs

1//! What a printer pushes on its own, and the closed set of commands it may be
2//! sent.
3//!
4//! A poll reads the printer's state; some of what matters never shows in one.
5//! A firmware reset that lands the machine idle reads exactly like a cancel,
6//! and a reset while idle changes nothing a read reports at all. The printer's
7//! own push channel says so, and [`PrinterPush`] is that channel's vocabulary
8//! here: what [`PrinterPort::push_events`](crate::PrinterPort::push_events)
9//! answers.
10//!
11//! [`PrinterCommand`] is the other direction: the only commands
12//! [`PrinterPort::send_command`](crate::PrinterPort::send_command) takes. It is
13//! a closed vocabulary rather than a line, so nothing a caller writes reaches a
14//! machine through it; each adapter renders a variant to its own line.
15
16use core::pin::Pin;
17use core::task::{Context, Poll};
18
19/// A stream of values that arrive one at a time, asynchronously.
20///
21/// The one shape [`PrinterPort::push_events`](crate::PrinterPort::push_events)
22/// answers in, declared here so that this port depends on nothing but the type
23/// crate. It is the same contract as the ecosystem's own `Stream`: `Ready(None)`
24/// is the end, after which nothing more arrives.
25pub trait Stream {
26    /// What the stream yields.
27    type Item;
28
29    /// Take the next value when one has arrived, `Ready(None)` at the end, or
30    /// `Pending` — having arranged for the context's waker to be woken when
31    /// either is ready.
32    fn poll_next(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>>;
33}
34
35/// A stream this port answers, in the one shape a trait object can carry.
36pub type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + Send + 'a>>;
37
38/// One thing a printer pushed on its own.
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub enum PrinterPush {
41    /// The printer reset: its firmware restarted, and every state it held —
42    /// the job, the position, the heaters — is gone.
43    Reset,
44    /// The printer reported an error.
45    Error {
46        /// What kind of error the source reported.
47        reason: PrinterErrorReason,
48    },
49    /// One line of the printer's terminal, as the source shows it.
50    Log {
51        /// The line.
52        line: String,
53    },
54}
55
56/// What kind of error a printer pushed.
57#[derive(Debug, Clone, PartialEq, Eq)]
58pub enum PrinterErrorReason {
59    /// The printer's own firmware reported it: a thermal runaway, a kill, a
60    /// halt. What ends a print `failed`.
61    Firmware,
62    /// Anything else the source reported, in its own word for it: a resend it
63    /// could not satisfy, the host losing the line.
64    Other(String),
65}
66
67/// A command a printer may be sent: a closed vocabulary, each variant rendered
68/// by the adapter to its own line.
69#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70pub enum PrinterCommand {
71    /// Report where the print head is. The answer arrives on the push channel.
72    ReportPosition,
73}