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
use crate::grammar::parse_tree::Span;
use crate::message::horiz::Horiz;
use crate::message::indent::Indent;
use crate::message::styled::Styled;
use crate::message::text::Text;
use crate::message::vert::Vert;
use crate::message::wrap::Wrap;
use crate::message::{Content, Message};
use crate::style::Style;

pub struct MessageBuilder {
    span: Span,
    heading: Option<Box<dyn Content>>,
    body: Option<Box<dyn Content>>,
}

pub struct HeadingCharacter {
    message: MessageBuilder,
}

pub struct BodyCharacter {
    message: MessageBuilder,
}

impl MessageBuilder {
    pub fn new(span: Span) -> Self {
        MessageBuilder {
            span,
            heading: None,
            body: None,
        }
    }

    pub fn heading(self) -> Builder<HeadingCharacter> {
        Builder::new(HeadingCharacter { message: self })
    }

    pub fn body(self) -> Builder<BodyCharacter> {
        Builder::new(BodyCharacter { message: self })
    }

    pub fn end(self) -> Message {
        Message::new(
            self.span,
            self.heading.expect("never defined a heading"),
            self.body.expect("never defined a body"),
        )
    }
}

impl Character for HeadingCharacter {
    type End = MessageBuilder;

    fn end(mut self, items: Vec<Box<dyn Content>>) -> MessageBuilder {
        assert!(
            self.message.heading.is_none(),
            "already defined a heading for this message"
        );
        self.message.heading = Some(Box::new(Vert::new(items, 1)));
        self.message
    }
}

impl Character for BodyCharacter {
    type End = MessageBuilder;

    fn end(mut self, items: Vec<Box<dyn Content>>) -> MessageBuilder {
        assert!(
            self.message.body.is_none(),
            "already defined a body for this message"
        );
        self.message.body = Some(Box::new(Vert::new(items, 2)));
        self.message
    }
}

///////////////////////////////////////////////////////////////////////////
// Inline builder: Useful for constructing little bits of content: for
// example, converting an Example into something renderable. Using an
// inline builder, if you push exactly one item, then when you call
// `end` that is what you get; otherwise, you get items laid out
// adjacent to one another horizontally (no spaces in between).

pub struct InlineBuilder;

impl InlineBuilder {
    pub fn new() -> Builder<InlineBuilder> {
        Builder::new(InlineBuilder)
    }
}

impl Character for InlineBuilder {
    type End = Box<dyn Content>;

    fn end(self, mut items: Vec<Box<dyn Content>>) -> Box<dyn Content> {
        if items.len() == 1 {
            items.pop().unwrap()
        } else {
            Box::new(Horiz::new(items, 1))
        }
    }
}

///////////////////////////////////////////////////////////////////////////
// Builder -- generic helper for multi-part items

/// The builder is used to construct multi-part items. It is intended
/// to be used in a "method-call chain" style. The base method is
/// called `push`, and it simply pushes a new child of the current
/// parent.
///
/// Methods whose name like `begin_foo` are used to create a new
/// multi-part child; they return a fresh builder corresponding to the
/// child. When the child is completely constructed, call `end` to
/// finish the child builder and return to the parent builder.
///
/// Methods whose name ends in "-ed", such as `styled`, post-process
/// the last item pushed. They will panic if invoked before any items
/// have been pushed.
///
/// Example:
///
/// ```
/// let node = InlineBuilder::new()
///     .begin_lines() // starts a child builder for adjacent lines
///     .text("foo")   // add a text node "foo" to the child builder
///     .text("bar")   // add a text node "bar" to the child builder
///     .end()         // finish the lines builder, return to the parent
///     .end();        // finish the parent `InlineBuilder`, yielding up the
///                    // `lines` child that was pushed (see `InlineBuilder`
///                    // for more details)
/// ```
pub struct Builder<C: Character> {
    items: Vec<Box<dyn Content>>,
    character: C,
}

impl<C: Character> Builder<C> {
    fn new(character: C) -> Self {
        Builder {
            items: vec![],
            character,
        }
    }

    pub fn push<I>(mut self, item: I) -> Self
    where
        I: Into<Box<dyn Content>>,
    {
        self.items.push(item.into());
        self
    }

    fn pop(&mut self) -> Option<Box<dyn Content>> {
        self.items.pop()
    }

    pub fn begin_vert(self, separate: usize) -> Builder<VertCharacter<C>> {
        Builder::new(VertCharacter {
            base: self,
            separate,
        })
    }

    pub fn begin_lines(self) -> Builder<VertCharacter<C>> {
        self.begin_vert(1)
    }

    pub fn begin_horiz(self, separate: usize) -> Builder<HorizCharacter<C>> {
        Builder::new(HorizCharacter {
            base: self,
            separate,
        })
    }

    // "item1item2"
    pub fn begin_adjacent(self) -> Builder<HorizCharacter<C>> {
        self.begin_horiz(1)
    }

    // "item1 item2"
    pub fn begin_spaced(self) -> Builder<HorizCharacter<C>> {
        self.begin_horiz(2)
    }

    pub fn begin_wrap(self) -> Builder<WrapCharacter<C>> {
        Builder::new(WrapCharacter { base: self })
    }

    pub fn styled(mut self, style: Style) -> Self {
        let content = self.pop().expect("bold must be applied to an item");
        self.push(Box::new(Styled::new(style, content)))
    }

    pub fn indented_by(mut self, amount: usize) -> Self {
        let content = self.pop().expect("indent must be applied to an item");
        self.push(Box::new(Indent::new(amount, content)))
    }

    pub fn indented(self) -> Self {
        self.indented_by(2)
    }

    pub fn text<T: ToString>(self, text: T) -> Self {
        self.push(Box::new(Text::new(text.to_string())))
    }

    /// Take the item just pushed and makes some text adjacent to it.
    /// E.g. `builder.wrap().text("foo").adjacent_text(".").end()`
    /// result in `"foo."` being printed without any wrapping in
    /// between.
    pub fn adjacent_text<T: ToString, U: ToString>(mut self, prefix: T, suffix: U) -> Self {
        let item = self.pop().expect("adjacent text must be added to an item");
        let prefix = prefix.to_string();
        let suffix = suffix.to_string();
        if !prefix.is_empty() && !suffix.is_empty() {
            self.begin_adjacent()
                .text(prefix)
                .push(item)
                .text(suffix)
                .end()
        } else if !suffix.is_empty() {
            self.begin_adjacent().push(item).text(suffix).end()
        } else if !prefix.is_empty() {
            self.begin_adjacent().text(prefix).push(item).end()
        } else {
            self.push(item)
        }
    }

    pub fn verbatimed(self) -> Self {
        self.adjacent_text("`", "`")
    }

    pub fn punctuated<T: ToString>(self, text: T) -> Self {
        self.adjacent_text("", text)
    }

    pub fn wrap_text<T: ToString>(self, text: T) -> Self {
        self.begin_wrap().text(text).end()
    }

    pub fn end(self) -> C::End {
        self.character.end(self.items)
    }
}

pub trait Character {
    type End;
    fn end(self, items: Vec<Box<dyn Content>>) -> Self::End;
}

///////////////////////////////////////////////////////////////////////////

pub struct HorizCharacter<C: Character> {
    base: Builder<C>,
    separate: usize,
}

impl<C: Character> Character for HorizCharacter<C> {
    type End = Builder<C>;

    fn end(self, items: Vec<Box<dyn Content>>) -> Builder<C> {
        self.base.push(Box::new(Horiz::new(items, self.separate)))
    }
}

///////////////////////////////////////////////////////////////////////////

pub struct VertCharacter<C: Character> {
    base: Builder<C>,
    separate: usize,
}

impl<C: Character> Character for VertCharacter<C> {
    type End = Builder<C>;

    fn end(self, items: Vec<Box<dyn Content>>) -> Builder<C> {
        self.base.push(Box::new(Vert::new(items, self.separate)))
    }
}

///////////////////////////////////////////////////////////////////////////

pub struct WrapCharacter<C: Character> {
    base: Builder<C>,
}

impl<C: Character> Character for WrapCharacter<C> {
    type End = Builder<C>;

    fn end(self, items: Vec<Box<dyn Content>>) -> Builder<C> {
        self.base.push(Box::new(Wrap::new(items)))
    }
}

impl<T> From<Box<T>> for Box<dyn Content>
where
    T: Content + 'static,
{
    fn from(b: Box<T>) -> Box<dyn Content> {
        b
    }
}