Skip to main content

printobserver_core/
error.rs

1//! The one error type core answers its callers with.
2
3use printobserver_printer_api::PrinterError;
4use printobserver_store_api::StoreError;
5use printobserver_supervisor_api::SupervisorError;
6use printobserver_types::PrintId;
7use printobserver_vision_api::VisionError;
8
9/// Why core could not do what it was asked.
10///
11/// A port's own error is carried rather than flattened into a message, because
12/// a caller that has somewhere to put a failure needs to know which port failed
13/// and how — the store's initial event append in particular, which is the one
14/// failure core cannot record anywhere and so answers to its caller.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum CoreError {
17    /// The store failed, in its own words.
18    Store(StoreError),
19    /// The printer failed, in its own words.
20    Printer(PrinterError),
21    /// The vision port failed, in its own words.
22    Vision(VisionError),
23    /// The supervising agent's harness failed, in its own words.
24    Supervisor(SupervisorError),
25    /// There is no print by that identifier.
26    NoSuchPrint {
27        /// The identifier nothing was found for.
28        print_id: PrintId,
29    },
30    /// An instant this crate computed is not representable.
31    Unrepresentable {
32        /// What could not be represented.
33        detail: String,
34    },
35}
36
37impl core::fmt::Display for CoreError {
38    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
39        match self {
40            Self::Store(error) => write!(formatter, "the store failed: {error}"),
41            Self::Printer(error) => write!(formatter, "the printer failed: {error}"),
42            Self::Vision(error) => write!(formatter, "the vision port failed: {error}"),
43            Self::Supervisor(error) => write!(formatter, "the supervisor failed: {error}"),
44            Self::NoSuchPrint { print_id } => write!(formatter, "there is no print {print_id}"),
45            Self::Unrepresentable { detail } => {
46                write!(formatter, "an instant is not representable: {detail}")
47            }
48        }
49    }
50}
51
52impl core::error::Error for CoreError {}
53
54impl From<StoreError> for CoreError {
55    fn from(error: StoreError) -> Self {
56        Self::Store(error)
57    }
58}