Skip to main content

lichtung_plot/text/
position.rs

1//! Typed scientific, panel and owning-output references for authored annotations.
2
3use super::layout::Finite;
4use crate::{DataAreaCorner, Result};
5pub(crate) mod resolve;
6
7/// A typed scientific point, independent of drawing instructions.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct DataPosition {
10    pub(crate) x: DataCoordinate,
11    pub(crate) y: DataCoordinate,
12}
13impl DataPosition {
14    /// Retain independent scientific x and y references.
15    pub const fn new(x: DataCoordinate, y: DataCoordinate) -> Self {
16        Self { x, y }
17    }
18
19    /// Borrow the authored x coordinate.
20    #[must_use]
21    pub const fn get_x(&self) -> &DataCoordinate {
22        &self.x
23    }
24
25    /// Borrow the authored y coordinate.
26    #[must_use]
27    pub const fn get_y(&self) -> &DataCoordinate {
28        &self.y
29    }
30}
31
32/// A raw scientific coordinate. Category and series names are never aliases.
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct DataCoordinate(pub(crate) DataValue);
35#[derive(Debug, Clone, PartialEq, Eq)]
36pub(crate) enum DataValue {
37    Numeric(Finite),
38    Category(String),
39    Series(String, String),
40}
41impl DataCoordinate {
42    /// A finite numeric data value, transformed by the actual scientific scale.
43    pub fn numeric(value: f64) -> Result<Self> {
44        Ok(Self(DataValue::Numeric(Finite::new(value)?)))
45    }
46    /// An exact raw category identity, resolved against the actual local domain.
47    pub fn category(raw: impl Into<String>) -> Self {
48        Self(DataValue::Category(raw.into()))
49    }
50    /// An exact raw category and positioned-series identity.
51    pub fn category_series(category: impl Into<String>, series: impl Into<String>) -> Self {
52        Self(DataValue::Series(category.into(), series.into()))
53    }
54
55    /// Numeric payload, absent for category references.
56    #[must_use]
57    pub const fn get_numeric_value(&self) -> Option<f64> {
58        match &self.0 {
59            DataValue::Numeric(value) => Some(value.get()),
60            DataValue::Category(_) | DataValue::Series(..) => None,
61        }
62    }
63
64    /// Borrow the raw category of an unqualified category reference.
65    #[must_use]
66    pub fn get_category_value(&self) -> Option<&str> {
67        match &self.0 {
68            DataValue::Category(value) => Some(value),
69            DataValue::Numeric(_) | DataValue::Series(..) => None,
70        }
71    }
72
73    /// Borrow the exact raw category and positioned-series identities.
74    #[must_use]
75    pub fn get_category_series_value(&self) -> Option<(&str, &str)> {
76        match &self.0 {
77            DataValue::Series(category, series) => Some((category, series)),
78            DataValue::Numeric(_) | DataValue::Category(_) => None,
79        }
80    }
81}
82/// One independently qualified coordinate of an authored text reference.
83#[derive(Debug, Clone, PartialEq, Eq)]
84pub struct TextCoordinate(pub(crate) Coordinate);
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub(crate) enum Coordinate {
87    Data(DataCoordinate),
88    Panel(Finite),
89    Figure(Finite),
90}
91impl TextCoordinate {
92    /// Use a scientific coordinate on this actual axis.
93    pub const fn data(value: DataCoordinate) -> Self {
94        Self(Coordinate::Data(value))
95    }
96    /// Use a finite fraction of the actual scientific panel, positive y upward.
97    pub fn data_area_fraction(value: f64) -> Result<Self> {
98        Ok(Self(Coordinate::Panel(Finite::new(value)?)))
99    }
100    /// Use a finite fraction of the owning output root, including an embedding Canvas.
101    pub fn figure_fraction(value: f64) -> Result<Self> {
102        Ok(Self(Coordinate::Figure(Finite::new(value)?)))
103    }
104}
105/// A semantic reference, separate from the selected point of the text block.
106#[derive(Debug, Clone, PartialEq, Eq)]
107pub struct TextPosition(pub(crate) Position);
108#[derive(Debug, Clone, PartialEq, Eq)]
109pub(crate) enum Position {
110    Coordinates(TextCoordinate, TextCoordinate),
111    Corner(DataAreaCorner),
112}
113impl TextPosition {
114    /// Retain independently qualified x and y coordinates.
115    pub const fn new(x: TextCoordinate, y: TextCoordinate) -> Self {
116        Self(Position::Coordinates(x, y))
117    }
118    /// Preserve the panel corner's ordinary inset and placement defaults.
119    pub const fn corner(value: DataAreaCorner) -> Self {
120        Self(Position::Corner(value))
121    }
122    /// Inspect corner syntax without inventing one for unrelated coordinate frames.
123    pub const fn as_corner(&self) -> Option<DataAreaCorner> {
124        match self.0 {
125            Position::Corner(value) => Some(value),
126            Position::Coordinates(..) => None,
127        }
128    }
129}