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
#![feature(try_from)]

#[macro_use]
extern crate nom;

pub mod fmt;

use std::ops::{Deref, DerefMut};

pub struct PrettyPrinter<'a, 'b: 'a> {
    padding: &'a str,
    fmt: &'a mut std::fmt::Formatter<'b>,
    on_newline: bool,
}

impl<'a, 'b> PrettyPrinter<'a, 'b> {
    pub fn new(fmt: &'a mut std::fmt::Formatter<'b>, padding: &'a str) -> PrettyPrinter<'a, 'b> {
        PrettyPrinter {
            padding: padding,
            fmt: fmt,
            on_newline: false,
        }
    }

    pub fn fmt(&'a mut self) -> &'a mut std::fmt::Formatter<'b> {
        self.fmt
    }
}

impl<'a, 'b> Deref for PrettyPrinter<'a, 'b> {
    type Target = std::fmt::Formatter<'b>;

    fn deref(&self) -> &Self::Target {
        self.fmt
    }
}

impl<'a, 'b> DerefMut for PrettyPrinter<'a, 'b> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.fmt
    }
}

impl<'a, 'b> std::fmt::Write for PrettyPrinter<'a, 'b> {
    fn write_str(&mut self, mut s: &str) -> std::fmt::Result {
        while !s.is_empty() {
            if self.on_newline {
                self.fmt.write_str(self.padding)?;
            }

            let split = match s.find('\n') {
                Some(pos) => {
                    self.on_newline = true;
                    pos + 1
                }
                None => {
                    self.on_newline = false;
                    s.len()
                }
            };
            self.fmt.write_str(&s[..split])?;
            s = &s[split..];
        }

        Ok(())
    }
}


pub struct ListDisplay<'a, T: std::fmt::Display + 'a>(pub &'a [T]);

impl<'a, T: std::fmt::Display + 'a> std::fmt::Display for ListDisplay<'a, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let mut i = self.0.iter().peekable();
        while let Some(e) = i.next() {
            e.fmt(f)?;
            if i.peek().is_some() {
                write!(f, ", ")?;
            }
        }
        Ok(())
    }
}