lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! Actual declaration addresses, independent of rendering order.

use crate::{
    builder::PlotDescription,
    data::{self, DataRoleInfo, DataSelector, DataValue, InstanceId},
    layout::PanelPath,
    Result,
};

/// The owning layer slot of an authored scientific declaration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum LayerSlot {
    /// The plot's original scientific declaration.
    Primary,
    /// An independently attached layer, by its actual zero-based slot.
    Added(usize),
}

/// An opaque reference to one actual declaration in its owning panel.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ElementRef {
    root: InstanceId,
    placement: InstanceId,
    instance: InstanceId,
    slot: LayerSlot,
    panel: PanelPath,
    declaring_panel: PanelPath,
}

impl ElementRef {
    pub(crate) fn layer(
        root: &InstanceId,
        placement: &InstanceId,
        instance: &InstanceId,
        slot: LayerSlot,
        panel: &PanelPath,
        declaring_panel: &PanelPath,
    ) -> Self {
        Self {
            root: root.clone(),
            placement: placement.clone(),
            instance: instance.clone(),
            slot,
            panel: panel.clone(),
            declaring_panel: declaring_panel.clone(),
        }
    }

    pub(crate) fn instance(&self) -> &InstanceId {
        &self.instance
    }

    /// Actual owning layer slot.
    pub const fn layer_slot(&self) -> LayerSlot {
        self.slot
    }

    /// The selected actual panel.
    pub const fn panel(&self) -> &PanelPath {
        &self.panel
    }

    /// The underlying declaration without a local panel restriction.
    #[must_use]
    pub fn declaration(&self) -> Self {
        let mut reference = self.clone();
        reference.panel.clone_from(&reference.declaring_panel);
        reference
    }
}

/// An owning snapshot of one actual native scientific layer.
#[derive(Clone)]
pub struct Layer {
    pub(crate) declaration: LayerDeclaration,
    pub(crate) reference: ElementRef,
}

#[derive(Clone)]
pub(crate) enum LayerDeclaration {
    Primary(PlotDescription),
    Points(crate::elements::PointLayer),
    BarPoints(crate::elements::BarPointLayer),
}

impl std::fmt::Debug for Layer {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("Layer")
            .field("reference", &self.reference)
            .finish_non_exhaustive()
    }
}

impl Layer {
    /// The actual declaration address of this owning snapshot.
    pub fn reference(&self) -> &ElementRef {
        &self.reference
    }

    /// Inspect the actual original significance annotations of this declaring layer.
    pub fn get_significance(&self) -> Option<&[crate::elements::SignificanceAnnotation]> {
        match &self.declaration {
            LayerDeclaration::Primary(description) => {
                let annotations = description.significance_annotations();
                (!annotations.is_empty()).then_some(annotations)
            }
            _ => None,
        }
    }

    pub(crate) fn data_values(&self) -> Vec<DataValue> {
        let owner = self.reference.declaration();
        match &self.declaration {
            LayerDeclaration::Primary(description) => description.primary_layer_data(&owner),
            LayerDeclaration::Points(layer) => {
                crate::builder::data_access::point_layer_data(layer, &owner)
            }
            LayerDeclaration::BarPoints(layer) => {
                crate::builder::data_access::bar_point_layer_data(layer, &owner)
            }
        }
    }

    /// Discover actual original input roles without preparing the plot.
    pub fn data_roles(&self) -> Vec<DataRoleInfo> {
        self.data_values()
            .into_iter()
            .map(|value| value.info().clone())
            .collect()
    }

    /// Read the actual original value of a bound role.
    pub fn values(&self, selector: impl Into<DataSelector>) -> Result<DataValue> {
        data::select_value(&self.data_values(), selector.into())
    }

    /// Read an actual unique numeric vector.
    pub fn numeric(&self, selector: impl Into<DataSelector>) -> Result<crate::NumericValues> {
        self.values(selector)?.numeric()
    }

    /// Read an actual unique string-category vector.
    pub fn category(&self, selector: impl Into<DataSelector>) -> Result<crate::CategoryValues> {
        self.values(selector)?.category()
    }

    /// Read an actual Boolean vector.
    pub fn boolean(&self, selector: impl Into<DataSelector>) -> Result<crate::BooleanValues> {
        self.values(selector)?.boolean()
    }

    /// Read an actual coordinate vector.
    pub fn axis(&self, selector: impl Into<DataSelector>) -> Result<crate::data::AxisValues> {
        self.values(selector)?.axis()
    }
}

/// An owning snapshot of an actual authored declaration.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ElementSnapshot {
    /// A scientific layer.
    Layer(Layer),
}

impl ElementSnapshot {
    /// The actual declaration's native layer, when it is a layer.
    pub fn get_layer(&self) -> Option<&Layer> {
        match self {
            Self::Layer(layer) => Some(layer),
        }
    }

    /// Discover actual original input roles.
    pub fn data_roles(&self) -> Vec<DataRoleInfo> {
        match self {
            Self::Layer(layer) => layer.data_roles(),
        }
    }

    /// Read the actual original value of a bound role.
    pub fn values(&self, selector: impl Into<DataSelector>) -> Result<DataValue> {
        match self {
            Self::Layer(layer) => layer.values(selector),
        }
    }

    /// Read an actual unique numeric vector.
    pub fn numeric(&self, selector: impl Into<DataSelector>) -> Result<crate::NumericValues> {
        self.values(selector)?.numeric()
    }

    /// Read an actual unique string-category vector.
    pub fn category(&self, selector: impl Into<DataSelector>) -> Result<crate::CategoryValues> {
        self.values(selector)?.category()
    }

    /// Read an actual Boolean vector.
    pub fn boolean(&self, selector: impl Into<DataSelector>) -> Result<crate::BooleanValues> {
        self.values(selector)?.boolean()
    }

    /// Read an actual coordinate vector.
    pub fn axis(&self, selector: impl Into<DataSelector>) -> Result<crate::data::AxisValues> {
        self.values(selector)?.axis()
    }
}

/// Select actual scientific layer declarations in a panel.
#[derive(Debug, Clone, Default)]
pub struct ElementQuery {
    pub(crate) panel: Option<PanelPath>,
    pub(crate) slot: Option<LayerSlot>,
}

impl ElementQuery {
    /// Query the actual scientific roots.
    pub fn root() -> Self {
        Self::default()
    }

    /// Limit the query to one actual panel.
    #[must_use]
    pub fn panel(mut self, panel: PanelPath) -> Self {
        self.panel = Some(panel);
        self
    }

    /// Select one actual owning layer slot.
    #[must_use]
    pub fn slot(mut self, slot: LayerSlot) -> Self {
        self.slot = Some(slot);
        self
    }
}
impl From<LayerSlot> for ElementQuery {
    fn from(slot: LayerSlot) -> Self {
        Self::root().slot(slot)
    }
}