chart-js-rs 0.1.6

Chart JS API for Rust WebAssembly
Documentation
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
#![allow(unreachable_patterns)]

use {
    crate::traits::*,
    js_sys::{Function, Reflect},
    serde::{
        de::{self, DeserializeOwned},
        Deserialize, Serialize,
    },
    std::fmt::{Debug, Display},
    wasm_bindgen::{JsCast, JsValue},
};

#[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, Clone, Deserialize, Serialize, Default, PartialEq, Eq, PartialOrd, Ord)]
#[serde(bound = "D: DatasetTrait")]
#[allow(unreachable_patterns)]
pub struct Dataset<D: DatasetTrait> {
    datasets: D,
    #[serde(skip_serializing_if = "Option::is_none", rename(serialize = "labels"))]
    forced_labels: Option<Vec<NumberOrDateString>>,
    #[serde(skip_serializing_if = "option_vec_is_none")]
    labels: Option<Vec<NumberOrDateString>>,
}
impl<D: DatasetTrait> Dataset<D> {
    pub fn new() -> Self {
        Self {
            datasets: D::default(),
            labels: None,
            forced_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();

        if self.forced_labels.is_none() {
            let labels = self.datasets.clone();
            self._labels(labels.labels())
        } else {
            self
        }
    }

    pub fn get_labels(&mut self) -> &mut Option<Vec<NumberOrDateString>> {
        match (&self.labels, &self.forced_labels) {
            (Some(_), None) => &mut self.labels,
            _ => &mut self.forced_labels,
        }
    }

    fn _labels<T: Into<NumberOrDateString>>(mut self, labels: impl IntoIterator<Item = T>) -> Self {
        self.labels = Some(labels.into_iter().map(Into::into).collect());

        self
    }

    pub fn labels<T: Into<NumberOrDateString>>(
        mut self,
        labels: impl IntoIterator<Item = T>,
    ) -> Self {
        self.forced_labels = Some(labels.into_iter().map(Into::into).collect());
        self.labels = None;

        self
    }
}
fn option_vec_is_none<T: Default + PartialEq + Clone>(opt: &Option<Vec<T>>) -> bool {
    match opt {
        Some(vec) => vec.is_empty() || vec.clone().try_into() == Ok([T::default()]),
        None => true,
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Any {
    Null(Option<()>),
    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(),
            Any::Null(_) => true,
        }
    }
}
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, ""),
            Any::Null(_) => write!(f, "null"),
        }
    }
}
#[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();
        if self.0.eq_ignore_ascii_case("null") {
            return serializer.serialize_none();
        }
        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, 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 Default for BoolString {
    fn default() -> Self {
        Self::_false()
    }
}
impl ChartJsRsObject for BoolString {
    fn is_empty(&self) -> bool {
        self.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, Deserialize, Serialize)]
struct JavascriptFunction {
    args: Vec<String>,
    body: String,
    return_value: String,
    closure_id: Option<String>,
}

const ALPHABET: [&str; 32] = [
    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
    "t", "u", "v", "w", "x", "y", "z", "aa", "bb", "cc", "dd", "ee", "ff",
];

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct FnWithArgs<const N: usize> {
    pub(crate) args: [String; N],
    pub(crate) body: String,
    pub(crate) return_value: String,
    pub(crate) closure_id: Option<String>,
}
impl<const N: usize> FnWithArgs<N> {
    pub fn rationalise_1_level(obj: &JsValue, name: &'static str) {
        super::rationalise_1_level::<N, Self>(obj, name, |o| {
            let _ = Reflect::set(obj, &name.into(), &o.build());
        })
    }
    pub fn rationalise_2_levels(obj: &JsValue, name: (&'static str, &'static str)) {
        super::rationalise_2_levels::<N, Self>(obj, name, |a, o| {
            let _ = Reflect::set(&a, &name.1.into(), &o.build());
        })
    }
}

impl<const N: usize> Default for FnWithArgs<N> {
    fn default() -> Self {
        Self {
            args: (0..N)
                .map(|idx| ALPHABET[idx].to_string())
                .collect::<Vec<_>>()
                .try_into()
                .unwrap(),
            body: Default::default(),
            return_value: Default::default(),
            closure_id: None,
        }
    }
}
impl<'de, const N: usize> Deserialize<'de> for FnWithArgs<N> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let js = JavascriptFunction::deserialize(deserializer)?;
        Ok(FnWithArgs::<N> {
            args: js.args.clone().try_into().map_err(|_| {
                de::Error::custom(format!("Array had length {}, needed {}.", js.args.len(), N))
            })?,
            body: js.body,
            return_value: js.return_value,
            closure_id: js.closure_id,
        })
    }
}
impl<const N: usize> Serialize for FnWithArgs<N> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        JavascriptFunction::serialize(
            &JavascriptFunction {
                args: self.args.to_vec(),
                body: self.body.clone(),
                return_value: self.return_value.clone(),
                closure_id: self.closure_id.clone(),
            },
            serializer,
        )
    }
}

impl<const N: usize> FnWithArgs<N> {
    pub fn is_empty(&self) -> bool {
        match self.closure_id {
            Some(_) => false,
            None => self.body.is_empty(),
        }
    }

    pub fn new() -> Self {
        Self::default()
    }

    pub fn args<S: AsRef<str>>(mut self, args: [S; N]) -> Self {
        self.args = args
            .into_iter()
            .enumerate()
            .map(|(idx, s)| {
                let arg = s.as_ref();
                if arg.is_empty() { ALPHABET[idx] } else { arg }.to_string()
            })
            .collect::<Vec<_>>()
            .try_into()
            .unwrap();
        self
    }

    pub fn js_body(mut self, body: &str) -> Self {
        self.body = format!("{}\n{body}", self.body);
        self.to_owned()
    }

    pub fn js_return_value(self, return_value: &str) -> Self {
        let mut s = if self.body.is_empty() {
            self.js_body("")
        } else {
            self
        };
        s.return_value = return_value.to_string();
        s.to_owned()
    }

    pub fn build(self) -> Function {
        if let Some(id) = self.closure_id {
            let args = self.args.join(", ");
            Function::new_with_args(&args, &format!("{{ return window['{id}']({args}) }}"))
        } else {
            Function::new_with_args(
                &self.args.join(", "),
                &format!("{{ {}\nreturn {} }}", self.body, self.return_value),
            )
        }
    }
}

impl FnWithArgs<1> {
    pub fn run_rust_fn<A, B, FN: Fn(A) -> B>(mut self, _func: FN) -> Self {
        let fn_name = std::any::type_name::<FN>()
            .split("::")
            .collect::<Vec<_>>()
            .into_iter()
            .next_back()
            .unwrap();

        self.body = format!(
            "{}\nconst _out_ = window.callbacks.{}({});",
            self.body,
            fn_name,
            self.args.join(", ")
        );
        self.js_return_value("_out_")
    }

    #[track_caller]
    pub fn rust_closure<F: Fn(JsValue) -> JsValue + 'static>(mut self, closure: F) -> Self {
        let js_closure = wasm_bindgen::closure::Closure::wrap(
            Box::new(closure) as Box<dyn Fn(JsValue) -> JsValue>
        );
        let js_sys_fn: &js_sys::Function = js_closure.as_ref().unchecked_ref();

        let js_window = gloo_utils::window();
        let id = uuid::Uuid::new_v4().to_string();
        Reflect::set(&js_window, &JsValue::from_str(&id), js_sys_fn).unwrap();
        js_closure.forget();

        gloo_console::debug!(format!(
            "Closure at {}:{}:{} set at window.['{id}'].",
            file!(),
            line!(),
            column!()
        ));
        self.closure_id = Some(id);
        self
    }
}

impl FnWithArgs<2> {
    pub fn run_rust_fn<A, B, C, FN: Fn(A, B) -> C>(mut self, _func: FN) -> Self {
        let fn_name = std::any::type_name::<FN>()
            .split("::")
            .collect::<Vec<_>>()
            .into_iter()
            .next_back()
            .unwrap();

        self.body = format!(
            "{}\nconst _out_ = window.callbacks.{}({});",
            self.body,
            fn_name,
            self.args.join(", ")
        );
        self.js_return_value("_out_")
    }

    #[track_caller]
    pub fn rust_closure<F: Fn(JsValue, JsValue) -> JsValue + 'static>(
        mut self,
        closure: F,
    ) -> Self {
        let js_closure = wasm_bindgen::closure::Closure::wrap(
            Box::new(closure) as Box<dyn Fn(JsValue, JsValue) -> JsValue>
        );
        let js_sys_fn: &js_sys::Function = js_closure.as_ref().unchecked_ref();

        let js_window = gloo_utils::window();
        let id = uuid::Uuid::new_v4().to_string();
        Reflect::set(&js_window, &JsValue::from_str(&id), js_sys_fn).unwrap();
        js_closure.forget();

        gloo_console::debug!(format!(
            "Closure at {}:{}:{} set at window.['{id}'].",
            file!(),
            line!(),
            column!()
        ));
        self.closure_id = Some(id);
        self
    }
}

impl FnWithArgs<3> {
    pub fn run_rust_fn<A, B, C, D, FN: Fn(A, B, C) -> D>(mut self, _func: FN) -> Self {
        let fn_name = std::any::type_name::<FN>()
            .split("::")
            .collect::<Vec<_>>()
            .into_iter()
            .next_back()
            .unwrap();

        self.body = format!(
            "{}\nconst _out_ = window.callbacks.{}({});",
            self.body,
            fn_name,
            self.args.join(", ")
        );
        self.js_return_value("_out_")
    }

    #[track_caller]
    pub fn rust_closure<F: Fn(JsValue, JsValue, JsValue) -> JsValue + 'static>(
        mut self,
        closure: F,
    ) -> Self {
        let js_closure = wasm_bindgen::closure::Closure::wrap(
            Box::new(closure) as Box<dyn Fn(JsValue, JsValue, JsValue) -> JsValue>
        );
        let js_sys_fn: &js_sys::Function = js_closure.as_ref().unchecked_ref();

        let js_window = gloo_utils::window();
        let id = uuid::Uuid::new_v4().to_string();
        Reflect::set(&js_window, &JsValue::from_str(&id), js_sys_fn).unwrap();
        js_closure.forget();

        gloo_console::debug!(format!(
            "Closure at {}:{}:{} set at window.['{id}'].",
            file!(),
            line!(),
            column!()
        ));
        self.closure_id = Some(id);
        self
    }
}

impl FnWithArgs<4> {
    pub fn run_rust_fn<A, B, C, D, E, FN: Fn(A, B, C, D) -> E>(mut self, _func: FN) -> Self {
        let fn_name = std::any::type_name::<FN>()
            .split("::")
            .collect::<Vec<_>>()
            .into_iter()
            .next_back()
            .unwrap();

        self.body = format!(
            "{}\nconst _out_ = window.callbacks.{}({});",
            self.body,
            fn_name,
            self.args.join(", ")
        );
        self.js_return_value("_out_")
    }

    #[track_caller]
    pub fn rust_closure<F: Fn(JsValue, JsValue, JsValue, JsValue) -> JsValue + 'static>(
        mut self,
        closure: F,
    ) -> Self {
        let js_closure = wasm_bindgen::closure::Closure::wrap(
            Box::new(closure) as Box<dyn Fn(JsValue, JsValue, JsValue, JsValue) -> JsValue>
        );
        let js_sys_fn: &js_sys::Function = js_closure.as_ref().unchecked_ref();

        let js_window = gloo_utils::window();
        let id = uuid::Uuid::new_v4().to_string();
        Reflect::set(&js_window, &JsValue::from_str(&id), js_sys_fn).unwrap();
        js_closure.forget();

        gloo_console::debug!(format!(
            "Closure at {}:{}:{} set at window.['{id}'].",
            file!(),
            line!(),
            column!()
        ));
        self.closure_id = Some(id);
        self
    }
}

impl FnWithArgs<5> {
    pub fn run_rust_fn<A, B, C, D, E, F, FN: Fn(A, B, C, D, E) -> F>(mut self, _func: FN) -> Self {
        let fn_name = std::any::type_name::<FN>()
            .split("::")
            .collect::<Vec<_>>()
            .into_iter()
            .next_back()
            .unwrap();

        self.body = format!(
            "{}\nconst _out_ = window.callbacks.{}({});",
            self.body,
            fn_name,
            self.args.join(", ")
        );
        self.js_return_value("_out_")
    }

    #[track_caller]
    pub fn rust_closure<F: Fn(JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue + 'static>(
        mut self,
        closure: F,
    ) -> Self {
        let js_closure = wasm_bindgen::closure::Closure::wrap(Box::new(closure)
            as Box<dyn Fn(JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue>);
        let js_sys_fn: &js_sys::Function = js_closure.as_ref().unchecked_ref();

        let js_window = gloo_utils::window();
        let id = uuid::Uuid::new_v4().to_string();
        Reflect::set(&js_window, &JsValue::from_str(&id), js_sys_fn).unwrap();
        js_closure.forget();

        gloo_console::debug!(format!(
            "Closure at {}:{}:{} set at window.['{id}'].",
            file!(),
            line!(),
            column!()
        ));
        self.closure_id = Some(id);
        self
    }
}

impl FnWithArgs<6> {
    pub fn run_rust_fn<A, B, C, D, E, F, G, FN: Fn(A, B, C, D, E, F) -> G>(
        mut self,
        _func: FN,
    ) -> Self {
        let fn_name = std::any::type_name::<FN>()
            .split("::")
            .collect::<Vec<_>>()
            .into_iter()
            .next_back()
            .unwrap();

        self.body = format!(
            "{}\nconst _out_ = window.callbacks.{}({});",
            self.body,
            fn_name,
            self.args.join(", ")
        );
        self.js_return_value("_out_")
    }

    #[track_caller]
    pub fn rust_closure<
        F: Fn(JsValue, JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue + 'static,
    >(
        mut self,
        closure: F,
    ) -> Self {
        let js_closure = wasm_bindgen::closure::Closure::wrap(Box::new(closure)
            as Box<dyn Fn(JsValue, JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue>);
        let js_sys_fn: &js_sys::Function = js_closure.as_ref().unchecked_ref();

        let js_window = gloo_utils::window();
        let id = uuid::Uuid::new_v4().to_string();
        Reflect::set(&js_window, &JsValue::from_str(&id), js_sys_fn).unwrap();
        js_closure.forget();

        gloo_console::debug!(format!(
            "Closure at {}:{}:{} set at window.['{id}'].",
            file!(),
            line!(),
            column!()
        ));
        self.closure_id = Some(id);
        self
    }
}

// 7 is the maximum wasm_bindgen can handle rn AFAIK
impl FnWithArgs<7> {
    pub fn run_rust_fn<A, B, C, D, E, F, G, H, FN: Fn(A, B, C, D, E, F, G) -> H>(
        mut self,
        _func: FN,
    ) -> Self {
        let fn_name = std::any::type_name::<FN>()
            .split("::")
            .collect::<Vec<_>>()
            .into_iter()
            .next_back()
            .unwrap();

        self.body = format!(
            "{}\nconst _out_ = window.callbacks.{}({});",
            self.body,
            fn_name,
            self.args.join(", ")
        );
        self.js_return_value("_out_")
    }

    #[track_caller]
    pub fn rust_closure<
        F: Fn(JsValue, JsValue, JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue + 'static,
    >(
        mut self,
        closure: F,
    ) -> Self {
        let js_closure = wasm_bindgen::closure::Closure::wrap(Box::new(closure)
            as Box<
                dyn Fn(JsValue, JsValue, JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue,
            >);
        let js_sys_fn: &js_sys::Function = js_closure.as_ref().unchecked_ref();

        let js_window = gloo_utils::window();
        let id = uuid::Uuid::new_v4().to_string();
        Reflect::set(&js_window, &JsValue::from_str(&id), js_sys_fn).unwrap();
        js_closure.forget();

        gloo_console::debug!(format!(
            "Closure at {}:{}:{} set at window.['{id}'].",
            file!(),
            line!(),
            column!()
        ));
        self.closure_id = Some(id);
        self
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FnWithArgsOrT<const N: usize, T> {
    T(T),
    FnWithArgs(FnWithArgs<N>),
}

impl<const N: usize, T: for<'a> Deserialize<'a>> FnWithArgsOrT<N, T> {
    pub fn rationalise_1_level(obj: &JsValue, name: &'static str) {
        super::rationalise_1_level::<N, Self>(obj, name, |o| match o {
            FnWithArgsOrT::T(_) => (),
            FnWithArgsOrT::FnWithArgs(fnwa) => {
                let _ = Reflect::set(obj, &name.into(), &fnwa.build());
            }
        })
    }
    pub fn rationalise_2_levels(obj: &JsValue, name: (&'static str, &'static str)) {
        super::rationalise_2_levels::<N, Self>(obj, name, |a, o| match o {
            FnWithArgsOrT::T(_) => (),
            FnWithArgsOrT::FnWithArgs(fnwa) => {
                let _ = Reflect::set(&a, &name.1.into(), &fnwa.build());
            }
        })
    }
}
#[allow(private_bounds)]
impl<const N: usize, T: ChartJsRsObject> FnWithArgsOrT<N, T> {
    pub fn is_empty(&self) -> bool {
        match self {
            FnWithArgsOrT::T(a) => a.is_empty(),
            FnWithArgsOrT::FnWithArgs(fnwa) => fnwa.is_empty(),
        }
    }
}
impl<const N: usize, T: Default> Default for FnWithArgsOrT<N, T> {
    fn default() -> Self {
        FnWithArgsOrT::T(T::default())
    }
}
impl<const N: usize, T: Into<String>> From<T> for FnWithArgsOrT<N, String> {
    fn from(s: T) -> Self {
        Self::T(s.into())
    }
}
impl<const N: usize, T: Into<NumberString>> From<T> for FnWithArgsOrT<N, NumberString> {
    fn from(ns: T) -> Self {
        Self::T(ns.into())
    }
}
impl<const N: usize, T: Into<BoolString>> From<T> for FnWithArgsOrT<N, BoolString> {
    fn from(bs: T) -> Self {
        Self::T(bs.into())
    }
}
impl<const N: usize, T> From<FnWithArgs<N>> for FnWithArgsOrT<N, T> {
    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 ChartJsRsObject for NumberString {
    fn is_empty(&self) -> bool {
        self.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();
        if self.0.eq_ignore_ascii_case("null") {
            return serializer.serialize_none();
        }
        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>,
    {
        let value = serde_json::Value::deserialize(deserializer)?;

        match serde_json::from_value::<NumberString>(value.clone()) {
            Ok(ns) => Ok(Self::NumberString(ns)),
            Err(_) => serde_json::from_value::<T>(value)
                .map(Self::T)
                .map_err(de::Error::custom),
        }
    }
}
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 + ChartJsRsObject, U: Serialize + DeserializeOwned> From<T>
    for NumberStringOrT<U>
{
    fn from(value: T) -> Self {
        serde_json::from_value(serde_json::to_value(value).unwrap()).unwrap()
    }
}