lichtung_plot/elements/
reference.rs1use crate::{
4 builder::PlotDescription,
5 data::{self, DataRoleInfo, DataSelector, DataValue, InstanceId},
6 layout::PanelPath,
7 Result,
8};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum LayerSlot {
14 Primary,
16 Added(usize),
18}
19
20#[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 pub const fn layer_slot(&self) -> LayerSlot {
56 self.slot
57 }
58
59 pub const fn panel(&self) -> &PanelPath {
61 &self.panel
62 }
63
64 #[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#[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 pub fn reference(&self) -> &ElementRef {
99 &self.reference
100 }
101
102 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 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 pub fn values(&self, selector: impl Into<DataSelector>) -> Result<DataValue> {
136 data::select_value(&self.data_values(), selector.into())
137 }
138
139 pub fn numeric(&self, selector: impl Into<DataSelector>) -> Result<crate::NumericValues> {
141 self.values(selector)?.numeric()
142 }
143
144 pub fn category(&self, selector: impl Into<DataSelector>) -> Result<crate::CategoryValues> {
146 self.values(selector)?.category()
147 }
148
149 pub fn boolean(&self, selector: impl Into<DataSelector>) -> Result<crate::BooleanValues> {
151 self.values(selector)?.boolean()
152 }
153
154 pub fn axis(&self, selector: impl Into<DataSelector>) -> Result<crate::data::AxisValues> {
156 self.values(selector)?.axis()
157 }
158}
159
160#[derive(Debug, Clone)]
162#[non_exhaustive]
163pub enum ElementSnapshot {
164 Layer(Layer),
166}
167
168impl ElementSnapshot {
169 pub fn get_layer(&self) -> Option<&Layer> {
171 match self {
172 Self::Layer(layer) => Some(layer),
173 }
174 }
175
176 pub fn data_roles(&self) -> Vec<DataRoleInfo> {
178 match self {
179 Self::Layer(layer) => layer.data_roles(),
180 }
181 }
182
183 pub fn values(&self, selector: impl Into<DataSelector>) -> Result<DataValue> {
185 match self {
186 Self::Layer(layer) => layer.values(selector),
187 }
188 }
189
190 pub fn numeric(&self, selector: impl Into<DataSelector>) -> Result<crate::NumericValues> {
192 self.values(selector)?.numeric()
193 }
194
195 pub fn category(&self, selector: impl Into<DataSelector>) -> Result<crate::CategoryValues> {
197 self.values(selector)?.category()
198 }
199
200 pub fn boolean(&self, selector: impl Into<DataSelector>) -> Result<crate::BooleanValues> {
202 self.values(selector)?.boolean()
203 }
204
205 pub fn axis(&self, selector: impl Into<DataSelector>) -> Result<crate::data::AxisValues> {
207 self.values(selector)?.axis()
208 }
209}
210
211#[derive(Debug, Clone, Default)]
213pub struct ElementQuery {
214 pub(crate) panel: Option<PanelPath>,
215 pub(crate) slot: Option<LayerSlot>,
216}
217
218impl ElementQuery {
219 pub fn root() -> Self {
221 Self::default()
222 }
223
224 #[must_use]
226 pub fn panel(mut self, panel: PanelPath) -> Self {
227 self.panel = Some(panel);
228 self
229 }
230
231 #[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}