Skip to main content

printobserver_core/
listing.rs

1//! Every print this system holds, and the one the printer is running now.
2//!
3//! A print is otherwise known here only once a provider reports on it, and an
4//! identifier nothing lists is one nobody can act with. So reading the listing
5//! also **adopts** the job the printer reports: when that job is in one of
6//! [`ACTIVE_STATES`] and no open print carries its file name, one is opened for
7//! it, and the listing names it active. An alert about the same job later
8//! attaches its provider's identifier to that print rather than opening a
9//! second, which is [`Supervisor::handle_event`]'s own rule.
10//!
11//! # What it asks of the printer
12//!
13//! One read of the job, which is a read rather than an action: nothing here
14//! reaches an action method of the printer port, so adopting a job commands
15//! nothing at the machine.
16//!
17//! # What an adopted print records
18//!
19//! The store records a print's state when it opens it and when it ends it, and
20//! at no point between. An adopted print is opened exactly as an alert opens
21//! one, so it records `printing` whether or not the machine is paused at that
22//! moment: what the machine is doing now is the status read's answer, and never
23//! the record's.
24
25use printobserver_printer_api::PrinterState;
26use printobserver_types::PrintId;
27
28use crate::error::CoreError;
29use crate::records::PrintRecord;
30use crate::supervisor::Supervisor;
31
32/// The job states a printer's job is adoptable in: a job it is running, and a
33/// job it has paused part-way through.
34///
35/// Every other state is a machine with no job in progress — idle, cancelling
36/// the one it had, in error, unreachable, or saying something this vocabulary
37/// does not name — and a print opened for one of those would be a record of a
38/// job that is not there.
39pub const ACTIVE_STATES: [PrinterState; 2] = [PrinterState::Printing, PrinterState::Paused];
40
41/// Every print this system holds, and the one the printer is running now.
42#[derive(Debug, Clone, PartialEq)]
43pub struct PrintListing {
44    /// Every print, ended or not, most recently opened first.
45    pub prints: Vec<PrintRecord>,
46    /// The print the printer's job belongs to, present exactly when the printer
47    /// reports a job in one of [`ACTIVE_STATES`] with a file name.
48    pub active: Option<PrintId>,
49}
50
51impl Supervisor {
52    /// Every print, and the one the printer's job belongs to — **opening** one
53    /// for that job when no open print carries its file name.
54    ///
55    /// Named for both halves, because it is not only a read: the listing is
56    /// what a caller asked for, and the print it may open is what makes that
57    /// listing name the job at all. A printer that cannot be read is a listing
58    /// with nothing active rather than a failure: the stored prints are still
59    /// the answer to which prints there are.
60    ///
61    /// # Errors
62    ///
63    /// Returns the store's own error when the prints could not be read, or a
64    /// print for the job could not be opened.
65    pub async fn list_and_adopt_prints(&self) -> Result<PrintListing, CoreError> {
66        let _resolving = self.resolving().await;
67        // The stored prints are read before the machine is asked what it is
68        // running. Which comes first changes no answer while the lock above is
69        // held; it is the order in which a second resolution the lock did not
70        // keep out would have read a listing that no longer stands.
71        let prints = self.stores().prints.prints().await?;
72        let running = self
73            .read_job()
74            .await
75            .ok()
76            .filter(|job| ACTIVE_STATES.contains(&job.state))
77            .and_then(|job| job.file_name);
78        let Some(file_name) = running else {
79            return Ok(PrintListing {
80                prints,
81                active: None,
82            });
83        };
84        // Most recently opened first, so the first open print carrying the
85        // name is the most recent of them.
86        if let Some(found) = prints
87            .iter()
88            .find(|print| print.ended_at.is_none() && print.file_name.as_ref() == Some(&file_name))
89        {
90            let active = Some(found.id);
91            return Ok(PrintListing { prints, active });
92        }
93        let opened = self
94            .stores()
95            .prints
96            .open_print(None, Some(file_name))
97            .await?;
98        Ok(PrintListing {
99            prints: self.stores().prints.prints().await?,
100            active: Some(opened.id),
101        })
102    }
103}