tudelft-xray-sim 1.0.0

simulation library for the modeling assignment in the course 'Software Systems' at the TU Delft.
Documentation
use crate::{Dose, Mode, Projection};
use log::{error, info, warn};

#[allow(unused_imports)]
use crate::Controller;

/// An X-ray plane. The projection of each plane is either frontal or lateral.
///
/// In a single-plane system the only plane will be frontal.
/// In a two-plane system you have both a frontal and lateral plane.
/// The planes cannot change their projection.
///
/// The planes are modified through the [`Controller`].
/// You do not need to interact with them directly.
/// You can access their current state if needed through getters.
#[derive(Debug, Clone, Default)]
pub struct Plane {
    projection: Projection,
    dose: Dose,
    mode: Mode,
    active: bool,
}

impl Plane {
    /// Getter for the [`Projection`] of this plane.
    #[must_use]
    pub fn projection(&self) -> Projection {
        self.projection
    }

    /// Getter for the current [`Dose`] setting of this plane.
    #[must_use]
    pub fn dose(&self) -> Dose {
        self.dose
    }

    /// Getter for the [`Mode`] of this plane.
    #[must_use]
    pub fn mode(&self) -> Mode {
        self.mode
    }

    /// Get whether the plane in currently active.
    #[must_use]
    pub fn active(&self) -> bool {
        self.active
    }

    pub(crate) fn frontal() -> Self {
        Self {
            projection: Projection::Frontal,
            ..Default::default()
        }
    }

    pub(crate) fn lateral() -> Self {
        Self {
            projection: Projection::Lateral,
            ..Default::default()
        }
    }

    pub(crate) fn set_dose(&mut self, dose: Dose) {
        if self.active() {
            error!("Cannot set the dose while the X-ray is active");
            return;
        }

        info!("Dose was set to {dose:?} for {:?} plane", self.projection);
        self.dose = dose;
    }

    pub(crate) fn set_mode(&mut self, mode: Mode) {
        if self.active() {
            error!("Cannot set the mode while the X-ray is active");
            return;
        }

        info!("Mode was set to {mode:?} for {:?} plane", self.projection);
        self.mode = mode;
    }

    pub(crate) fn deactivate_xray(&mut self) {
        if !self.active() {
            warn!("Tried deactivating the X-ray, but it is already inactive");
            return;
        }

        info!("{:?} X-ray deactivated", self.projection);
        self.active = false;
    }

    pub(crate) fn activate_xray(&mut self) {
        if self.active() {
            warn!("Tried activating the X-ray, but it is already active");
            return;
        }

        info!("{:?} X-ray activated", self.projection);
        self.active = true;
    }

    pub(crate) fn set_and_activate_xray(&mut self, dose: Dose, mode: Mode) {
        self.set_dose(dose);
        self.set_mode(mode);
        self.activate_xray();
    }
}