Skip to main content

printobserver_panels/
error.rs

1//! Why a check image or an alignment verdict could not be made.
2
3/// Which property of a frame differs from the calibration's.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum FrameField {
6    /// Its size in pixels.
7    Resolution,
8    /// The snapshot URL it came from.
9    Stream,
10}
11
12impl FrameField {
13    /// The field's name, as a refusal names it.
14    #[must_use]
15    pub const fn as_str(self) -> &'static str {
16        match self {
17            Self::Resolution => "resolution",
18            Self::Stream => "stream",
19        }
20    }
21}
22
23/// Why nothing was drawn.
24#[derive(Debug, Clone, PartialEq)]
25pub enum ComposeError {
26    /// The frame is not the calibration's: another size, or another stream.
27    /// It is refused, never scaled.
28    // llmlint: ignore[invalid_states_unrepresentable] See suppressions.toml.
29    FrameMismatch {
30        /// What differs.
31        field: FrameField,
32        /// The calibration's.
33        expected: String,
34        /// The frame's.
35        got: String,
36    },
37    /// At least two visible landmarks have moved past the calibration's
38    /// tolerance: the camera has moved since it was calibrated.
39    CameraMoved {
40        /// The landmark that moved furthest.
41        landmark: String,
42        /// How far it moved, in pixels.
43        offset_px: f64,
44    },
45    /// Fewer than two landmarks are visible, so whether the camera has moved
46    /// cannot be told.
47    DriftUnverified,
48    /// No point of the geometry to draw lands in the frame.
49    OutlineOffFrame,
50    /// A frame is not a JPEG this crate can decode.
51    Undecodable {
52        /// What the decoder said.
53        detail: String,
54    },
55    /// The check image could not be encoded.
56    Encode {
57        /// What the encoder said.
58        detail: String,
59    },
60}
61
62impl core::fmt::Display for ComposeError {
63    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
64        match self {
65            Self::FrameMismatch {
66                field,
67                expected,
68                got,
69            } => write!(
70                formatter,
71                "the frame's {} is {got}, and the calibration's is {expected}",
72                field.as_str()
73            ),
74            Self::CameraMoved {
75                landmark,
76                offset_px,
77            } => write!(
78                formatter,
79                "the camera has moved since it was calibrated: {landmark} is {offset_px:.1} px from where it was"
80            ),
81            Self::DriftUnverified => formatter.write_str(
82                "fewer than two of the calibration's landmarks are visible, so whether the camera has moved is unknown",
83            ),
84            Self::OutlineOffFrame => {
85                formatter.write_str("nothing of the planned geometry lands in the frame")
86            }
87            Self::Undecodable { detail } => write!(formatter, "the frame is not a JPEG: {detail}"),
88            Self::Encode { detail } => write!(formatter, "the check image: {detail}"),
89        }
90    }
91}
92
93impl std::error::Error for ComposeError {}