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    Named(String),
21    Facet(FacetCell),
22    Matrix(RawKey, RawKey),
23    Marginal(MarginalRegion),
24}
25
26/// A typed path through the actual plotting and composition regions.
27#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
28pub struct PanelPath(Vec<PanelStep>);
29
30impl PanelPath {
31    /// The root of this configuration.
32    pub fn root() -> Self {
33        Self::default()
34    }
35
36    /// Whether this path names the root itself.
37    pub fn is_root(&self) -> bool {
38        self.0.is_empty()
39    }
40
41    /// Select a zero-based Canvas anchor in the current grid.
42    #[must_use]
43    pub fn panel(mut self, row: usize, column: usize) -> Self {
44        self.0.push(PanelStep::Cell(row, column));
45        self
46    }
47
48    /// Select the uniquely named anchor of the current Canvas grid.
49    #[must_use]
50    pub fn panel_named(mut self, name: impl Into<String>) -> Self {
51        self.0.push(PanelStep::Named(name.into()));
52        self
53    }
54
55    /// Select an actual facet's original level or level pair.
56    #[must_use]
57    pub fn facet(mut self, key: impl Into<FacetCell>) -> Self {
58        self.0.push(PanelStep::Facet(key.into()));
59        self
60    }
61
62    /// Select a matrix region by its two original variable identities.
63    #[must_use]
64    pub fn matrix(mut self, x: impl Into<RawKey>, y: impl Into<RawKey>) -> Self {
65        self.0.push(PanelStep::Matrix(x.into(), y.into()));
66        self
67    }
68
69    /// Select an actual marginal region.
70    #[must_use]
71    pub fn marginal(mut self, region: MarginalRegion) -> Self {
72        self.0.push(PanelStep::Marginal(region));
73        self
74    }
75
76    pub(crate) fn steps(&self) -> &[PanelStep] {
77        &self.0
78    }
79
80    pub(crate) fn from_steps(steps: &[PanelStep]) -> Self {
81        Self(steps.to_vec())
82    }
83}
84
85impl From<(usize, usize)> for PanelPath {
86    fn from((row, column): (usize, usize)) -> Self {
87        Self::root().panel(row, column)
88    }
89}
90
91impl From<&str> for PanelPath {
92    fn from(name: &str) -> Self {
93        Self::root().panel_named(name)
94    }
95}
96
97impl From<String> for PanelPath {
98    fn from(name: String) -> Self {
99        Self::root().panel_named(name)
100    }
101}