lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! Typed addresses of actual configured plotting regions.

use crate::{data::RawKey, layout::FacetCell};

/// One scientific region of a marginal plot.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MarginalRegion {
    /// Joint observations in the central region.
    Joint,
    /// Upper marginal distribution.
    Top,
    /// Right-hand marginal distribution.
    Right,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) enum PanelStep {
    Cell(usize, usize),
    Named(String),
    Facet(FacetCell),
    Matrix(RawKey, RawKey),
    Marginal(MarginalRegion),
}

/// A typed path through the actual plotting and composition regions.
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct PanelPath(Vec<PanelStep>);

impl PanelPath {
    /// The root of this configuration.
    pub fn root() -> Self {
        Self::default()
    }

    /// Whether this path names the root itself.
    pub fn is_root(&self) -> bool {
        self.0.is_empty()
    }

    /// Select a zero-based Canvas anchor in the current grid.
    #[must_use]
    pub fn panel(mut self, row: usize, column: usize) -> Self {
        self.0.push(PanelStep::Cell(row, column));
        self
    }

    /// Select the uniquely named anchor of the current Canvas grid.
    #[must_use]
    pub fn panel_named(mut self, name: impl Into<String>) -> Self {
        self.0.push(PanelStep::Named(name.into()));
        self
    }

    /// Select an actual facet's original level or level pair.
    #[must_use]
    pub fn facet(mut self, key: impl Into<FacetCell>) -> Self {
        self.0.push(PanelStep::Facet(key.into()));
        self
    }

    /// Select a matrix region by its two original variable identities.
    #[must_use]
    pub fn matrix(mut self, x: impl Into<RawKey>, y: impl Into<RawKey>) -> Self {
        self.0.push(PanelStep::Matrix(x.into(), y.into()));
        self
    }

    /// Select an actual marginal region.
    #[must_use]
    pub fn marginal(mut self, region: MarginalRegion) -> Self {
        self.0.push(PanelStep::Marginal(region));
        self
    }

    pub(crate) fn steps(&self) -> &[PanelStep] {
        &self.0
    }

    pub(crate) fn from_steps(steps: &[PanelStep]) -> Self {
        Self(steps.to_vec())
    }
}

impl From<(usize, usize)> for PanelPath {
    fn from((row, column): (usize, usize)) -> Self {
        Self::root().panel(row, column)
    }
}

impl From<&str> for PanelPath {
    fn from(name: &str) -> Self {
        Self::root().panel_named(name)
    }
}

impl From<String> for PanelPath {
    fn from(name: String) -> Self {
        Self::root().panel_named(name)
    }
}