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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
use std::{
    borrow::Borrow,
    fmt::{Debug, Display, Write},
    hash::Hash,
    marker::PhantomData,
};

use num::ToPrimitive;

use crate::{
    impact::serialize::{Keyed, MySerialize},
    types::{HyperType, IterableChildren, MySlice, NodeId},
};

// pub type TypeIdentifier = Type;

pub trait RefContainer {
    type Result;
    fn check<U: MySerialize + Keyed<usize>>(&self, rf: U) -> Self::Result;
}

/// identifying data for a node in an HyperAST
// pub struct SimpleNode1<Child, Label> {
//     pub(crate) kind: TypeIdentifier,
//     pub(crate) label: Option<Label>,
//     pub(crate) children: Vec<Child>,
// }

// pub type DefaultLabelIdentifier = DefaultSymbol;
// pub type DefaultNodeIdentifier = legion::Entity;
pub type HashSize = u32;

#[derive(Hash, PartialEq, Eq, Copy, Clone)]
pub enum Space {
    Space,
    // LineBreak,
    NewLine,
    CariageReturn,
    Tabulation,
    ParentIndentation,
}

#[derive(Debug, Clone)]
pub enum CompressedNode<NodeId, LabelId, Type> {
    Type(Type),
    Label { label: LabelId, kind: Type },
    Children2 { children: [NodeId; 2], kind: Type },
    Children { children: Box<[NodeId]>, kind: Type },
    Spaces(LabelId), //Box<[Space]>),
}

// pub(crate) enum SimpNode<NodeId, LabelId> {
//     Type(Type),
//     Label { label: LabelId, kind: Type },
//     Children { children: Box<[NodeId]>, kind: Type },
//     Spaces(Box<[Space]>),
// }

// mod type_baggable_nodes {
//     use std::marker::PhantomData;

//     struct Keyword<Type> {
//         kind: Type,
//     }

//     struct UnsizedNode<Type, NodeId, LabelId> {
//         // kind: Type,
//         _phantom: PhantomData<*const (Type, NodeId, LabelId)>,
//         bytes: [u8],
//         // children: [MyUnion<LabelId,NodeId>],
//     }
// }

// #[repr(C)]
// union MyUnion<NodeId, LabelId> {
//     node: std::mem::ManuallyDrop<NodeId>,
//     label: std::mem::ManuallyDrop<LabelId>,
// }

impl<N: PartialEq, L: PartialEq, T: PartialEq> PartialEq for CompressedNode<N, L, T> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Type(l0), Self::Type(r0)) => l0 == r0,
            (
                Self::Label {
                    label: l_label,
                    kind: l_kind,
                },
                Self::Label {
                    label: r_label,
                    kind: r_kind,
                },
            ) => l_label == r_label && l_kind == r_kind,
            (
                Self::Children2 {
                    children: l_children,
                    kind: l_kind,
                },
                Self::Children2 {
                    children: r_children,
                    kind: r_kind,
                },
            ) => l_children == r_children && l_kind == r_kind,
            (
                Self::Children {
                    children: l_children,
                    kind: l_kind,
                },
                Self::Children {
                    children: r_children,
                    kind: r_kind,
                },
            ) => l_children == r_children && l_kind == r_kind,
            (Self::Spaces(l0), Self::Spaces(r0)) => l0 == r0,
            _ => false,
        }
    }
}

impl<N: Eq, L: Eq, T: Eq> Eq for CompressedNode<N, L, T> {}

impl<N, L, T> CompressedNode<N, L, T> {
    pub fn new(kind: T, label: Option<L>, children: Vec<N>) -> Self {
        if children.len() > 2 {
            Self::Children {
                kind,
                children: children.into_boxed_slice(),
            }
        } else if children.len() == 2 {
            let mut it = children.into_iter();
            Self::Children2 {
                kind,
                children: [it.next().unwrap(), it.next().unwrap()],
            }
        } else if children.len() > 0 {
            // TODO Children2 Optional child2 might be better
            Self::Children {
                kind,
                children: children.into_boxed_slice(),
            }
        } else if let Some(label) = label {
            Self::Label { kind, label }
        } else {
            Self::Type(kind)
        }
    }
}

// CompressedNode

impl<N, L, T: HyperType + Copy + Hash + Eq + Send + Sync> crate::types::Typed
    for CompressedNode<N, L, T>
{
    type Type = T;

    fn get_type(&self) -> T {
        match self {
            CompressedNode::Type(kind) => *kind,
            CompressedNode::Label { label: _, kind } => *kind,
            CompressedNode::Children2 { children: _, kind } => *kind,
            CompressedNode::Children { children: _, kind } => *kind,
            CompressedNode::Spaces(_) => todo!("what is the generic version of Type::Spaces ?"),
        }
    }
}

impl<N, L: Eq, T> crate::types::Labeled for CompressedNode<N, L, T> {
    type Label = L;

    fn get_label_unchecked(&self) -> &L {
        match self {
            CompressedNode::Label { label, kind: _ } => label,
            _ => panic!(),
        }
    }

    fn try_get_label<'a>(&'a self) -> Option<&'a L> {
        todo!()
    }
}

impl<N: Eq + Clone + NodeId<IdN = N>, L, T> crate::types::WithChildren for CompressedNode<N, L, T> {
    type ChildIdx = u16;
    type Children<'a> = MySlice<N> where Self: 'a;
    // type Children<'a> = [N] where Self:'a;

    fn child_count(&self) -> Self::ChildIdx {
        match self {
            CompressedNode::Children2 {
                children: _,
                kind: _,
            } => 2,
            CompressedNode::Children { children, kind: _ } => children.len().to_u16().unwrap(),
            _ => 0,
        }
    }

    fn child(&self, idx: &Self::ChildIdx) -> Option<N> {
        match self {
            CompressedNode::Children2 { children, kind: _ } if *idx == 0 => {
                Some(children[0].as_id().clone())
            }
            CompressedNode::Children2 { children, kind: _ } if *idx == 1 => {
                Some(children[1].as_id().clone())
            }
            CompressedNode::Children { children, kind: _ } => {
                Some(children[*idx as usize].as_id().clone())
            }
            _ => None,
        }
    }

    fn child_rev(&self, idx: &Self::ChildIdx) -> Option<N> {
        match self {
            CompressedNode::Children2 { children, kind: _ } if *idx == 1 => {
                Some(children[0].as_id().clone())
            }
            CompressedNode::Children2 { children, kind: _ } if *idx == 0 => {
                Some(children[1].as_id().clone())
            }
            CompressedNode::Children { children, kind: _ } => Some(
                children[children.len() - 1 - (*idx as usize)]
                    .as_id()
                    .clone(),
            ),
            _ => None,
        }
    }

    // fn children_unchecked<'a>(&'a self) -> &[N] {
    //     match self {
    //         CompressedNode::Children2 { children, kind: _ } => &*children,
    //         CompressedNode::Children { children, kind: _ } => &*children,
    //         _ => &[],
    //     }
    // }

    // fn get_children_cpy<'a>(&'a self) -> Vec<N> {
    //     match self {
    //         CompressedNode::Children2 { children, kind: _ } => children.to_vec(),
    //         CompressedNode::Children { children, kind: _ } => children.to_vec(),
    //         _ => vec![],
    //     }
    // }

    fn children<'a>(&'a self) -> Option<&'a <Self as crate::types::WithChildren>::Children<'a>> {
        fn f<'a, N, L, T>(x: &'a CompressedNode<N, L, T>) -> &'a [N] {
            match x {
                CompressedNode::Children2 { children, kind: _ } => &*children,
                CompressedNode::Children { children, kind: _ } => &**children,
                _ => &[],
            }
        }
        // TODO check if it work, not sure
        Some(f(self).into())
    }
}

impl<N, L, T> crate::types::Node for CompressedNode<N, L, T> {}
impl<N: NodeId + Eq, L, T> crate::types::Stored for CompressedNode<N, L, T> {
    type TreeId = N;
}

impl<N: NodeId<IdN = N> + Eq + Clone, L: Eq, T: Copy + Hash + Eq + HyperType + Send + Sync>
    crate::types::Tree for CompressedNode<N, L, T>
where
    N::IdN: Copy,
{
    fn has_children(&self) -> bool {
        match self {
            CompressedNode::Children2 {
                children: _,
                kind: _,
            } => true,
            CompressedNode::Children {
                children: _,
                kind: _,
            } => true,
            _ => false,
        }
    }

    fn has_label(&self) -> bool {
        match self {
            CompressedNode::Label { label: _, kind: _ } => true,
            _ => false,
        }
    }
}

// impl Hash for CompressedNode {
//     fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
//         match self {
//             CompressedNode::Type(k) => k.hash(state),
//             CompressedNode::Label { kind, label } => {}
//             CompressedNode::Children2 {
//                 kind,
//                 child1,
//                 child2,
//             } => {
//                 let size = 0;
//                 let middle_hash = 0;

//                 let mut k = DefaultHasher::new();
//                 kind.hash(&mut k);
//                 state.write_u32(innerNodeHash(&(((k.finish() & 0xffff0000) >> 32) as u32), &0, &size, &middle_hash));
//             }
//             CompressedNode::Children { kind, children } => {
//                 kind.hash(state);
//                 // children.
//             }
//             CompressedNode::Spaces(s) => s.hash(state),
//         }
//     }
// }

// Spaces

impl Display for Space {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Space::Space => write!(f, "s"),
            Space::NewLine => write!(f, "n"),
            Space::CariageReturn => write!(f, "r"),
            Space::Tabulation => write!(f, "t"),
            Space::ParentIndentation => write!(f, "0"),
        }
    }
}

impl Space {
    pub fn fmt<W: Write>(&self, w: &mut W, p: &str) -> std::fmt::Result {
        match self {
            Space::Space => write!(w, " "),
            Space::NewLine => write!(w, "\n"),
            Space::CariageReturn => write!(w, "\r"),
            Space::Tabulation => write!(w, "\t"),
            Space::ParentIndentation => write!(w, "{}", p),
        }
    }
}

impl Space {
    pub fn to_string(&self) -> &str {
        match self {
            Space::Space => " ",
            Space::NewLine => "\n",
            Space::CariageReturn => "\r",
            Space::Tabulation => "\t",
            Space::ParentIndentation => "0",
        }
    }
}
// impl Deref for Spaces {

// }

impl Debug for Space {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Space::Space => write!(f, "s"),
            Space::NewLine => write!(f, "n"),
            Space::CariageReturn => write!(f, "r"),
            Space::Tabulation => write!(f, "t"),
            Space::ParentIndentation => write!(f, "0"),
        }
    }
}
const NL: char = '\n';
const CR: char = '\r';
impl Space {
    pub fn format_indentation(spaces: &[u8]) -> Vec<Space> {
        spaces
            .iter()
            .map(|x| match *x as char {
                ' ' => Space::Space,
                '\u{000C}' => Space::Space,
                NL => Space::NewLine,
                '\t' => Space::Tabulation,
                CR => Space::CariageReturn,
                x => {
                    log::debug!("{:?}", x);
                    // log::error!("backtrace: {}", std::backtrace::Backtrace::force_capture());
                    dbg!(std::str::from_utf8(spaces));
                    // panic!("{:?}", spaces)
                    Space::Space
                }
            })
            .collect()
    }
    pub fn try_format_indentation(spaces: &[u8]) -> Option<Vec<Space>> {
        let mut err = false;
        let r = spaces
            .iter()
            .map_while(|x| match *x as char {
                ' ' => Some(Space::Space),
                '\u{000C}' => Some(Space::Space),
                NL => Some(Space::NewLine),
                '\t' => Some(Space::Tabulation),
                CR => Some(Space::CariageReturn),
                x => {
                    log::debug!("{:?}", x);
                    err = true;
                    None
                }
            })
            .collect();
        if err {
            None
        } else {
            Some(r)
        }
    }
    /// TODO test with nssss, n -> n
    pub fn replace_indentation(indentation: &[Space], spaces: &[Space]) -> Vec<Space> {
        let mut r = vec![];
        let mut tmp = vec![];
        let mut i = 0;
        for x in spaces {
            tmp.push(*x);
            if i < indentation.len() && indentation[i] == *x {
                i += 1;
                if i == indentation.len() {
                    r.push(Space::ParentIndentation);
                    tmp.clear();
                }
            } else {
                i = 0;
                r.extend_from_slice(&*tmp);
                tmp.clear();
            }
        }
        r.extend_from_slice(&*tmp);
        r
    }

    // pub(crate) fn replace_indentation(indentation: &[Spaces], spaces: &[Spaces]) -> Vec<Spaces> {
    //     if spaces.len() < indentation.len() {
    //         return spaces.to_vec();
    //     }
    //     if indentation.len() == 0 {
    //         assert!(false);
    //     }
    //     let mut it = spaces.windows(indentation.len());
    //     let mut r: Vec<Spaces> = vec![];
    //     let mut tmp: &[Spaces] = &[];
    //     loop {
    //         match it.next() {
    //             Some(x) => {
    //                 if x == indentation {
    //                     r.push(Spaces::ParentIndentation);
    //                     for _ in 0..indentation.len()-1 {
    //                         it.next();
    //                     }
    //                     tmp = &[];
    //                 } else {
    //                     if tmp.len()>0 {
    //                         r.push(tmp[0]);
    //                     }
    //                     tmp = x;
    //                 }
    //             }
    //             None => {
    //                 r.extend(tmp);
    //                 return r
    //             },
    //         }
    //     }
    // }
}

pub struct IoOut<W: std::io::Write> {
    stream: W,
}

impl<W: std::io::Write> From<W> for IoOut<W> {
    fn from(stream: W) -> Self {
        Self { stream }
    }
}

impl<W: std::io::Write> std::fmt::Write for IoOut<W> {
    fn write_str(&mut self, s: &str) -> std::fmt::Result {
        self.stream
            .write_all(s.as_bytes())
            .map_err(|_| std::fmt::Error)
    }
}

pub type StructureSerializer<'a, 'b, IdN, HAST> =
    SimpleSerializer<'a, IdN, HAST, true, false, false, false>;
pub type LabelSerializer<'a, 'b, IdN, HAST> =
    SimpleSerializer<'a, IdN, HAST, true, true, false, false>;
pub type IdsSerializer<'a, 'b, IdN, HAST> =
    SimpleSerializer<'a, IdN, HAST, false, false, true, false>;
pub type SyntaxSerializer<'a, 'b, IdN, HAST, const SPC: bool = false> =
    SimpleSerializer<'a, IdN, HAST, true, true, false, true>;
pub type SyntaxWithIdsSerializer<'a, 'b, IdN, HAST, const SPC: bool = false> =
    SimpleSerializer<'a, IdN, HAST, true, true, true, SPC>;

pub struct SimpleSerializer<
    'a,
    IdN,
    HAST,
    const TY: bool = true,
    const LABELS: bool = false,
    const IDS: bool = false,
    const SPC: bool = false,
> {
    stores: &'a HAST,
    root: IdN,
}

impl<'store, IdN, HAST, const TY: bool, const LABELS: bool, const IDS: bool, const SPC: bool>
    SimpleSerializer<'store, IdN, HAST, TY, LABELS, IDS, SPC>
{
    pub fn new(stores: &'store HAST, root: IdN) -> Self {
        Self { stores, root }
    }
}

impl<'store, IdN, HAST, const TY: bool, const LABELS: bool, const IDS: bool, const SPC: bool>
    Display for SimpleSerializer<'store, IdN, HAST, TY, LABELS, IDS, SPC>
where
    IdN: NodeId<IdN = IdN> + Debug,
    HAST: crate::types::NodeStore<IdN>,
    HAST: crate::types::LabelStore<str>,
    HAST: crate::types::TypeStore<HAST::R<'store>>,
    HAST::R<'store>: crate::types::Typed
        + crate::types::Labeled<Label = HAST::I>
        + crate::types::WithChildren<TreeId = IdN>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.serialize(&self.root, f)
    }
}

impl<'store, IdN, HAST, const TY: bool, const LABELS: bool, const IDS: bool, const SPC: bool>
    SimpleSerializer<'store, IdN, HAST, TY, LABELS, IDS, SPC>
where
    IdN: NodeId<IdN = IdN> + Debug,
    HAST: crate::types::NodeStore<IdN>,
    HAST: crate::types::LabelStore<str>,
    HAST: crate::types::TypeStore<HAST::R<'store>>,
    HAST::R<'store>: crate::types::Typed
        + crate::types::Labeled<Label = HAST::I>
        + crate::types::WithChildren<TreeId = IdN>,
{
    // pub fn tree_syntax_with_ids(
    fn serialize(
        &self,
        id: &IdN,
        out: &mut std::fmt::Formatter<'_>,
    ) -> Result<(), std::fmt::Error> {
        use crate::types::LabelStore;
        use crate::types::Labeled;
        use crate::types::NodeStore;
        use crate::types::Typed;
        use crate::types::WithChildren;
        let b = NodeStore::resolve(self.stores, id);
        // let kind = (self.stores.type_store(), b);
        let kind = self.stores.resolve_type(&b);
        let label = b.try_get_label();
        let children = b.children();

        if kind.is_spaces() {
            if SPC {
                let s = LabelStore::resolve(self.stores, &label.unwrap());
                let b: String = Space::format_indentation(s.as_bytes())
                    .iter()
                    .map(|x| x.to_string())
                    .collect();
                write!(out, "(")?;
                if IDS { write!(out, "{:?}", id) } else { Ok(()) }.and_then(|x| {
                    if TY {
                        write!(out, "_",)
                    } else {
                        Ok(x)
                    }
                })?;
                if LABELS {
                    write!(out, " {:?}", Space::format_indentation(b.as_bytes()))?;
                }
                write!(out, ")")?;
            }
            return Ok(());
        }

        let w_kind = |out: &mut std::fmt::Formatter<'_>| {
            if IDS { write!(out, "{:?}", id) } else { Ok(()) }.and_then(|x| {
                if TY {
                    write!(out, "{}", kind.to_string())
                } else {
                    Ok(x)
                }
            })
        };

        match (label, children) {
            (None, None) => {
                w_kind(out)?;
            }
            (label, Some(children)) => {
                if let Some(label) = label {
                    let s = LabelStore::resolve(self.stores, label);
                    if LABELS {
                        write!(out, " {:?}", Space::format_indentation(s.as_bytes()))?;
                    }
                }
                if !children.is_empty() {
                    let it = children.iter_children();
                    write!(out, "(")?;
                    w_kind(out)?;
                    for id in it {
                        self.serialize(&id, out)?;
                    }
                    write!(out, ")")?;
                }
            }
            (Some(label), None) => {
                write!(out, "(")?;
                w_kind(out)?;
                if LABELS {
                    let s = LabelStore::resolve(self.stores, label);
                    if s.len() > 20 {
                        write!(out, "='{}...'", &s[..20])?;
                    } else {
                        write!(out, "='{}'", s)?;
                    }
                }
                write!(out, ")")?;
            }
        }
        return Ok(());
    }
}

fn escape(src: &str) -> String {
    let mut escaped = String::with_capacity(src.len());
    let mut utf16_buf = [0u16; 2];
    for c in src.chars() {
        match c {
            ' ' => escaped += " ",
            '\x08' => escaped += "\\b",
            '\x0c' => escaped += "\\f",
            '\n' => escaped += "\\n",
            '\r' => escaped += "\\r",
            '\t' => escaped += "\\t",
            '"' => escaped += "\\\"",
            '\\' => escaped += "\\\\",
            c if c.is_ascii_graphic() => escaped.push(c),
            c => {
                let encoded = c.encode_utf16(&mut utf16_buf);
                for utf16 in encoded {
                    write!(&mut escaped, "\\u{:04X}", utf16).unwrap();
                }
            }
        }
    }
    escaped
}

pub struct Json;
pub struct Text;

pub trait Format {}
impl Format for Json {}
impl Format for Text {}

pub type JsonSerializer<'a, 'b, IdN, HAST, const SPC: bool> =
    IndentedSerializer<'a, 'b, IdN, HAST, Json, SPC>;
pub type TextSerializer<'a, 'b, IdN, HAST> = IndentedSerializer<'a, 'b, IdN, HAST, Text, true>;

pub struct IndentedSerializer<'a, 'b, IdN, HAST, Fmt: Format = Text, const SPC: bool = false> {
    stores: &'a HAST,
    root: IdN,
    root_indent: &'b str,
    phantom: PhantomData<Fmt>,
}

impl<'store, 'b, IdN, HAST, Fmt: Format, const SPC: bool>
    IndentedSerializer<'store, 'b, IdN, HAST, Fmt, SPC>
{
    pub fn new(stores: &'store HAST, root: IdN) -> Self {
        Self {
            stores,
            root,
            root_indent: "\n",
            phantom: PhantomData,
        }
    }
}

impl<'store, 'b, IdN, HAST, const SPC: bool> Display
    for IndentedSerializer<'store, 'b, IdN, HAST, Text, SPC>
where
    IdN: NodeId<IdN = IdN>,
    HAST: crate::types::NodeStore<IdN>,
    HAST: crate::types::LabelStore<str>,
    HAST: crate::types::TypeStore<HAST::R<'store>>,
    HAST::R<'store>:
        crate::types::Labeled<Label = HAST::I> + crate::types::WithChildren<TreeId = IdN>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.serialize(&self.root, &self.root_indent, f) {
            Err(IndentedAlt::FmtError) => Err(std::fmt::Error),
            _ => Ok(()),
        }
    }
}

impl<'store, 'b, IdN, HAST, const SPC: bool> Display
    for IndentedSerializer<'store, 'b, IdN, HAST, Json, SPC>
where
    IdN: NodeId<IdN = IdN>,
    HAST: crate::types::NodeStore<IdN>,
    HAST: crate::types::LabelStore<str>,
    HAST: crate::types::TypeStore<HAST::R<'store>>,
    HAST::R<'store>:
        crate::types::Labeled<Label = HAST::I> + crate::types::WithChildren<TreeId = IdN>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.serialize(&self.root, &self.root_indent, f) {
            Err(IndentedAlt::FmtError) => Err(std::fmt::Error),
            _ => Ok(()),
        }
    }
}

impl<'store, 'b, IdN, HAST, const SPC: bool> IndentedSerializer<'store, 'b, IdN, HAST, Text, SPC>
where
    IdN: NodeId<IdN = IdN>,
    HAST: crate::types::NodeStore<IdN>,
    HAST: crate::types::LabelStore<str>,
    HAST: crate::types::TypeStore<HAST::R<'store>>,
    HAST::R<'store>:
        crate::types::Labeled<Label = HAST::I> + crate::types::WithChildren<TreeId = IdN>,
{
    fn serialize(
        &self,
        id: &IdN,
        parent_indent: &str,
        out: &mut std::fmt::Formatter<'_>,
    ) -> Result<String, IndentedAlt> {
        use crate::types::LabelStore;
        use crate::types::Labeled;
        use crate::types::NodeStore;
        use crate::types::Typed;
        use crate::types::WithChildren;
        let b = NodeStore::resolve(self.stores, id);
        // let kind = (self.stores.type_store(), b);
        let kind = self.stores.resolve_type(&b);
        let label = b.try_get_label();
        let children = b.children();

        if kind.is_spaces() {
            let indent = if let Some(label) = label {
                let s = LabelStore::resolve(self.stores, label);
                let b: String = Space::format_indentation(s.as_bytes())
                    .iter()
                    .map(|x| x.to_string())
                    .collect();
                out.write_str(&b).unwrap();
                if b.contains("\n") {
                    b
                } else {
                    parent_indent[parent_indent.rfind('\n').unwrap_or(0)..].to_owned()
                }
            } else {
                parent_indent[parent_indent.rfind('\n').unwrap_or(0)..].to_owned()
            };
            return Ok(indent);
        }

        match (label, children) {
            (None, None) => {
                out.write_str(&kind.to_string()).unwrap();
                Err(IndentedAlt::NoIndent)
            }
            (label, Some(children)) => {
                if let Some(label) = label {
                    let s = LabelStore::resolve(self.stores, label);
                    dbg!(s);
                }
                if !children.is_empty() {
                    let mut it = children.iter_children();
                    let op = |alt| {
                        if alt == IndentedAlt::NoIndent {
                            Ok(parent_indent[parent_indent.rfind('\n').unwrap_or(0)..].to_owned())
                        } else {
                            Err(alt)
                        }
                    };
                    let mut ind = self
                        .serialize(&it.next().unwrap(), parent_indent, out)
                        .or_else(op)?;
                    for id in it {
                        ind = self.serialize(&id, &ind, out).or_else(op)?;
                    }
                }
                Err(IndentedAlt::NoIndent)
            }
            (Some(label), None) => {
                let s = LabelStore::resolve(self.stores, label);
                out.write_str(&s).unwrap();
                Err(IndentedAlt::NoIndent)
            }
        }
    }
}
impl<'store, 'b, IdN, HAST, const SPC: bool> IndentedSerializer<'store, 'b, IdN, HAST, Json, SPC>
where
    IdN: NodeId<IdN = IdN>,
    HAST: crate::types::NodeStore<IdN>,
    HAST: crate::types::LabelStore<str>,
    HAST: crate::types::TypeStore<HAST::R<'store>>,
    HAST::R<'store>:
        crate::types::Labeled<Label = HAST::I> + crate::types::WithChildren<TreeId = IdN>,
{
    fn serialize(
        &self,
        id: &IdN,
        parent_indent: &str,
        out: &mut std::fmt::Formatter<'_>,
    ) -> Result<String, IndentedAlt> {
        use crate::types::LabelStore;
        use crate::types::Labeled;
        use crate::types::NodeStore;
        use crate::types::Typed;
        use crate::types::WithChildren;
        let b = NodeStore::resolve(self.stores, id);
        // let kind = (self.stores.type_store(), b);
        let kind = self.stores.resolve_type(&b);
        let label = b.try_get_label();
        let children = b.children();

        if kind.is_spaces() {
            let s = LabelStore::resolve(self.stores, &label.unwrap());
            let b:String = //s; //String::new();
        Space::format_indentation(s.as_bytes())
            .iter()
            .map(|x| x.to_string())
            .collect();
            if SPC {
                // let a = &*s;
                // a.iter()
                //     .for_each(|a| Space::fmt(a, &mut b, parent_indent).unwrap());
                out.write_str("{\"kind\":\"")?;
                // out.write_str(&kind.to_string())?;
                out.write_str(&"spaces")?;
                out.write_str("\",\"label\":\"")?;
                out.write_str(&escape(&b))?;
                out.write_str("\"}")?;
            }
            return Ok(if b.contains("\n") {
                b
            } else {
                parent_indent[parent_indent.rfind('\n').unwrap_or(0)..].to_owned()
            });
        }

        match (label, children) {
            (None, None) => {
                out.write_str("\"")?;
                out.write_str(&escape(&kind.to_string()))?;
                out.write_str("\"")?;
                Err(IndentedAlt::NoIndent)
            }
            (label, Some(children)) => {
                out.write_str("{\"kind\":\"")?;
                out.write_str(&escape(&kind.to_string()))?;
                if let Some(label) = label {
                    out.write_str("\",\"label\":\"")?;
                    let s = LabelStore::resolve(self.stores, label);
                    out.write_str(&escape(&s))?;
                }
                if !children.is_empty() {
                    out.write_str("\",\"children\":[").unwrap();
                    let mut it = children.iter_children();
                    let mut ind = self
                        .serialize(&it.next().unwrap(), parent_indent, out)
                        .unwrap_or(
                            parent_indent[parent_indent.rfind('\n').unwrap_or(0)..].to_owned(),
                        );
                    for id in it {
                        out.write_str(",").unwrap();
                        ind = self.serialize(&id, &ind, out).unwrap_or(
                            parent_indent[parent_indent.rfind('\n').unwrap_or(0)..].to_owned(),
                        );
                    }
                    out.write_str("]}").unwrap();
                } else {
                    out.write_str("\"}").unwrap();
                }
                Err(IndentedAlt::NoIndent)
            }
            (Some(label), None) => {
                out.write_str("{\"kind\":\"").unwrap();
                out.write_str(&escape(&kind.to_string())).unwrap();
                out.write_str("\",\"label\":\"").unwrap();
                let s = LabelStore::resolve(self.stores, label);
                out.write_str(&escape(&s)).unwrap();
                out.write_str("\"}").unwrap();
                Err(IndentedAlt::NoIndent)
            }
        }
    }
}

#[derive(PartialEq, Eq)]
pub enum IndentedAlt {
    FmtError,
    NoIndent,
}
impl From<std::fmt::Error> for IndentedAlt {
    fn from(_: std::fmt::Error) -> Self {
        IndentedAlt::FmtError
    }
}