Skip to main content

lichtung_plot/elements/
reference.rs

1//! Actual declaration addresses, independent of rendering order.
2
3use crate::{
4    builder::PlotDescription,
5    data::{self, DataRoleInfo, DataSelector, DataValue, InstanceId},
6    layout::PanelPath,
7    Result,
8};
9
10/// The owning layer slot of an authored scientific declaration.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum LayerSlot {
14    /// The plot's original scientific declaration.
15    Primary,
16    /// An independently attached layer, by its actual zero-based slot.
17    Added(usize),
18}
19
20/// An opaque reference to one actual declaration in its owning panel.
21#[derive(Debug, Clone, PartialEq, Eq, Hash)]
22pub struct ElementRef {
23    root: InstanceId,
24    placement: InstanceId,
25    instance: InstanceId,
26    slot: LayerSlot,
27    panel: PanelPath,
28    declaring_panel: PanelPath,
29}
30
31impl ElementRef {
32    pub(crate) fn layer(
33        root: &InstanceId,
34        placement: &InstanceId,
35        instance: &InstanceId,
36        slot: LayerSlot,
37        panel: &PanelPath,
38        declaring_panel: &PanelPath,
39    ) -> Self {
40        Self {
41            root: root.clone(),
42            placement: placement.clone(),
43            instance: instance.clone(),
44            slot,
45            panel: panel.clone(),
46            declaring_panel: declaring_panel.clone(),
47        }
48    }
49
50    pub(crate) fn instance(&self) -> &InstanceId {
51        &self.instance
52    }
53
54    /// Actual owning layer slot.
55    pub const fn layer_slot(&self) -> LayerSlot {
56        self.slot
57    }
58
59    /// The selected actual panel.
60    pub const fn panel(&self) -> &PanelPath {
61        &self.panel
62    }
63
64    /// The underlying declaration without a local panel restriction.
65    #[must_use]
66    pub fn declaration(&self) -> Self {
67        let mut reference = self.clone();
68        reference.panel.clone_from(&reference.declaring_panel);
69        reference
70    }
71}
72
73/// An owning snapshot of one actual native scientific layer.
74#[derive(Clone)]
75pub struct Layer {
76    pub(crate) declaration: LayerDeclaration,
77    pub(crate) reference: ElementRef,
78}
79
80#[derive(Clone)]
81pub(crate) enum LayerDeclaration {
82    Primary(PlotDescription),
83    Points(crate::elements::PointLayer),
84    BarPoints(crate::elements::BarPointLayer),
85}
86
87impl std::fmt::Debug for Layer {
88    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        formatter
90            .debug_struct("Layer")
91            .field("reference", &self.reference)
92            .finish_non_exhaustive()
93    }
94}
95
96impl Layer {
97    /// The actual declaration address of this owning snapshot.
98    pub fn reference(&self) -> &ElementRef {
99        &self.reference
100    }
101
102    /// Inspect the actual original significance annotations of this declaring layer.
103    pub fn get_significance(&self) -> Option<&[crate::elements::SignificanceAnnotation]> {
104        match &self.declaration {
105            LayerDeclaration::Primary(description) => {
106                let annotations = description.significance_annotations();
107                (!annotations.is_empty()).then_some(annotations)
108            }
109            _ => None,
110        }
111    }
112
113    pub(crate) fn data_values(&self) -> Vec<DataValue> {
114        let owner = self.reference.declaration();
115        match &self.declaration {
116            LayerDeclaration::Primary(description) => description.primary_layer_data(&owner),
117            LayerDeclaration::Points(layer) => {
118                crate::builder::data_access::point_layer_data(layer, &owner)
119            }
120            LayerDeclaration::BarPoints(layer) => {
121                crate::builder::data_access::bar_point_layer_data(layer, &owner)
122            }
123        }
124    }
125
126    /// Discover actual original input roles without preparing the plot.
127    pub fn data_roles(&self) -> Vec<DataRoleInfo> {
128        self.data_values()
129            .into_iter()
130            .map(|value| value.info().clone())
131            .collect()
132    }
133
134    /// Read the actual original value of a bound role.
135    pub fn values(&self, selector: impl Into<DataSelector>) -> Result<DataValue> {
136        data::select_value(&self.data_values(), selector.into())
137    }
138
139    /// Read an actual unique numeric vector.
140    pub fn numeric(&self, selector: impl Into<DataSelector>) -> Result<crate::NumericValues> {
141        self.values(selector)?.numeric()
142    }
143
144    /// Read an actual unique string-category vector.
145    pub fn category(&self, selector: impl Into<DataSelector>) -> Result<crate::CategoryValues> {
146        self.values(selector)?.category()
147    }
148
149    /// Read an actual Boolean vector.
150    pub fn boolean(&self, selector: impl Into<DataSelector>) -> Result<crate::BooleanValues> {
151        self.values(selector)?.boolean()
152    }
153
154    /// Read an actual coordinate vector.
155    pub fn axis(&self, selector: impl Into<DataSelector>) -> Result<crate::data::AxisValues> {
156        self.values(selector)?.axis()
157    }
158}
159
160/// An owning snapshot of an actual authored declaration.
161#[derive(Debug, Clone)]
162#[non_exhaustive]
163pub enum ElementSnapshot {
164    /// A scientific layer.
165    Layer(Layer),
166}
167
168impl ElementSnapshot {
169    /// The actual declaration's native layer, when it is a layer.
170    pub fn get_layer(&self) -> Option<&Layer> {
171        match self {
172            Self::Layer(layer) => Some(layer),
173        }
174    }
175
176    /// Discover actual original input roles.
177    pub fn data_roles(&self) -> Vec<DataRoleInfo> {
178        match self {
179            Self::Layer(layer) => layer.data_roles(),
180        }
181    }
182
183    /// Read the actual original value of a bound role.
184    pub fn values(&self, selector: impl Into<DataSelector>) -> Result<DataValue> {
185        match self {
186            Self::Layer(layer) => layer.values(selector),
187        }
188    }
189
190    /// Read an actual unique numeric vector.
191    pub fn numeric(&self, selector: impl Into<DataSelector>) -> Result<crate::NumericValues> {
192        self.values(selector)?.numeric()
193    }
194
195    /// Read an actual unique string-category vector.
196    pub fn category(&self, selector: impl Into<DataSelector>) -> Result<crate::CategoryValues> {
197        self.values(selector)?.category()
198    }
199
200    /// Read an actual Boolean vector.
201    pub fn boolean(&self, selector: impl Into<DataSelector>) -> Result<crate::BooleanValues> {
202        self.values(selector)?.boolean()
203    }
204
205    /// Read an actual coordinate vector.
206    pub fn axis(&self, selector: impl Into<DataSelector>) -> Result<crate::data::AxisValues> {
207        self.values(selector)?.axis()
208    }
209}
210
211/// Select actual scientific layer declarations in a panel.
212#[derive(Debug, Clone, Default)]
213pub struct ElementQuery {
214    pub(crate) panel: Option<PanelPath>,
215    pub(crate) slot: Option<LayerSlot>,
216}
217
218impl ElementQuery {
219    /// Query the actual scientific roots.
220    pub fn root() -> Self {
221        Self::default()
222    }
223
224    /// Limit the query to one actual panel.
225    #[must_use]
226    pub fn panel(mut self, panel: PanelPath) -> Self {
227        self.panel = Some(panel);
228        self
229    }
230
231    /// Select one actual owning layer slot.
232    #[must_use]
233    pub fn slot(mut self, slot: LayerSlot) -> Self {
234        self.slot = Some(slot);
235        self
236    }
237}
238impl From<LayerSlot> for ElementQuery {
239    fn from(slot: LayerSlot) -> Self {
240        Self::root().slot(slot)
241    }
242}