Skip to main content

lichtung_plot_ndarray/
element.rs

1mod sealed {
2    pub trait Sealed {}
3    macro_rules! seal {
4        ($($ty:ty),+ $(,)?) => {$(
5            impl Sealed for $ty {}
6            impl Sealed for Option<$ty> {}
7        )+};
8    }
9    seal!(
10        f32, f64, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, bool, String,
11        &str
12    );
13}
14
15/// Sealed primitive numeric input, including its nullable `Option` form.
16pub trait NumericElement: sealed::Sealed {
17    /// Normalize a present value to f64 with ordinary floating-point rounding.
18    fn to_f64(&self) -> Option<f64>;
19}
20
21macro_rules! numeric {
22    ($($ty:ty),+ $(,)?) => {$(
23        impl NumericElement for $ty {
24            #[allow(clippy::cast_precision_loss, clippy::cast_lossless,
25                reason = "all primitive widths share ordinary f64 normalization, including rounding")]
26            fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
27        }
28        impl NumericElement for Option<$ty> {
29            fn to_f64(&self) -> Option<f64> { self.as_ref().and_then(NumericElement::to_f64) }
30        }
31    )+};
32}
33numeric!(f32, f64, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
34
35/// Sealed raw string input, including nullable strings. No display conversion.
36pub trait CategoryElement: sealed::Sealed {
37    /// Borrow the exact present key without normalization.
38    fn as_key(&self) -> Option<&str>;
39}
40
41macro_rules! category {
42    ($($ty:ty),+ $(,)?) => {$(
43        impl CategoryElement for $ty {
44            fn as_key(&self) -> Option<&str> { Some(self) }
45        }
46        impl CategoryElement for Option<$ty> {
47            fn as_key(&self) -> Option<&str> { self.as_ref().and_then(CategoryElement::as_key) }
48        }
49    )+};
50}
51category!(String, &str);
52
53/// Sealed Boolean input. Booleans are not implicit numeric 0/1.
54pub trait BooleanElement: sealed::Sealed {
55    /// Read a Boolean independently of its validity.
56    fn as_boolean(&self) -> Option<bool>;
57}
58
59impl BooleanElement for bool {
60    fn as_boolean(&self) -> Option<bool> {
61        Some(*self)
62    }
63}
64impl BooleanElement for Option<bool> {
65    fn as_boolean(&self) -> Option<bool> {
66        *self
67    }
68}
69
70/// Sealed integer provenance input; IDs never pass through f64.
71pub trait SourceRowElement: sealed::Sealed + std::fmt::Debug {
72    /// Return an exact present nonnegative ID that fits usize.
73    fn to_usize(&self) -> Option<usize>;
74}
75
76macro_rules! source_row {
77    ($($ty:ty),+ $(,)?) => {$(
78        impl SourceRowElement for $ty {
79            #[allow(clippy::useless_conversion)]
80            fn to_usize(&self) -> Option<usize> { usize::try_from(*self).ok() }
81        }
82        impl SourceRowElement for Option<$ty> {
83            fn to_usize(&self) -> Option<usize> { self.as_ref().and_then(SourceRowElement::to_usize) }
84        }
85    )+};
86}
87source_row!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
88
89/// Sealed primitive source elements. Role support is checked before reading slots.
90pub trait ArrayElement: sealed::Sealed + std::fmt::Debug {
91    #[doc(hidden)]
92    const NUMERIC: bool = false;
93    #[doc(hidden)]
94    const CATEGORY: bool = false;
95    #[doc(hidden)]
96    const BOOLEAN: bool = false;
97    #[doc(hidden)]
98    const ID: bool = false;
99    #[doc(hidden)]
100    fn numeric(&self) -> Option<f64> {
101        None
102    }
103    #[doc(hidden)]
104    fn category(&self) -> Option<&str> {
105        None
106    }
107    #[doc(hidden)]
108    fn boolean(&self) -> Option<bool> {
109        None
110    }
111    #[doc(hidden)]
112    fn id(&self) -> Option<usize> {
113        None
114    }
115    #[doc(hidden)]
116    fn recipe_key(&self) -> Option<crate::data::RawKey>;
117}
118
119macro_rules! source_numeric {
120    ($($ty:ty),+) => {$ (
121        impl ArrayElement for $ty {
122            const NUMERIC: bool = true;
123            const ID: bool = true;
124            fn numeric(&self) -> Option<f64> { self.to_f64() }
125            fn id(&self) -> Option<usize> { self.to_usize() }
126            fn recipe_key(&self)->Option<crate::data::RawKey>{Some(crate::data::RawKey::Integer((*self).into()))}
127        }
128    )+};
129}
130source_numeric!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
131macro_rules! source_float {
132    ($($ty:ty),+) => {$ (
133        impl ArrayElement for $ty {
134            const NUMERIC: bool = true;
135            fn numeric(&self) -> Option<f64> { self.to_f64() }
136            fn recipe_key(&self)->Option<crate::data::RawKey>{Some(crate::data::RawKey::Float(f64::from(*self)))}
137        }
138    )+};
139}
140source_float!(f32, f64);
141impl ArrayElement for bool {
142    const BOOLEAN: bool = true;
143    fn boolean(&self) -> Option<bool> {
144        Some(*self)
145    }
146    fn recipe_key(&self) -> Option<crate::data::RawKey> {
147        Some(crate::data::RawKey::Boolean(*self))
148    }
149}
150impl ArrayElement for String {
151    const CATEGORY: bool = true;
152    fn category(&self) -> Option<&str> {
153        Some(self)
154    }
155    fn recipe_key(&self) -> Option<crate::data::RawKey> {
156        Some(crate::data::RawKey::String(self.clone()))
157    }
158}
159impl ArrayElement for &str {
160    const CATEGORY: bool = true;
161    fn category(&self) -> Option<&str> {
162        Some(self)
163    }
164    fn recipe_key(&self) -> Option<crate::data::RawKey> {
165        Some(crate::data::RawKey::String(self.to_string()))
166    }
167}
168impl<T: ArrayElement> ArrayElement for Option<T>
169where
170    Option<T>: sealed::Sealed,
171{
172    const NUMERIC: bool = T::NUMERIC;
173    const CATEGORY: bool = T::CATEGORY;
174    const BOOLEAN: bool = T::BOOLEAN;
175    const ID: bool = T::ID;
176    fn numeric(&self) -> Option<f64> {
177        self.as_ref().and_then(ArrayElement::numeric)
178    }
179    fn category(&self) -> Option<&str> {
180        self.as_ref().and_then(ArrayElement::category)
181    }
182    fn boolean(&self) -> Option<bool> {
183        self.as_ref().and_then(ArrayElement::boolean)
184    }
185    fn id(&self) -> Option<usize> {
186        self.as_ref().and_then(ArrayElement::id)
187    }
188    fn recipe_key(&self) -> Option<crate::data::RawKey> {
189        self.as_ref().and_then(ArrayElement::recipe_key)
190    }
191}