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}
116
117macro_rules! source_numeric {
118    ($($ty:ty),+) => {$ (
119        impl ArrayElement for $ty {
120            const NUMERIC: bool = true;
121            const ID: bool = true;
122            fn numeric(&self) -> Option<f64> { self.to_f64() }
123            fn id(&self) -> Option<usize> { self.to_usize() }
124        }
125    )+};
126}
127source_numeric!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
128macro_rules! source_float {
129    ($($ty:ty),+) => {$ (
130        impl ArrayElement for $ty {
131            const NUMERIC: bool = true;
132            fn numeric(&self) -> Option<f64> { self.to_f64() }
133        }
134    )+};
135}
136source_float!(f32, f64);
137impl ArrayElement for bool {
138    const BOOLEAN: bool = true;
139    fn boolean(&self) -> Option<bool> {
140        Some(*self)
141    }
142}
143impl ArrayElement for String {
144    const CATEGORY: bool = true;
145    fn category(&self) -> Option<&str> {
146        Some(self)
147    }
148}
149impl ArrayElement for &str {
150    const CATEGORY: bool = true;
151    fn category(&self) -> Option<&str> {
152        Some(self)
153    }
154}
155impl<T: ArrayElement> ArrayElement for Option<T>
156where
157    Option<T>: sealed::Sealed,
158{
159    const NUMERIC: bool = T::NUMERIC;
160    const CATEGORY: bool = T::CATEGORY;
161    const BOOLEAN: bool = T::BOOLEAN;
162    const ID: bool = T::ID;
163    fn numeric(&self) -> Option<f64> {
164        self.as_ref().and_then(ArrayElement::numeric)
165    }
166    fn category(&self) -> Option<&str> {
167        self.as_ref().and_then(ArrayElement::category)
168    }
169    fn boolean(&self) -> Option<bool> {
170        self.as_ref().and_then(ArrayElement::boolean)
171    }
172    fn id(&self) -> Option<usize> {
173        self.as_ref().and_then(ArrayElement::id)
174    }
175}