printobserver-core 0.19.0

Supervision logic for printobserver.
Documentation
//! What a start reads of a camera check: the operator's check, before a
//! print, that the camera is still aligned with its calibration on the empty
//! build sheet.
//!
//! The check itself — the frame, the fit, the correction it puts in force —
//! is the server's record, written into the printer's timeline as a
//! [`CAMERA_CHECKED_KIND`] event, because judging a frame against a
//! calibration is the server's. A start that names a check is held to three
//! of that record's fields, read back as a [`CameraCheckStanding`].

use printobserver_types::contract::Sample;
use printobserver_types::schemars::JsonSchema;
use printobserver_types::serde::{Deserialize, Serialize};

use crate::records::ids::CameraCheckId;
use crate::records::preflight::SheetId;

/// The kind a camera check is written into the printer timeline under.
pub const CAMERA_CHECKED_KIND: &str = "camera_checked";

/// What one camera check found.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", rename_all = "snake_case")]
#[schemars(crate = "printobserver_types::schemars")]
pub enum CameraCheckVerdict {
    /// The camera is aligned with its calibration, and the check carries the
    /// correction a print started under it is drawn through.
    Aligned,
    /// The camera moved, or the sheet lies beyond the tolerance: a
    /// calibration run is owed.
    NotAligned,
    /// The camera gave no frame that can show it.
    Unavailable,
}

impl CameraCheckVerdict {
    /// The verdict as the record spells it.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Aligned => "aligned",
            Self::NotAligned => "not_aligned",
            Self::Unavailable => "unavailable",
        }
    }
}

/// The fields of a camera check a start is held to, read back from its
/// record: every other field the record carries is the server's.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(crate = "printobserver_types::serde")]
pub struct CameraCheckStanding {
    /// The check.
    pub id: CameraCheckId,
    /// The sheet it was made on.
    pub sheet: SheetId,
    /// What it found.
    pub verdict: CameraCheckVerdict,
}

/// Why a start naming a camera check was refused.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(
    crate = "printobserver_types::serde",
    tag = "objection",
    rename_all = "snake_case",
    deny_unknown_fields
)]
#[schemars(crate = "printobserver_types::schemars")]
pub enum CameraCheckObjection {
    /// No camera check of that identifier is recorded.
    Unknown,
    /// The check did not find the camera aligned.
    NotAligned {
        /// What it found instead.
        verdict: CameraCheckVerdict,
    },
    /// The check was made on another sheet than the one the manifest declares.
    OtherSheet {
        /// The sheet the check was made on.
        checked_on: SheetId,
        /// The sheet the manifest declares.
        declared: SheetId,
    },
}

impl Sample for CameraCheckVerdict {
    fn sample_full() -> Self {
        Self::Aligned
    }

    fn sample_alternates() -> Vec<Self> {
        vec![Self::NotAligned, Self::Unavailable]
    }
}

impl Sample for CameraCheckObjection {
    fn sample_full() -> Self {
        Self::OtherSheet {
            checked_on: sheet("textured-pei"),
            declared: sheet("smooth-pei"),
        }
    }

    fn sample_minimal() -> Self {
        Self::Unknown
    }

    fn sample_alternates() -> Vec<Self> {
        vec![Self::NotAligned {
            verdict: CameraCheckVerdict::Unavailable,
        }]
    }
}

/// A sheet named by something.
fn sheet(id: &str) -> SheetId {
    SheetId::try_from(id.to_owned()).expect("a sheet named by something")
}