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    const BYTE: bool = false;
101    #[doc(hidden)]
102    fn numeric(&self) -> Option<f64> {
103        None
104    }
105    #[doc(hidden)]
106    fn category(&self) -> Option<&str> {
107        None
108    }
109    #[doc(hidden)]
110    fn boolean(&self) -> Option<bool> {
111        None
112    }
113    #[doc(hidden)]
114    fn id(&self) -> Option<usize> {
115        None
116    }
117    #[doc(hidden)]
118    fn byte(&self) -> Option<u8> {
119        None
120    }
121}
122
123macro_rules! source_numeric {
124    ($($ty:ty),+) => {$ (
125        impl ArrayElement for $ty {
126            const NUMERIC: bool = true;
127            const ID: bool = true;
128            fn numeric(&self) -> Option<f64> { self.to_f64() }
129            fn id(&self) -> Option<usize> { self.to_usize() }
130        }
131    )+};
132}
133source_numeric!(i8, i16, i32, i64, i128, isize, u16, u32, u64, u128, usize);
134impl ArrayElement for u8 {
135    const NUMERIC: bool = true;
136    const ID: bool = true;
137    const BYTE: bool = true;
138    fn numeric(&self) -> Option<f64> {
139        self.to_f64()
140    }
141    fn id(&self) -> Option<usize> {
142        self.to_usize()
143    }
144    fn byte(&self) -> Option<u8> {
145        Some(*self)
146    }
147}
148macro_rules! source_float {
149    ($($ty:ty),+) => {$ (
150        impl ArrayElement for $ty {
151            const NUMERIC: bool = true;
152            fn numeric(&self) -> Option<f64> { self.to_f64() }
153        }
154    )+};
155}
156source_float!(f32, f64);
157impl ArrayElement for bool {
158    const BOOLEAN: bool = true;
159    fn boolean(&self) -> Option<bool> {
160        Some(*self)
161    }
162}
163impl ArrayElement for String {
164    const CATEGORY: bool = true;
165    fn category(&self) -> Option<&str> {
166        Some(self)
167    }
168}
169impl ArrayElement for &str {
170    const CATEGORY: bool = true;
171    fn category(&self) -> Option<&str> {
172        Some(self)
173    }
174}
175impl<T: ArrayElement> ArrayElement for Option<T>
176where
177    Option<T>: sealed::Sealed,
178{
179    const NUMERIC: bool = T::NUMERIC;
180    const CATEGORY: bool = T::CATEGORY;
181    const BOOLEAN: bool = T::BOOLEAN;
182    const ID: bool = T::ID;
183    const BYTE: bool = T::BYTE;
184    fn numeric(&self) -> Option<f64> {
185        self.as_ref().and_then(ArrayElement::numeric)
186    }
187    fn category(&self) -> Option<&str> {
188        self.as_ref().and_then(ArrayElement::category)
189    }
190    fn boolean(&self) -> Option<bool> {
191        self.as_ref().and_then(ArrayElement::boolean)
192    }
193    fn id(&self) -> Option<usize> {
194        self.as_ref().and_then(ArrayElement::id)
195    }
196    fn byte(&self) -> Option<u8> {
197        self.as_ref().and_then(ArrayElement::byte)
198    }
199}