Skip to main content

lichtung_plot/layout/
path.rs

1//! Typed addresses of actual configured plotting regions.
2
3use crate::{data::RawKey, layout::FacetCell};
4
5/// One scientific region of a marginal plot.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum MarginalRegion {
9    /// Joint observations in the central region.
10    Joint,
11    /// Upper marginal distribution.
12    Top,
13    /// Right-hand marginal distribution.
14    Right,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub(crate) enum PanelStep {
19    Cell(usize, usize),
20    Facet(FacetCell),
21    Matrix(RawKey, RawKey),
22    Marginal(MarginalRegion),
23}
24
25/// A typed path through the actual plotting and composition regions.
26#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
27pub struct PanelPath(Vec<PanelStep>);
28
29impl PanelPath {
30    /// The root of this configuration.
31    pub fn root() -> Self {
32        Self::default()
33    }
34
35    /// Whether this path names the root itself.
36    pub fn is_root(&self) -> bool {
37        self.0.is_empty()
38    }
39
40    /// Select a zero-based Canvas anchor in the current grid.
41    #[must_use]
42    pub fn panel(mut self, row: usize, column: usize) -> Self {
43        self.0.push(PanelStep::Cell(row, column));
44        self
45    }
46
47    /// Select an actual facet's original level or level pair.
48    #[must_use]
49    pub fn facet(mut self, key: impl Into<FacetCell>) -> Self {
50        self.0.push(PanelStep::Facet(key.into()));
51        self
52    }
53
54    /// Select a matrix region by its two original variable identities.
55    #[must_use]
56    pub fn matrix(mut self, x: impl Into<RawKey>, y: impl Into<RawKey>) -> Self {
57        self.0.push(PanelStep::Matrix(x.into(), y.into()));
58        self
59    }
60
61    /// Select an actual marginal region.
62    #[must_use]
63    pub fn marginal(mut self, region: MarginalRegion) -> Self {
64        self.0.push(PanelStep::Marginal(region));
65        self
66    }
67
68    pub(crate) fn steps(&self) -> &[PanelStep] {
69        &self.0
70    }
71
72    pub(crate) fn from_steps(steps: &[PanelStep]) -> Self {
73        Self(steps.to_vec())
74    }
75}