chart_js_rs/objects/
helper_objects.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use {
    crate::{traits::*, utils::FnWithArgs},
    serde::{
        de::{self, DeserializeOwned},
        Deserialize, Serialize,
    },
    std::fmt::{Debug, Display},
};

#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq, Eq)]
#[serde(transparent)]
pub struct DatasetData(pub serde_json::Value);
impl DatasetData {
    pub fn is_empty(&self) -> bool {
        serde_json::to_value(self)
            .unwrap()
            .as_array()
            .unwrap()
            .is_empty()
    }

    pub fn from_single_point_array(iter: impl Iterator<Item = [NumberOrDateString; 1]>) -> Self {
        DatasetData(serde_json::to_value(iter.collect::<Vec<_>>()).unwrap())
    }

    pub fn from_minmax_array(iter: impl Iterator<Item = [NumberOrDateString; 2]>) -> Self {
        DatasetData(serde_json::to_value(iter.collect::<Vec<_>>()).unwrap())
    }
}
impl PartialOrd for DatasetData {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for DatasetData {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.to_string().cmp(&other.0.to_string())
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct NoDatasets {}
impl DatasetTrait for NoDatasets {
    fn labels(self) -> Vec<NumberOrDateString> {
        Vec::new()
    }
}
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct NoAnnotations {}
impl Annotation for NoAnnotations {}

#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct Dataset<D: DatasetTrait> {
    datasets: D,
    labels: Option<Vec<NumberOrDateString>>,
}
impl<D: DatasetTrait> Dataset<D> {
    pub fn new() -> Self {
        Self {
            datasets: D::default(),
            labels: None,
        }
    }
    pub fn get_datasets(&mut self) -> &mut D {
        &mut self.datasets
    }
    pub fn datasets(mut self, datasets: impl Into<D>) -> Self {
        self.datasets = datasets.into();
        let labels = self.datasets.clone();
        self.labels(labels.labels())
    }
    pub fn get_labels(&mut self) -> &mut Option<Vec<NumberOrDateString>> {
        &mut self.labels
    }
    pub fn labels<T: Into<NumberOrDateString>>(
        mut self,
        labels: impl IntoIterator<Item = T>,
    ) -> Self {
        self.labels = Some(labels.into_iter().map(Into::into).collect());
        self
    }
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Any {
    String(String),
    Int(isize),
    Bool(bool),
    Vec(Vec<()>),
}
impl From<bool> for Any {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}
impl From<String> for Any {
    fn from(value: String) -> Self {
        Self::String(value)
    }
}
impl Any {
    pub fn is_empty(&self) -> bool {
        match self {
            Any::String(s) => s.is_empty(),
            Any::Int(_i) => false,
            Any::Bool(_b) => false,
            Any::Vec(v) => v.is_empty(),
        }
    }
}
impl Display for Any {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Any::String(s) => write!(f, "{s}"),
            Any::Bool(b) => write!(f, "{b}"),
            Any::Int(i) => write!(f, "{i}"),
            Any::Vec(_) => write!(f, ""),
        }
    }
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct NumberOrDateString(String);
impl From<NumberString> for NumberOrDateString {
    fn from(value: NumberString) -> Self {
        value.0.into()
    }
}
impl NumberOrDateString {
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}
impl PartialOrd for NumberOrDateString {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for NumberOrDateString {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        if let Some((s, o)) = self
            .0
            .parse::<rust_decimal::Decimal>()
            .ok()
            .zip(other.0.parse::<rust_decimal::Decimal>().ok())
        {
            s.cmp(&o)
        } else {
            self.0.cmp(&other.0)
        }
    }
}
impl<T: Display> From<T> for NumberOrDateString {
    fn from(s: T) -> Self {
        Self(s.to_string())
    }
}
#[allow(unknown_lints, clippy::to_string_trait_impl)]
impl ToString for NumberOrDateString {
    fn to_string(&self) -> String {
        self.0.to_string()
    }
}
impl Serialize for NumberOrDateString {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let fnum: Result<f64, _> = self.0.parse();
        let inum: Result<i64, _> = self.0.parse();
        match (fnum, inum) {
            (Ok(_), Ok(inum)) => serializer.serialize_i64(inum),
            (Ok(fnum), _) => serializer.serialize_f64(fnum),
            _ => serializer.serialize_str(&self.0),
        }
    }
}
impl<'de> Deserialize<'de> for NumberOrDateString {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Any::deserialize(deserializer).map(|soi| Self(soi.to_string()))
    }
}

#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct BoolString(String);
impl BoolString {
    pub fn opt_true() -> Option<BoolString> {
        BoolString("true".into()).into()
    }
    pub fn opt_false() -> Option<BoolString> {
        BoolString("false".into()).into()
    }
    pub fn _true() -> BoolString {
        BoolString("true".into())
    }
    pub fn _false() -> BoolString {
        BoolString("false".into())
    }
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}
impl<T: Display> From<T> for BoolString {
    fn from(s: T) -> Self {
        Self(s.to_string())
    }
}
impl Serialize for BoolString {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let bool_: Result<bool, _> = self.0.parse();
        let any: Result<String, _> = self.0.parse();
        match (bool_, any) {
            (Ok(bool_), _) => serializer.serialize_bool(bool_),
            (_, Ok(any)) => serializer.serialize_str(&any),
            _ => unreachable!(),
        }
    }
}
impl<'de> Deserialize<'de> for BoolString {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Any::deserialize(deserializer).map(|soi| Self(soi.to_string()))
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FnWithArgsOrAny<const N: usize> {
    Any(Any),
    FnWithArgs(FnWithArgs<N>),
}
impl<const N: usize> FnWithArgsOrAny<N> {
    pub fn is_empty(&self) -> bool {
        match self {
            FnWithArgsOrAny::Any(a) => a.is_empty(),
            FnWithArgsOrAny::FnWithArgs(fnwa) => fnwa.is_empty(),
        }
    }
}
impl<const N: usize> Default for FnWithArgsOrAny<N> {
    fn default() -> Self {
        FnWithArgsOrAny::Any(Any::from(false))
    }
}
impl<const N: usize, T: Display> From<T> for FnWithArgsOrAny<N> {
    fn from(s: T) -> Self {
        Self::Any(s.to_string().into())
    }
}
impl<const N: usize> From<FnWithArgs<N>> for FnWithArgsOrAny<N> {
    fn from(value: FnWithArgs<N>) -> Self {
        Self::FnWithArgs(value)
    }
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct NumberString(String);
impl From<NumberOrDateString> for NumberString {
    fn from(value: NumberOrDateString) -> Self {
        value.0.into()
    }
}
impl NumberString {
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}
impl PartialOrd for NumberString {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for NumberString {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        if let Some((s, o)) = self
            .0
            .parse::<rust_decimal::Decimal>()
            .ok()
            .zip(other.0.parse::<rust_decimal::Decimal>().ok())
        {
            s.cmp(&o)
        } else {
            self.0.cmp(&other.0)
        }
    }
}
impl<T: Display> From<T> for NumberString {
    fn from(s: T) -> Self {
        Self(s.to_string())
    }
}
#[allow(clippy::to_string_trait_impl)]
impl ToString for NumberString {
    fn to_string(&self) -> String {
        self.0.to_string()
    }
}
impl Serialize for NumberString {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let fnum: Result<f64, _> = self.0.parse();
        let inum: Result<i64, _> = self.0.parse();
        match (fnum, inum) {
            (Ok(_), Ok(inum)) => serializer.serialize_i64(inum),
            (Ok(fnum), _) => serializer.serialize_f64(fnum),
            _ => serializer.serialize_str(&self.0),
        }
    }
}
impl<'de> Deserialize<'de> for NumberString {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Any::deserialize(deserializer).map(|soi| Self(soi.to_string()))
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(untagged)]
pub enum NumberStringOrT<T: Serialize + DeserializeOwned> {
    T(T),
    NumberString(NumberString),
}
impl<'de, T: Serialize + DeserializeOwned> Deserialize<'de> for NumberStringOrT<T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        // thanks serde :|
        let value = serde::__private::de::Content::deserialize(deserializer)?;
        let deserializer = serde::__private::de::ContentRefDeserializer::<D::Error>::new(&value);

        match NumberString::deserialize(deserializer) {
            Ok(ns) => Ok(Self::NumberString(ns)),
            Err(_) => T::deserialize(deserializer).map(Self::T),
        }
    }
}
impl<T: Serialize + DeserializeOwned> NumberStringOrT<T> {
    pub fn is_empty(&self) -> bool {
        match self {
            NumberStringOrT::T(_t) => false,
            NumberStringOrT::NumberString(ns) => ns.is_empty(),
        }
    }
}

impl<T: Serialize + Display, U: Serialize + DeserializeOwned> From<T> for NumberStringOrT<U> {
    fn from(value: T) -> Self {
        serde_json::from_value(serde_json::to_value(value).unwrap()).unwrap()
    }
}