lichtung_plot_ndarray/
element.rs1mod 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
15pub trait NumericElement: sealed::Sealed {
17 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
35pub trait CategoryElement: sealed::Sealed {
37 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
53pub trait BooleanElement: sealed::Sealed {
55 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
70pub trait SourceRowElement: sealed::Sealed + std::fmt::Debug {
72 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
89pub 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}