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
//! Pretty-printer with the following notable points (which may be good or bad depending on what you want) :
//!
//! (1) Uses a trait [`Doclike`] to allow insertion of different string types (and already-allocated docs)
//! inline. IE `"a".concat(format!("c"), printer).concat(d, printer)` where `d` is some doc that 
//! was previously allocated. "a" gets allocated as a &str, and format!("c") as an owned string automatically.
//!
//! (2) Allows use of borrowed or owned strings (using `Cow<'_, str>`) by default
//!
//! (3) Allows rendering to be done with format/write macros, instead of exclusively to a target string.
//!
//! (4) Includes macros allowing users to cut down on syntactic noise when composing docs.
//!
//! (5) Uses an explicit allocator [`IndexSet`] for efficiency.
//!
//! (6) Has explicit support for [`Rust Object Notation`] items. The motivating use case for this crate
//! was writing debug implementations for types that have some degree of indirection (like index pointers)
//! keeping them from using the #[derive(Debug)] macro effectively.
//!
//! Taking the printer as the last argument instead of being the `self` item both allows for 
//! more idiomatic dot composition and plays nicer with the idea that the items of interest are Docs, and
//! the Printer is just a thing we have to eventually tether our docs/text to.
//! 
//! # Safety
//!
//! There's no unsafe in this crate, but users should be aware of the following two safety
//! points with respect to runtime errors:
//!
//! 1. This crate uses u32 to keep track of Doc elements the length of documents.
//! Attempting to allocate more than u32::MAX unique docs, or attempting
//! to render a string with a length greater than u32::MAX will cause a runtime error.
//! When I have some free time I'll make the use of u64 in those positions an opt-in feature.
//!
//! 2. the API presented by this crate is safe with respect to memory lifetimes; you won't
//! be able to create a situation where you have a DocPtr to a dropped arena/bad memory, but there's
//! no guard rail to prevent you from creating multiple printers and accidentally trying to render a Doc
//! with some printer A when it was actually allocated in printer B. If you happen to do this, 
//! you'll get a runtime error (panic).
//!
//! [`Doclike`]: trait.Doclike.html
//! [`IndexSet`]: ../indexmap/set/struct.IndexSet.html
//! [`Rust Object Notation`]: https://github.com/ron-rs/ron
//! [`Printer`]: struct.Printer.html

#![allow(unused_parens)]
use std::fmt::{ Display, Formatter, Result as FmtResult };
use std::convert::TryFrom;
use std::borrow::Cow;
use std::hash::{ Hash, BuildHasherDefault };
use std::marker::PhantomData;
use indexmap::IndexSet;
use rustc_hash::FxHasher;

pub mod brackets;
pub mod object;
pub mod ron;

type FxIndexSet<A> = IndexSet<A, BuildHasherDefault<FxHasher>>;

use Doc::*;

///```
/// use shoebill::{ concat, Printer, Doclike };
/// let mut store = Printer::new();
/// let d = concat!(["a", "b", "c", "d"], &mut store);
/// assert_eq!(format!("{}", d.render(80, &mut store)), format!("abcd"));
///```
#[macro_export]
macro_rules! concat {
    ( [], $ctx:expr ) => {
        Nil.alloc($ctx)
    };
    ( [$d:expr], $ctx:expr ) => {
        $d.alloc($ctx)
    };
    ( [$d1:expr, $d2:expr], $ctx:expr ) => {
        $d1.concat($d2, $ctx)
    };
    ( [$d1:expr, $d2:expr, $($tl:expr),*], $ctx:expr) => {
        {
            let d = $d1.concat($d2, $ctx);
            concat!([d, $($tl),*], $ctx)
        }
    };
}

///```
/// use shoebill::{ concat_w, Printer, Doclike, Doc::* };
/// let mut store = Printer::new();
/// let d = concat_w!(["a", "b", "c", "d"], Hardline, &mut store);
/// assert_eq!(format!("{}", d.render(80, &mut store)), format!("a\nb\nc\nd"));
///```
#[macro_export]
macro_rules! concat_w {
    ( [] , $c:expr, $ctx:expr ) => {
        Nil.alloc($ctx)
    };
    ( [$d:expr], $c:expr, $ctx:expr ) => {
        $d.alloc($ctx)
    };
    ( [$d1:expr, $d2:expr], $c:expr, $ctx:expr ) => {
        $d1.concat($c, $ctx).concat($d2, $ctx)
    };
    ( [$d1:expr, $d2:expr, $($tl:expr),*], $c:expr, $ctx:expr) => {
        {
            let d = $d1.concat($c, $ctx).concat($d2, $ctx);
            concat_w!([d, $($tl),*], $c , $ctx)
        }
    };
}

/// Intercalate
#[macro_export]
macro_rules! inter {
    ( [], $sep:expr, $ctx:expr ) => {
        Nil.alloc($ctx)
    };
    ( [$d:expr], $sep:expr, $ctx:expr ) => {
        $d.alloc($ctx)
    };
    ( [$d1:expr, $d2:expr], $sep:expr, $ctx:expr ) => {
        $d1.concat($sep, $ctx).concat($d2, $ctx)
    };
    ( [$d1:expr, $d2:expr, $($tl:expr),*], $sep:expr, $ctx:expr) => {
        {
            let d = $d1.concat($sep, $ctx).concat($d2, $ctx);
            inter!([d, $($tl),*], $sep, $ctx)
        }
    };
}

/// Intercalate, adding a trailing `sep` chracter as well.
#[macro_export]
macro_rules! inter_trailing {
    ( [], $sep:expr, $ctx:expr ) => {
        Nil.alloc($ctx)
    };
    ( [$d:expr], $sep:expr, $ctx:expr ) => {
        $d.alloc($ctx).concat($sep, $ctx)
    };
    ( [$d1:expr, $d2:expr], $sep:expr, $ctx:expr ) => {
        $d1.concat($sep, $ctx).concat($d2, $ctx).concat($sep, $ctx)
    };
    ( [$d1:expr, $d2:expr, $($tl:expr),*], $sep:expr, $ctx:expr) => {
        {
            let d = $d1.concat($sep, $ctx).concat($d2, $ctx);
            inter_trailing!([d, $($tl),*], $sep, $ctx)
        }
    };
}

/// Macro for allowing more pleasant composition items that implement [`Doclike`] using
/// more familiar operators.
/// The infix operators (which are all left-associtive, but allow for grouping w/ parentheses)
///```
/// //`a <> b` : Concat(a, b)
/// //`a <n> b` : Concat(a, Concat(newline, b))
/// //`a <s> b` : Concat(a, Concat(" ", b)) (concat with a space)
/// //`a <h> b` : Concat(a, Concat(" ", b)) (concat with Hardline)
/// //`n @ ds` : prefix ds with a newline
/// //`g @ ds` : make ds a group
/// //`<any u32> @ ds` : nest ds by whatever the amount specified by the integer literal.
///```
/// There are some issues I need to come back to
/// that aren't bugs, but certain things like `"x" (n @ (<s> "y"))` don't work like
/// one might expect them to.
///```rust
///use shoebill::{ compose, Printer, Doclike };
///let mut store = Printer::new();
///let d = compose!(&mut store ; "this" <s> "is" <s> "text");
///assert_eq!(format!("{}", d.render(80, &mut store)), format!("this is text"));
///```
/// [`Doclike`]: trait.Doclike.html
#[macro_export]
macro_rules! compose {
    // Try to collect prefixes first since they're the most specific matches.
    ( $pr:expr; g @ $($rest:tt)* ) => {
        {
            let tl = compose!($pr ; $($rest)*);
            tl.group($pr)
        }
    
    };
    ( $pr:expr; n @ $($rest:tt)* ) => {
        {
            let tl = compose!($pr ; $($rest)*);
            Newline(None).concat(tl, $pr)
        }
    };
    ( $pr:expr; $x:tt @ $($rest:tt)* ) => {
        {
            let tl = compose!($pr ; $($rest)*);
            tl.nest($x, $pr)
        }
    };
    ( $pr:expr; ($($x:tt)*) ) => {
        {
            compose!($pr; $($x)*)
        }
    };
    ( $pr:expr; $l:tt <> $r:tt $($rest:tt)* ) => {
        {
            let l = compose!($pr ; $l);
            let r = compose!($pr ; $r);
            let x = l.concat(r, $pr);
            compose!($pr ; x $($rest)*)
        }
    };
    ( $pr:expr; $l:tt <s> $r:tt $($rest:tt)* ) => {
        {
            let ll = compose!($pr ; $l);
            let rr = compose!($pr ; $r);
            let ll = ll.concat(" ", $pr).concat(rr, $pr);
            compose!($pr ; ll $($rest)*)
        }

    };
    ( $pr:expr; $l:tt <n> $r:tt $($rest:tt)* ) => {
        {
            let ll = compose!($pr ; $l);
            let rr = compose!($pr ; $r);
            let ll = ll.concat(Newline(None), $pr).concat(rr, $pr);
            compose!($pr ; ll $($rest)*)
        }
    };
    ( $pr:expr; $l:tt <z> $r:tt $($rest:tt)* ) => {
        {
            let ll = compose!($pr ; $l);
            let rr = compose!($pr ; $r);
            let newline_zero = Newline(Some("".alloc($pr)));
            let ll = ll.concat(newline_zero, $pr).concat(rr, $pr);
            compose!($pr ; ll $($rest)*)
        }
    };
    ( $pr:expr; $l:tt <h> $r:tt $($rest:tt)* ) => {
        {
            let ll = compose!($pr ; $l);
            let rr = compose!($pr ; $r);
            let ll = ll.concat(Hardline, $pr).concat(rr, $pr);
            compose!($pr ; ll $($rest)*)
        }
    };
    ( $pr:expr ; $d:expr ) => { 
        {
            $d.alloc($pr) 
        }
    };
}

type CowStr<'p> = Cow<'p, str>;

/// A tagged union of either a Cow string, or a [`DocPtr`]. By using
/// this type in conjuction with the [`Doclike`] trait, we get a pretty
/// large amount of code reuse while cutting down on syntax noise
/// since in many cases we can treat string slices, owned strings,
/// and DocPtr items in the exact same way with the only overhead
/// being that of dealing with the enum (IE there are no extra
/// allocations or clones). 
///
/// [`DocPtr`]: struct.DocPtr.html
/// [`Doclike`]: trait.Doclike.html
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum StrOrDoc<'p> {
    S(Cow<'p, str>),
    D(DocPtr<'p>)
}

/// Can create a [`StrOrDoc`] from anything that can become a Cow string
/// (so a CowStr, a string slice, or an owned string)
///
/// [`StrOrDoc`]: enum.StrOrDoc.html
impl<'p, A> From<A> for StrOrDoc<'p>
where CowStr<'p> : From<A> {
    fn from(a : A) -> StrOrDoc<'p> {
        StrOrDoc::S(CowStr::from(a))
    }
}

/// Can create a StrOrDoc from a DocPtr
///
/// [`StrOrDoc`]: enum.StrOrDoc.html
/// [`DocPtr`]: struct.DocPtr.html
impl<'p> From<DocPtr<'p>> for StrOrDoc<'p> {
    fn from(d : DocPtr<'p>) -> StrOrDoc<'p> {
        StrOrDoc::D(d)
    }
}

/// StrOrDoc is doclike; in the case of a doc, you just get the doc
/// back. In the case of a Cow string, we allocate defer
/// to the implementation that's generic over any A such that A
/// can become a Cow string
impl<'p, P : HasPrinter<'p>> Doclike<'p, P> for StrOrDoc<'p> {
    fn alloc(self, pr : &mut P) -> DocPtr<'p> {
        match self {
            StrOrDoc::S(s) => s.alloc(pr),
            StrOrDoc::D(d) => d
        }
    }
}

/// Anything that can be turned into a Cow string (a CowStr, a string slice,
/// or an owned string) is Doclike.
/// 
/// [`Doclike`]: trait.Doclike.html
impl<'p, A, P : HasPrinter<'p>> Doclike<'p, P> for A 
where CowStr<'p> : From<A> {
    fn alloc(self, pr : &mut P) -> DocPtr<'p> {
        let as_cow = CowStr::from(self);
        let (s_idx, _) = pr.printer_mut().strings.insert_full(as_cow);
        let s_idx : u32 = u32::try_from(s_idx).expect("idx > u32::MAX");
        let s_ptr = StringPtr(PhantomData, s_idx);
        let (idx, _) = pr.printer_mut().docs.insert_full(Doc::Text(s_ptr));
        let idx : u32 = u32::try_from(idx).expect("idx > u32::MAX");
        DocPtr(PhantomData, idx as u32)
    }
}


/// A pointer to an allocated string or string slice. You should never have to deal
/// with this. It's only public since `Doc` would leak it otherwise.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StringPtr<'p>(PhantomData<CowStr<'p>>, u32);

impl<'p> StringPtr<'p> {
    pub fn read(self, store : &impl HasPrinter<'p>) -> &CowStr<'p> {
        match self {
            StringPtr(_, idx) => store.printer().strings.get_index(idx as usize).unwrap_or_else(|| {
                if idx > u32::MAX {
                    panic!("StringPtr idx cannot exceed u32::MAX. This shouldn't be possible, so please file an issue.")
                } else {
                    panic!(
                        "StringPtr idx not found. The most likely reason for this is that the doc was rendered
                        with the wrong Printer. If you only have one Printer, or you think this is not the case,
                        please file an issue."
                    )
                }
            })
        }
    }
}


/// Trait that defines items we can treat as Doc elements. We can use methods like
/// alloc, concat, group, nest, etc. directly on types implementing Doclike.
pub trait Doclike<'p, P> : Sized 
where P : HasPrinter<'p> {
    /// Generic over any Doclike, so you can use this for text as well.
    fn alloc(self, pr : &mut P) -> DocPtr<'p>;

    fn concat(self, other : impl Doclike<'p, P>, pr : &mut P) -> DocPtr<'p> {
        let l = self.alloc(pr);
        let r = other.alloc(pr);
        Concat {
            l,
            r,
            has_newline : match (l.has_newline(pr), r.has_newline(pr)) {
                (Some(true), _) | (_, Some(true)) => Some(true),
                (Some(false), _) | (_, Some(false)) => Some(false),
                _ => None
            },
            dist_next_newline : if l.has_newline(pr).is_some() {
                l.dist_next_newline(pr)
            } else {
                l.dist_next_newline(pr) + r.dist_next_newline(pr)
            },
            flat_len : if l.has_newline(pr) == Some(true) {
                l.flat_len(pr)
            } else {
                l.flat_len(pr) + r.flat_len(pr)
            }
        }.alloc(pr)
    }

    fn nest(self, amt : u32, pr: &mut P) -> DocPtr<'p> {
        let doc = self.alloc(pr);
        Nest {
            amt,
            doc,
            has_newline : doc.has_newline(pr),
            dist_next_newline : doc.dist_next_newline(pr),
            flat_len : doc.flat_len(pr),
        }.alloc(pr)
    }

    fn nest_line(self, other : impl Doclike<'p, P>, amt: u32, pr : &mut P) -> DocPtr<'p> {
        self
        .concat(Newline(None).alloc(pr).nest(amt, pr), pr)
        .concat(other, pr)
    }

    fn nest_doc(self, other : impl Doclike<'p, P>, amt: u32, pr: &mut P) -> DocPtr<'p> {
        //compose!(pr ; self <> (amt @ ((Newline(None)) <> other)))
        let mut d = Newline(None).concat(other, pr);
        d = d.nest(amt, pr);
        d = self.concat(d, pr);
        d
    }

    fn nest_doc_zero(self, other : impl Doclike<'p, P>, amt: u32, pr: &mut P) -> DocPtr<'p> {
        let newline_zero = Newline(Some("".alloc(pr)));
        compose!(pr ; self <> (amt @ ((newline_zero) <> other)))
    }

    fn group(self, pr : &mut P) -> DocPtr<'p> {
        let doc = self.alloc(pr);
        Group {
            doc,
            has_newline : doc.has_newline(pr),
            dist_next_newline : doc.dist_next_newline(pr),
            flat_len : doc.flat_len(pr),
        }.alloc(pr)
    }
}

impl<'p, P : HasPrinter<'p>> Doclike<'p, P> for DocPtr<'p> {
// This lets us keep blanket implementations of concat/nest/etc since they use 
// 'text' to actually get a DocPtr.
    fn alloc(self, _ : &mut P) -> DocPtr<'p> {
        self
    }
}


impl<'p, P : HasPrinter<'p>> Doclike<'p, P> for Doc<'p> {
    fn alloc(self, pr : &mut P) -> DocPtr<'p> {
        let (idx, _) = pr.printer_mut().docs.insert_full(self);
        let idx : u32 = u32::try_from(idx).expect("number of unique docs allocted cannot exceed u32::MAX");
        DocPtr(PhantomData, idx as u32)
    }
}

/// A cheap (copy-able) pointer to an allocated Doc.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DocPtr<'p>(PhantomData<Doc<'p>>, u32);

/// [`Renderable`] is a separate thing so that we can use it with 
/// format! and write! style macros. This has two lifetimes so that you can have multiple
/// [`Renderable`] structs in one scope as long as their lifetimes don't overlap. If we ONLY had
/// the <'p> lifetime, we wouldn't be able to do something like:
///```
/// //let s1 = format!("{}", d1.render(printer));
/// //let s2 = format!("{}", d2.render(printer));
///```
/// without getting an error that printer was still borrwed by the first statement,
/// since the borrow by render would be believed to have duration <'p>
///
/// [`Renderable`]: struct.Renderable.html
pub struct Renderable<'x, 'p : 'x, P : HasPrinter<'p>> {
    doc : DocPtr<'p>,
    printer : &'x P,
    line_width : u32,
    flat : bool,
}

/// By implementing this as an instance of `Display` for `Renderable`, 
/// you can render something directly to some sink (as a `Formatter`)
/// without having to build the actual string first and then write it.
///
/// [`Renderable`]: struct.Renderable.html
impl<'x, 'p : 'x, P : HasPrinter<'p>> Display for Renderable<'x, 'p, P>  {
    fn fmt(&self, f : &mut Formatter) -> FmtResult {
        let mut stack = vec![(self.doc, RenderInfo::new(self.flat, 0, 0))];
        let mut size = 0usize;
        let mut eol = self.line_width as usize;
        while let Some((top, info)) = stack.pop() {
            match top.read(self.printer) {
                // Skip all Nil elements.
                Nil => continue,
                Newline(Some(alt)) if info.flat => {
                    stack.push((alt, info));
                },
                // If we get a Newline with no specified flatmode repr, just insert a space.
                Newline(None) if info.flat => {
                    size += 1;
                    write!(f, " ")?;
                }
                Hardline | Newline(_) => {
                    assert!(!info.flat || top.read(self.printer) == Hardline);
                    write!(f, "\n")?;
                    size += 1;
                    eol += (size + self.line_width as usize);
                    for _ in 0..info.nest {
                        write!(f, " ")?;
                        size += 1;
                    }
                },
                Text(ptr) => {
                    let inner = ptr.read(self.printer.printer());
                    size += inner.len();
                    write!(f, "{}", inner)?
                },
                Concat { l, r, .. } => {
                    let lhs_dist_next_newline = if r.has_newline(self.printer).is_some() {
                        r.dist_next_newline(self.printer)
                    } else {
                        r.dist_next_newline(self.printer) + info.dist_next_newline
                    };

                    stack.push((r, info));
                    stack.push((l, info.new_dist_next_newline(lhs_dist_next_newline)));
                },
                Nest { amt, doc, .. } => {
                    let info = info.add_nest(amt as u16);
                    stack.push((doc, info));
                },
                Group { doc, flat_len, .. } => {
                    if info.flat || (size + flat_len as usize + info.dist_next_newline as usize <= eol) {
                        stack.push((doc, info.with_flat(true)))
                    } else {
                        stack.push((doc, info.with_flat(false)))
                    }
                },
            }
        }
        Ok(())
    }
}

impl<'x, 'p : 'x> DocPtr<'p> {
    pub fn render<P : HasPrinter<'p>>(self, line_width : u32, store : &'x P) -> Renderable<'x, 'p, P> {
        Renderable { doc : self, line_width, printer : store, flat : false }
    }

    pub fn render_flat<P : HasPrinter<'p>>(self, line_width : u32, store : &'x P) -> Renderable<'x, 'p, P> {
        Renderable { doc : self, line_width, printer : store, flat : true }
    }
}

/// The actual thing what holds the information used in composing
/// and printing documents. The Cow strings are held in `strings`, and
/// Holds [`CowStr`] elements and Doc elements in separate [`IndexSet`]s.
/// 
/// [`CowStr`]: type.CowStr.html
/// [`Doc`]: enum.Doc.html
/// [`IndexSet`]: ../indexmap/set/struct.IndexSet.html
#[derive(Debug)]
pub struct Printer<'p> {
    strings : FxIndexSet<CowStr<'p>>,
    docs : FxIndexSet<Doc<'p>>,
    dedup : bool,
}

/// By implementing these two methods on your structure, you can
/// use it as both a document/string allocator, and to render documents.
/// The idea is that if you have some larget structure that holds the state
/// you want to reference while building and rendering docs, you just give
/// it some way of accessing a Printer storage unit and then you no longer
/// really have to think about it.
impl<'p> HasPrinter<'p> for Printer<'p> {
    fn printer(&self) -> &Printer<'p> {
        self
    }

    fn printer_mut(&mut self) -> &mut Printer<'p> {
        self
    }
}

/// Trait that allows types to allocate and render Docs. See the trait documentation
/// for more details.
///
/// The type `Printer` is itself an instance of `HasPrinter` so you're free to 
/// use that, but the intended purpose of this trait is to allow users to turn one 
/// of their own types into a printer by adding a `Printer` field, then implementing 
/// `HasPrinter` for their type. For example:
///```
/// //pub struct MyState<'p> {
/// //    ast_elems : Vec<AstElems>,
/// //    modifiers : HashMap<Modifier, Defn>,
/// //    printer : Printer<'p>
/// //}
/// //
/// //impl<'p> HasPrinter<'p> for MyState<'p> {
/// //    fn printer(&self) -> &Printer<'p> {
/// //        self
/// //    }
/// //
/// //    fn printer_mut(&mut self) -> &mut Printer<'p> {
/// //        self
/// //    }
/// //}
/// // 
/// // You can now pass an element of MyState anywhere you need to handle documents.
/// // let my_state : &mut MyState<'p> = ...;
/// // let doc1 = ...;
/// // let doc2 = ...;
/// // doc3 = doc1.concat(doc2, my_state);
/// // println!("{}", doc3.render(80, my_state));
///```

pub trait HasPrinter<'p> {
    fn printer(&self) -> &Printer<'p>;
    fn printer_mut(&mut self) -> &mut Printer<'p>;
}

impl<'p> Printer<'p> {
    pub fn new() -> Self {
        Printer {
            strings : FxIndexSet::with_hasher(Default::default()),
            docs : FxIndexSet::with_hasher(Default::default()),
            dedup : true,
        }
    }
}

/// The inner document type. Newline gives you the option of specifying
/// an alternate representation (as an Option<StringPtr>) in cases where
/// the renderer is in flat mode, where being in flat mode means you've 
/// either elected to specifically render something as flat, or you elected to
/// group elements together (as a `Group` node), and all of the grouped elements
/// will fit onto the current line without exceeding the line width specified 
/// for rendering.
///
/// The default behavior of `Newline(None)` in flatmode is to render a space.
/// `Hardline` will always render as a linebreak, no matter what.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Doc<'p> {
    Nil,
    Hardline,
    Newline(Option<DocPtr<'p>>),
    Text(StringPtr<'p>),
    Concat {
        l : DocPtr<'p>,
        r : DocPtr<'p>,
        flat_len : u32,
        has_newline : Option<bool>,
        dist_next_newline : u32,
    },
    Nest {
        amt : u32, 
        doc : DocPtr<'p>,
        flat_len : u32,
        has_newline : Option<bool>,
        dist_next_newline : u32,
    },
    Group {
        doc : DocPtr<'p>,
        flat_len : u32,
        has_newline : Option<bool>,
        dist_next_newline : u32,
    },
}

impl<'p> DocPtr<'p> {
    pub fn read(self, store : &impl HasPrinter<'p>) -> Doc<'p> {
        match self {
            DocPtr(_, idx) => store.printer().docs.get_index(idx as usize).copied().unwrap_or_else(|| {
                if idx > u32::MAX {
                    panic!("DocPtr idx cannot exceed u32::MAX. This shouldn't be possible, so please file an issue.")
                } else {
                    panic!(
                        "DocPtr idx not found. The most likely reason for this is that the doc was rendered
                        with the wrong Printer. If you only have one Printer, or you think this is not the case,
                        please file an issue."
                    )
                }
            })
        }
    }

    fn has_newline(&self, pr : &impl HasPrinter<'p>) -> Option<bool> {
        match self.read(pr.printer()) {
            Nil => None,
            Hardline => Some(true),
            Newline(..) => Some(false),
            Concat { has_newline, .. } 
            | Nest   { has_newline, .. }
            | Group  { has_newline, .. } => has_newline,
            Text   { .. }              => None,
        }
    }   
    
    fn dist_next_newline(&self, pr : &impl HasPrinter<'p>) -> u32 {
        match self.read(pr.printer()) {
            Nil 
            | Hardline 
            | Newline(..) => 0,
            Concat   { dist_next_newline, .. }
            | Nest   { dist_next_newline, .. } 
            | Group  { dist_next_newline, .. } => dist_next_newline,
            Text(_)          => self.flat_len(pr),
        }
    }
    
    pub(crate) fn flat_len(self, pr : &impl HasPrinter<'p>) -> u32 {
        match self.read(pr.printer()) {
            Nil 
            | Hardline => 0,
            Concat { flat_len, .. }
            | Nest { flat_len, .. }
            | Group { flat_len, .. } => flat_len,
            Newline(None) => 1,
            Newline(Some(d)) => d.flat_len(pr),
            Text(s)          => u32::try_from(s.read(pr).len()).expect("flat_len got a string whose length was > u32::MAX"),
        }
    }
}

/// Just used to move rendering info between nodes.
#[derive(Debug, Clone, Copy)]
struct RenderInfo {
    flat : bool,
    nest : u16,
    dist_next_newline : u32,
}

impl Default for RenderInfo {
    fn default() -> Self {
        RenderInfo {
            flat : false,
            nest : 0,
            dist_next_newline : 0
        }
    }
}

impl RenderInfo {
    fn new(
        flat : bool, 
        nest : u16, 
        dist_next_newline : u32 
    ) -> Self {
        RenderInfo {
            flat,
            nest,
            dist_next_newline,
        }
    }

    fn add_nest(self, addition : u16) -> Self {
        RenderInfo {
            flat : self.flat,
            nest : self.nest + addition,
            dist_next_newline : self.dist_next_newline,
        }
    }

    fn new_dist_next_newline(self, dist_next_newline : u32) -> Self {
        RenderInfo {
            flat : self.flat,
            nest : self.nest,
            dist_next_newline,
        }
    }

    fn with_flat(self, flat : bool) -> Self {
        RenderInfo {
            flat,
            nest : self.nest,
            dist_next_newline : self.dist_next_newline,
        }
    }
}

// Needs to be here so I don't have to make flat_len() public.
#[cfg(test)]
mod unit_tests {
    use super::*;
    #[test]
    fn flat_len_test1() {
        let mut store = Printer::new();
        let _x = compose!(&mut store ; "four" <h> "five5" <> "six___");
        assert_eq!(_x.flat_len(&store), 4);
        assert_eq!(_x.dist_next_newline(&store), 4);
    }       

    #[test]
    fn flat_len_test2() {
        let mut store = Printer::new();
        let _x = compose!(&mut store ; "four" <n> "five5" <> "six___");
        assert_eq!(_x.flat_len(&store), 16);
        assert_eq!(_x.dist_next_newline(&store), 4);
    }

    #[test]
    fn flat_len_test3() {
        let mut store = Printer::new();
        let _x = compose!(&mut store ; "four" <z> "five5" <> "six___");
        assert_eq!(_x.flat_len(&store), 15);
        assert_eq!(_x.dist_next_newline(&store), 4);
    }

    #[test]
    fn flat_len_test4() {
        let mut store = Printer::new();
        let _x = compose!(&mut store ; "333" <> "four" <h> "five5" <> "six___");
        assert_eq!(_x.flat_len(&store), 7);
        assert_eq!(_x.dist_next_newline(&store), 7);
    }       

    #[test]
    fn flat_len_test5() {
        let mut store = Printer::new();
        let _x = compose!(&mut store ; "333" <> "four" <n> "five5" <> "six___");
        assert_eq!(_x.flat_len(&store), 19);
        assert_eq!(_x.dist_next_newline(&store), 7);
    }

    #[test]
    fn flat_len_test6() {
        let mut store = Printer::new();
        let _x = compose!(&mut store ; "333" <> "four" <z> "five5" <> "six___");
        assert_eq!(_x.flat_len(&store), 18);
        assert_eq!(_x.dist_next_newline(&store), 7);
    }

    #[test]
    fn flat_len_test7() {
        let mut store = Printer::new();
        let _x = compose!(&mut store ; g @ "333" <> "four" <h> "five5" <> "six___");
        assert_eq!(_x.flat_len(&store), 7);
        assert_eq!(_x.dist_next_newline(&store), 7);
    }       

    #[test]
    fn flat_len_test8() {
        let mut store = Printer::new();
        let _x = compose!(&mut store ; g @ "333" <> "four" <n> "five5" <> "six___");
        assert_eq!(_x.flat_len(&store), 19);
        assert_eq!(_x.dist_next_newline(&store), 7);
    }

    #[test]
    fn flat_len_test9() {
        let mut store = Printer::new();
        let _x = compose!(&mut store ; g @ "333" <> "four" <z> "five5" <> "six___");
        assert_eq!(_x.flat_len(&store), 18);
        assert_eq!(_x.dist_next_newline(&store), 7);
    }

}