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
//! # pretty-trait
//!
//! `pretty-trait` is a simple trait-based library for producing pretty debug output.  It is
//! intended to make it easy to render large tree-like structures (such as program syntax trees) in
//! such a way that long items are broken across multiple lines and indented.
//!
//! The core feature of this crate is the `Pretty` trait, which represents types that can be
//! pretty-printed.  This crate provides a number of built-in types implementing `Pretty`, which be
//! combined to implement a wide variety of formatting and layout strategies.  For many purposes,
//! you will not need to implement `Pretty` for your own types, but can instead convert your type
//! into a structure composed out of these built-in types.
//!
//! # Examples
//!
//! Converting a custom type to built-in `Pretty` types:
//!
//! ```
//! use pretty_trait::{Pretty, JoinExt, Group, Indent, Sep, delimited, Conditional, to_string};
//!
//! enum NestList {
//!     Atom(i32),
//!     List(Vec<NestList>),
//! }
//!
//! fn to_pretty(nest_list: &NestList) -> Box<Pretty> {
//!     match nest_list {
//!         &NestList::Atom(val) => Box::new(val.to_string()),
//!         &NestList::List(ref children) => {
//!             Box::new(Group::new(
//!                 "["
//!                     .join(Indent(
//!                         Sep(0)
//!                             .join(delimited(&",".join(Sep(1)), children.iter().map(to_pretty)))
//!                             .join(Conditional::OnlyBroken(",")),
//!                     )).join(Sep(0))
//!                     .join("]"),
//!             ))
//!         }
//!     }
//! }
//!
//! let max_line = Some(40);
//! let tab_size = 4;
//!
//! let small_list = NestList::List(vec![NestList::Atom(1), NestList::Atom(2), NestList::Atom(3)]);
//! assert_eq!(to_string(&to_pretty(&small_list), max_line, tab_size), "[1, 2, 3]");
//!
//! let large_list = NestList::List(vec![
//!     NestList::List(vec![
//!         NestList::Atom(1),
//!         NestList::Atom(2),
//!         NestList::Atom(3),
//!         NestList::Atom(4),
//!         NestList::Atom(5),
//!     ]),
//!     NestList::List(vec![
//!         NestList::Atom(6),
//!         NestList::Atom(7),
//!         NestList::Atom(8),
//!         NestList::Atom(9),
//!         NestList::Atom(10),
//!     ]),
//!     NestList::List(vec![
//!         NestList::List(vec![NestList::Atom(11), NestList::Atom(12), NestList::Atom(13)]),
//!         NestList::List(vec![NestList::Atom(14), NestList::Atom(15), NestList::Atom(16)]),
//!     ]),
//! ]);
//! let expected = "\
//! [
//!     [1, 2, 3, 4, 5],
//!     [6, 7, 8, 9, 10],
//!     [[11, 12, 13], [14, 15, 16]],
//! ]";
//! assert_eq!(to_string(&to_pretty(&large_list), max_line, tab_size), expected);
//! ```

use std::io;
use std::ops::{Add, Mul, Deref};
use std::rc::Rc;

/// Represents the number of visual columns a value would take up if it were displayed on one line,
/// unless it is inherently multi-line.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Size {
    Size(usize),
    MultiLine,
}

impl Size {
    fn exceeds(self, max_line: Option<usize>) -> bool {
        self >
            match max_line {
                Some(max) => Size::Size(max),
                None => Size::MultiLine,
            }
    }
}

impl Add<Size> for Size {
    type Output = Size;

    fn add(self, other: Size) -> Size {
        match (self, other) {
            (Size::Size(size1), Size::Size(size2)) => Size::Size(size1 + size2),
            _ => Size::MultiLine,
        }
    }
}

impl Mul<usize> for Size {
    type Output = Size;

    fn mul(self, other: usize) -> Size {
        match self {
            Size::Size(size) => Size::Size(size * other),
            Size::MultiLine => Size::MultiLine,
        }
    }
}

/// A struct used internally in pretty-printing to store information about the rendering
/// environment.
///
/// You only need to use this struct if you are implementing your own `Pretty` types.  To render an
/// existing `Pretty` type with custom line length and tab size parameters, use the `max_line` and
/// `tab_size` arguments of the [`write`] or [`to_string`] functions.
///
/// [`write`]: fn.write.html
/// [`to_string`]: fn.to_string.html
pub struct Context<'a> {
    /// The maximum desired line length, or `None` if lines may be of unlimited length.
    pub max_line: Option<usize>,

    /// The desired number of spaces to use for a single level of indentation.
    pub tab_size: usize,

    /// The current number of tab stops to be inserted before each new line.
    pub indent_level: usize,

    /// Whether or not the environment has been broken across multiple lines because its contents
    /// were too large.
    pub broken: bool,

    /// The handle to render to.
    pub writer: &'a mut io::Write,
}

impl<'a> Context<'a> {
    fn reborrow<'b>(&'b mut self) -> Context<'b> {
        Context {
            max_line: self.max_line,
            tab_size: self.tab_size,
            indent_level: self.indent_level,
            broken: self.broken,
            writer: &mut self.writer,
        }
    }
}

/// Types which can be pretty-printed.
///
/// Strings implement `Pretty`, as do a number of useful built-in composable wrapper types.  As
/// such, you usually don't need to implement it for your own types, although you can if necessary.
///
/// You usually do not need to directly call the methods defined here, unless your are implementing
/// your own `Pretty` type.  If you just want to render a value to a buffer or an IO handle, use one
/// of the [`write`], [`println_simple`], or [`to_string`] functions instead.
///
/// [`write`]: fn.write.html
/// [`println_simple`]: fn.println_simple.html
/// [`to_string`]: fn.to_string.html
pub trait Pretty {
    /// Calculate the intrinsic size of this value, if it were to be displayed on a single line.
    fn size(&self) -> Size;

    /// Render this value in a given context.
    fn pretty_write(&self, context: Context) -> io::Result<()>;
}

impl<'a, T: Pretty + ?Sized> Pretty for &'a T {
    fn size(&self) -> Size {
        (*self).size()
    }

    fn pretty_write(&self, context: Context) -> io::Result<()> {
        (*self).pretty_write(context)
    }
}

impl<'a, T: Pretty + ?Sized> Pretty for &'a mut T {
    fn size(&self) -> Size {
        (**self).size()
    }

    fn pretty_write(&self, context: Context) -> io::Result<()> {
        (**self).pretty_write(context)
    }
}

impl<'a, T: Pretty + ?Sized> Pretty for Box<T> {
    fn size(&self) -> Size {
        self.deref().size()
    }

    fn pretty_write(&self, context: Context) -> io::Result<()> {
        self.deref().pretty_write(context)
    }
}

impl<'a, T: Pretty + ?Sized> Pretty for Rc<T> {
    fn size(&self) -> Size {
        self.deref().size()
    }

    fn pretty_write(&self, context: Context) -> io::Result<()> {
        self.deref().pretty_write(context)
    }
}

impl<'a> Pretty for &'a str {
    fn size(&self) -> Size {
        Size::Size(self.chars().count())
    }

    fn pretty_write(&self, context: Context) -> io::Result<()> {
        write!(context.writer, "{}", self)
    }
}

impl Pretty for String {
    fn size(&self) -> Size {
        Size::Size(self.chars().count())
    }

    fn pretty_write(&self, context: Context) -> io::Result<()> {
        write!(context.writer, "{}", self)
    }
}

/// A wrapper which groups its contents so they will fit onto one line if possible, even if their
/// environment has been broken across multiple lines.
///
/// # Examples
///
/// ```
/// use pretty_trait::{JoinExt, Group, Sep, to_string};
///
/// let max_line = Some(10);
/// let tab_size = 4;
///
/// let expected_ungrouped = "\
/// hello
/// ,
/// world
/// !";
///
/// assert_eq!(
///     to_string(
///         &"hello"
///             .join(Sep(0))
///             .join(",")
///             .join(Sep(1))
///             .join("world")
///             .join(Sep(0))
///             .join("!"),
///         max_line,
///         tab_size,
///     ),
///     expected_ungrouped
/// );
///
/// let expected_grouped = "\
/// hello,
/// world!";
///
/// assert_eq!(
///     to_string(
///         &Group::new("hello".join(Sep(0)).join(","))
///             .join(Sep(1))
///             .join(Group::new("world".join(Sep(0)).join("!"))),
///         max_line,
///         tab_size,
///     ),
///     expected_grouped,
/// );
/// ```
#[derive(Clone, Copy, Debug)]
pub struct Group<T> {
    size: Size,
    content: T,
}

impl<T: Pretty> Group<T> {
    pub fn new(content: T) -> Self {
        Group {
            size: content.size(),
            content,
        }
    }
}

impl<T: Pretty> Pretty for Group<T> {
    fn size(&self) -> Size {
        self.size
    }

    fn pretty_write(&self, mut context: Context) -> io::Result<()> {
        let indented_size = self.size + Size::Size(context.indent_level * context.tab_size);
        context.broken = indented_size.exceeds(context.max_line);
        self.content.pretty_write(context)
    }
}

/// A whitespace separator, rendered as a space if unbroken or a newline if broken.
///
/// The most common uses of `Sep` are `Sep(1)`, which renders as a single space or a newline, and
/// `Sep(0)`, which introduces a point where a newline will be inserted if the content is broken.
///
/// # Examples
///
/// Breaking into multiple lines:
///
/// ```
/// use pretty_trait::{JoinExt, Sep, to_string};
///
/// let max_line = Some(10);
/// let tab_size = 4;
///
/// // Exceeding the line length without a separator:
/// assert_eq!(to_string(&"hello".join("world!"), max_line, tab_size), "helloworld!");
///
/// let expected_broken = "\
/// hello
/// world!";
///
/// assert_eq!(
///     to_string(&"hello".join(Sep(0)).join("world!"), max_line, tab_size),
///     expected_broken
/// );
///
/// assert_eq!(
///     to_string(&"hello".join(Sep(1)).join("world!"), max_line, tab_size),
///     expected_broken
/// );
/// ```
///
/// Introducing spaces on a single line:
///
/// ```
/// # use pretty_trait::{JoinExt, Sep, to_string};
/// # let tab_size = 4;
/// #
/// assert_eq!(
///     to_string(&"hello".join(Sep(1)).join("world!"), None, tab_size),
///     "hello world!"
/// );
///
/// assert_eq!(
///     to_string(&"hello".join(Sep(0)).join("world!"), None, tab_size),
///     "helloworld!"
/// );
/// ```
#[derive(Clone, Copy, Debug)]
pub struct Sep(pub usize);

impl Pretty for Sep {
    fn size(&self) -> Size {
        Size::Size(self.0)
    }

    fn pretty_write(&self, context: Context) -> io::Result<()> {
        if context.broken {
            writeln!(context.writer, "")?;
            for _ in 0..(context.tab_size * context.indent_level) {
                write!(context.writer, " ")?;
            }
        } else {
            for _ in 0..self.0 {
                write!(context.writer, " ")?;
            }
        }
        Ok(())
    }
}

/// An unconditional newline.
///
/// Always causes its environment to break.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use pretty_trait::{JoinExt, Newline, to_string};
///
/// let expected = "\
/// hello
/// world";
///
/// assert_eq!(to_string(&"hello".join(Newline).join("world"), None, 4), expected);
/// ```
#[derive(Clone, Copy, Debug)]
pub struct Newline;

impl Pretty for Newline {
    fn size(&self) -> Size {
        Size::MultiLine
    }

    fn pretty_write(&self, context: Context) -> io::Result<()> {
        writeln!(context.writer, "")?;
        for _ in 0..(context.tab_size * context.indent_level) {
            write!(context.writer, " ")?;
        }
        Ok(())
    }
}

/// A wrapper which indents any newlines inside its contents.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use pretty_trait::{JoinExt, Sep, Indent, to_string};
///
/// let max_line = Some(20);
/// let tab_size = 4;
///
/// let expected = "\
/// (
///     lorem
///     ipsum
///     dolor
///     sit
///     amet
/// )";
///
/// assert_eq!(
///     to_string(
///         &"(".join(Indent(
///             Sep(0)
///                 .join("lorem")
///                 .join(Sep(1))
///                 .join("ipsum")
///                 .join(Sep(1))
///                 .join("dolor")
///                 .join(Sep(1))
///                 .join("sit")
///                 .join(Sep(1))
///                 .join("amet")
///         )).join(Sep(0)).join(")"),
///         max_line,
///         tab_size,
///     ),
///     expected
/// );
/// ```
///
/// # Caution
///
/// To indent a block enclosed in paired delimiters like brackets, care must be taken to ensure that
/// the first line of the content *is* indented, and that the closing delimiter *is not* indented
/// along with its contents.  To ensure this, the newline after the opening delimiter should occur
/// *inside* the `Indent` block, and the newline before the closing delimiter should occur *outside*
/// the `Indent` block, as in the example above.
#[derive(Clone, Copy, Debug)]
pub struct Indent<T>(pub T);

impl<T: Pretty> Pretty for Indent<T> {
    fn size(&self) -> Size {
        self.0.size()
    }

    fn pretty_write(&self, mut context: Context) -> io::Result<()> {
        context.indent_level += 1;
        self.0.pretty_write(context)
    }
}

/// A wrapper which concatenates two pretty-printable values.
///
/// This struct is created by the [`join`] method from the `JoinExt` trait.  See its documentation
/// for more.
///
/// [`join`]: trait.JoinExt.html#method.join
#[derive(Clone, Copy, Debug)]
pub struct Join<T, U>(pub T, pub U);

impl<T: Pretty, U: Pretty> Pretty for Join<T, U> {
    fn size(&self) -> Size {
        self.0.size() + self.1.size()
    }

    fn pretty_write(&self, mut context: Context) -> io::Result<()> {
        self.0.pretty_write(context.reborrow())?;
        self.1.pretty_write(context)?;
        Ok(())
    }
}

/// Allows `join` to be called on any `Pretty` type.
///
/// This trait is automatically implemented for all `Pretty` types.  It should never be implemented
/// manually.
pub trait JoinExt: Sized {
    /// Concatenate two pretty-printable values.  This directly displays one after the other, with
    /// no separation or line breaks.  For separation, use the [`Sep`] type.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use pretty_trait::{JoinExt, to_string};
    ///
    /// let max_line = Some(10);
    /// let tab_size = 4;
    ///
    /// // Exceeds maximum line length, but does not break because there is no break-point:
    /// assert_eq!(
    ///     to_string(&"hello".join("world!"), max_line, tab_size),
    ///     "helloworld!"
    /// );
    /// ```
    ///
    /// [`Sep`]: struct.Sep.html
    fn join<U>(self, other: U) -> Join<Self, U>;
}

impl<T: Pretty> JoinExt for T {
    fn join<U>(self, other: U) -> Join<Self, U> {
        Join(self, other)
    }
}

/// A wrapper that concatenates an arbitrary sequence of pretty-printable values.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use pretty_trait::{JoinExt, Sep, Seq, to_string};
///
/// let max_line = Some(10);
/// let tab_size = 4;
///
/// let expected = "\
/// lorem
/// ipsum
/// dolor
/// sit
/// amet";
///
/// assert_eq!(
///     to_string(
///         &Seq(vec![
///             "lorem".join(Some(Sep(1))),
///             "ipsum".join(Some(Sep(1))),
///             "dolor".join(Some(Sep(1))),
///             "sit".join(Some(Sep(1))),
///             "amet".join(None),
///         ]),
///         max_line,
///         tab_size,
///     ),
///     expected
/// );
/// ```
///
/// # Note
///
/// Because a `Seq` is just a thin wrapper around a `Vec`, all of its items must be of the same
/// type.  When working with combinators like `join` this can sometimes be confusing. For example,
/// the following code will not compile because the final element of the `Vec` does not have the
/// same type as the others:
///
/// ```compile_fail
/// # use pretty_trait::{JoinExt, Seq, Sep};
/// Seq(vec![
///     "lorem".join(Some(Sep(1))),
///     "ipsum".join(Some(Sep(1))),
///     "dolor".join(Some(Sep(1))),
///     "sit".join(Some(Sep(1))),
///     "amet",
/// ]);
/// ```
#[derive(Clone, Debug)]
pub struct Seq<T>(pub Vec<T>);

impl<T: Pretty> Pretty for Seq<T> {
    fn size(&self) -> Size {
        self.0.iter().fold(
            Size::Size(0),
            |total, item| total + item.size(),
        )
    }

    fn pretty_write(&self, mut context: Context) -> io::Result<()> {
        for item in &self.0 {
            item.pretty_write(context.reborrow())?;
        }
        Ok(())
    }
}

/// Render a pretty-printable value to an arbitrary `io::Write` handle.
///
/// This is the most general way to render a `Pretty` type.
pub fn write<T: Pretty>(
    writer: &mut io::Write,
    content: &T,
    max_line: Option<usize>,
    tab_size: usize,
) -> io::Result<()> {
    let size = content.size();
    let context = Context {
        max_line,
        tab_size,
        indent_level: 0,
        broken: size.exceeds(max_line),
        writer,
    };
    content.pretty_write(context)
}

/// Render a pretty-printable value to an owned string and return it.
///
/// If you just want to write a value to standard output, you probably want one of the more
/// efficient [`println_simple`] or [`write`] functions instead.
///
/// # Panics
///
/// Because `Pretty` is defined in terms of writing to an `io::Write` handle, not a string, there is
/// no guarantee that rendering a `Pretty` type will produce valid UTF-8.  None of the built-in
/// types in the `pretty-trait` crate will produce invalid UTF-8, but if a custom `Pretty` type
/// generates invalid UTF-8 then this function will panic.
///
/// [`println_simple`]: fn.println_simple.html
/// [`write`]: fn.write.html
pub fn to_string<T: Pretty>(content: &T, max_line: Option<usize>, tab_size: usize) -> String {
    let mut result = Vec::new();
    write(&mut result, content, max_line, tab_size).expect("Writing to a string should not fail");
    String::from_utf8(result).expect("Invalid UTF8")
}

/// Conveniently render a pretty-printable value to standard output.
///
/// This function uses a default maximum line length of 80 characters, and a tab size of 4 spaces.
pub fn println_simple<T: Pretty>(content: &T) {
    write(&mut io::stdout(), content, Some(80), 2).unwrap();
    println!("");
}

/// A wrapper which decides whether or not to render its contents based on the breaking mode of the
/// environment.
///
/// # Examples
///
/// Adding a trailing comma only when broken:
///
/// ```
/// use pretty_trait::{JoinExt, Sep, Conditional, to_string};
///
/// let max_line = Some(10);
/// let tab_size = 4;
///
/// let to_render = "lorem,"
///     .join(Sep(1))
///     .join("ipsum,")
///     .join(Sep(1))
///     .join("dolor,")
///     .join(Sep(1))
///     .join("sit,")
///     .join(Sep(1))
///     .join("amet")
///     .join(Conditional::OnlyBroken(","));
///
/// // Trailing comma when broken across multiple lines:
///
/// let expected_broken = "\
/// lorem,
/// ipsum,
/// dolor,
/// sit,
/// amet,";
///
/// assert_eq!(to_string(&to_render, max_line, tab_size), expected_broken);
///
/// // No trailing comma when rendered on a single line:
///
/// assert_eq!(
///     to_string(&to_render, None, tab_size),
///     "lorem, ipsum, dolor, sit, amet"
/// );
/// ```
#[derive(Clone, Copy, Debug)]
pub enum Conditional<T> {
    /// Render the wrapped value under all circumstances
    Always(T),

    /// Render the wrapped value only when it appears in a multi-line context
    OnlyBroken(T),

    /// Render the wrapped value only when it appears in a single-line context
    OnlyUnbroken(T),
}

impl<T: Pretty> Pretty for Conditional<T> {
    fn size(&self) -> Size {
        Size::Size(0)
    }

    fn pretty_write(&self, context: Context) -> io::Result<()> {
        match (self, context.broken) {
            (&Conditional::Always(ref inner), _) |
            (&Conditional::OnlyBroken(ref inner), true) |
            (&Conditional::OnlyUnbroken(ref inner), false) => inner.pretty_write(context),
            _ => Ok(()),
        }
    }
}

/// An `Option` will render its contents if it is `Some`, or an empty string if it is `None`.
///
/// This is useful when you need multiple pretty values to have the same type, even though they are
/// not all of exactly the same form when rendered.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use pretty_trait::{JoinExt, Seq, Sep, to_string};
///
/// let tab_size = 4;
///
/// assert_eq!(
///     to_string(
///         &Seq(vec![
///             "lorem".join(Some(",".join(Sep(1)))),
///             "ipsum".join(Some(",".join(Sep(1)))),
///             "dolor".join(Some(",".join(Sep(1)))),
///             "sit".join(Some(",".join(Sep(1)))),
///             "amet".join(None),
///         ]),
///         None,
///         tab_size,
///     ),
///     "lorem, ipsum, dolor, sit, amet"
/// );
/// ```
///
/// If the above example were modified so that it did not use `Option`s, it would not compile
/// because the last item in the `Seq` would have a mismatched type:
///
/// ```compile_fail
/// # use pretty_trait::{JoinExt, Seq, Sep};
/// Seq(vec![
///     "lorem".join(",".join(Some(Sep(1)))),
///     "ipsum".join(",".join(Some(Sep(1)))),
///     "dolor".join(",".join(Some(Sep(1)))),
///     "sit".join(",".join(Some(Sep(1)))),
///     "amet",
/// ]);
/// ```
impl<T: Pretty> Pretty for Option<T> {
    fn size(&self) -> Size {
        match self {
            &Some(ref inner) => inner.size(),
            &None => Size::Size(0),
        }
    }

    fn pretty_write(&self, context: Context) -> io::Result<()> {
        match self {
            &Some(ref inner) => inner.pretty_write(context),
            &None => Ok(()),
        }
    }
}

/// Separate a sequence of pretty-printable values by a delimiter.
///
/// The delimiter is not included on the last item.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use pretty_trait::{JoinExt, Sep, delimited, to_string};
///
/// assert_eq!(
///     to_string(&delimited(&",".join(Sep(1)), &["lorem", "ipsum", "dolor"]), None, 4),
///     "lorem, ipsum, dolor"
/// );
/// ```
pub fn delimited<Delim, Item, It>(delim: &Delim, it: It) -> Seq<Join<Item, Option<Delim>>>
where
    Delim: Pretty + Clone,
    Item: Pretty,
    It: IntoIterator<Item = Item>,
{
    let mut iter = it.into_iter().peekable();
    let mut results = Vec::new();
    while let Some(item) = iter.next() {
        let cond_delim = if iter.peek().is_some() {
            Some(delim.clone())
        } else {
            None
        };
        results.push(item.join(cond_delim));
    }
    Seq(results)
}