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
//!
//! Elem trait and building blocks
//!

use super::*;

///
/// Writer struct passed to escapable closure elem
///
pub struct ElemWriteEscapable<'a>(WriteWrap<'a>);

impl<'a> ElemWriteEscapable<'a> {
    pub fn writer_escapable(&mut self) -> WriteWrap {
        self.0.borrow_mut()
    }
    pub fn writer(&mut self) -> tools::EscapeGuard<WriteWrap> {
        tools::escape_guard(self.0.borrow_mut())
    }

    pub fn render<E: Elem>(&mut self, elem: E) -> fmt::Result {
        let tail = elem.render_head(&mut self.as_elem_write())?;
        tail.render(&mut self.as_elem_write())
    }
    fn as_elem_write(&mut self) -> ElemWrite {
        ElemWrite(WriteWrap(self.0 .0))
    }

    pub fn render_map<E: Elem, F: FnOnce() -> E>(&mut self, func: F) -> fmt::Result {
        let elem = func();
        let tail = elem.render_head(&mut self.as_elem_write())?;
        tail.render(&mut self.as_elem_write())
    }

    pub fn session<'b, E: Elem>(&'b mut self, elem: E) -> SessionEscapable<'b, 'a, E> {
        SessionEscapable { elem, writer: self }
    }

    pub fn session_map<'b, E: Elem, F: FnOnce() -> E>(
        &'b mut self,
        func: F,
    ) -> SessionEscapable<'b, 'a, E> {
        let elem = func();
        SessionEscapable { elem, writer: self }
    }
}

///
/// Writer struct passed to closure elem
///
#[must_use]
pub struct ElemWrite<'a>(pub(crate) WriteWrap<'a>);

impl<'a> ElemWrite<'a> {
    pub fn writer(&mut self) -> tools::EscapeGuard<WriteWrap> {
        tools::escape_guard(self.0.borrow_mut())
    }
    pub fn render<E: Elem + Locked>(&mut self, elem: E) -> fmt::Result {
        self.render_inner(elem)
    }

    pub fn render_map<E: Elem + Locked, F: FnOnce() -> E>(&mut self, func: F) -> fmt::Result {
        let elem = func();
        let tail = elem.render_head(self)?;
        tail.render(self)
    }

    pub fn session<'b, E: Elem + Locked>(&'b mut self, elem: E) -> Session<'b, 'a, E> {
        Session { elem, writer: self }
    }

    pub fn session_map<'b, E: Elem + Locked, F: FnOnce() -> E>(
        &'b mut self,
        func: F,
    ) -> Session<'b, 'a, E> {
        let elem = func();
        Session { elem, writer: self }
    }
    fn as_escapable(&mut self) -> ElemWriteEscapable {
        ElemWriteEscapable(WriteWrap(self.0 .0))
    }
    fn writer_escapable(&mut self) -> WriteWrap {
        self.0.borrow_mut()
    }

    fn as_attr_write(&mut self) -> AttrWrite {
        attr::AttrWrite::new(self.0.borrow_mut())
    }

    fn new(w: &'a mut dyn fmt::Write) -> Self {
        ElemWrite(WriteWrap(w))
    }

    fn render_inner<E: Elem>(&mut self, elem: E) -> fmt::Result {
        let tail = elem.render_head(self)?;
        tail.render(self)
    }
}

///
/// Alternative trait for Elem that is friendly to dyn trait.
///
pub trait ElemDyn {
    fn render_head(&mut self, w: &mut ElemWrite) -> Result<(), fmt::Error>;
    fn render_tail(&mut self, w: &mut ElemWrite) -> Result<(), fmt::Error>;
}

///
/// Tail to DynElem
///
pub struct DynamicElementTail<'a> {
    elem: Box<dyn ElemDyn + 'a>,
}
impl<'a> ElemTail for DynamicElementTail<'a> {
    fn render(mut self, w: &mut ElemWrite) -> std::fmt::Result {
        self.elem.render_tail(w)
    }
}

impl Locked for DynamicElement<'_> {}

///
/// A dynamic elem that implement Elem
///
pub struct DynamicElement<'a> {
    elem: Box<dyn ElemDyn + 'a>,
}
impl<'a> DynamicElement<'a> {
    pub fn new<E: Elem + 'a>(elem: E) -> Self {
        ///
        /// A dynamic elem, that
        ///
        pub struct DynamicElem<E: Elem> {
            head: Option<E>,
            tail: Option<E::Tail>,
        }

        impl<E: Elem> DynamicElem<E> {
            pub fn new(elem: E) -> DynamicElem<E> {
                DynamicElem {
                    head: Some(elem),
                    tail: None,
                }
            }
        }
        impl<E: Elem> ElemDyn for DynamicElem<E> {
            fn render_head(&mut self, w: &mut ElemWrite) -> Result<(), fmt::Error> {
                let tail = self.head.take().unwrap().render_head(w)?;
                self.tail = Some(tail);
                Ok(())
            }
            fn render_tail(&mut self, w: &mut ElemWrite) -> Result<(), fmt::Error> {
                self.tail.take().unwrap().render(w)
            }
        }

        DynamicElement {
            elem: Box::new(DynamicElem::new(elem)),
        }
    }
}

impl<'a> Elem for DynamicElement<'a> {
    type Tail = DynamicElementTail<'a>;
    fn render_head(mut self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        self.elem.render_head(w)?;
        Ok(DynamicElementTail { elem: self.elem })
    }
}

///
/// Main building block.
///
pub trait Elem {
    type Tail: ElemTail;
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error>;

    fn render_closure<K>(
        self,
        w: &mut ElemWrite,
        func: impl FnOnce(&mut ElemWrite) -> Result<K, fmt::Error>,
    ) -> Result<K, fmt::Error>
    where
        Self: Sized,
    {
        let tail = self.render_head(w)?;
        let res = func(w)?;
        tail.render(w)?;
        Ok(res)
    }

    /// Render all of Self and head of other, store tail of other.
    fn chain<R: Elem>(self, other: R) -> Chain<Self, R>
    where
        Self: Sized,
    {
        Chain {
            top: self,
            bottom: other,
        }
    }

    /// Render head of Self, and all of other, store tail of self.
    fn append<R: Elem>(self, bottom: R) -> Append<Self, R>
    where
        Self: Sized,
    {
        Append { top: self, bottom }
    }
}

///
/// Indicates that the implementor does that allow arbitrary html escaping.
///
pub trait Locked {}

///
/// Append an element to another adaptor
///
#[must_use]
#[derive(Copy, Clone)]
pub struct Append<A, B> {
    top: A,
    bottom: B,
}

impl<A: Locked, B: Locked> Locked for Append<A, B> {}

impl<A: Elem, B: Elem> Elem for Append<A, B> {
    type Tail = A::Tail;
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        let Append { top, bottom } = self;
        let tail = top.render_head(w)?;
        w.render_inner(bottom)?;
        Ok(tail)
    }
}

///
/// Chain two elements adaptor
///
#[must_use]
#[derive(Copy, Clone)]
pub struct Chain<A, B> {
    top: A,
    bottom: B,
}
impl<A: Locked, B: Locked> Locked for Chain<A, B> {}

impl<A: Elem, B: Elem> Elem for Chain<A, B> {
    type Tail = B::Tail;
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        let Chain { top, bottom } = self;
        w.render_inner(top)?;
        bottom.render_head(w)
    }
}

///
/// Tail to elem trait.
///
pub trait ElemTail {
    fn render(self, w: &mut ElemWrite) -> std::fmt::Result;
}

///
/// Used to start a closure session
///
#[must_use]
pub struct Session<'a, 'b, E> {
    elem: E,
    writer: &'a mut ElemWrite<'b>,
}

impl<'a, 'b, E: Elem> Session<'a, 'b, E> {
    pub fn build(self, func: impl FnOnce(&mut ElemWrite) -> fmt::Result) -> fmt::Result {
        let Session { elem, writer } = self;
        let tail = elem.render_head(writer)?;
        func(writer)?;
        tail.render(writer)
    }
}

///
/// Used to start an escapable closure session
///
#[must_use]
pub struct SessionEscapable<'a, 'b, E> {
    elem: E,
    writer: &'a mut ElemWriteEscapable<'b>,
}

impl<'a, 'b, E: Elem> SessionEscapable<'a, 'b, E> {
    pub fn build(self, func: impl FnOnce(&mut ElemWriteEscapable) -> fmt::Result) -> fmt::Result {
        let SessionEscapable { elem, writer } = self;
        let tail = elem.render_head(&mut writer.as_elem_write())?;
        func(writer)?;
        tail.render(&mut writer.as_elem_write())
    }
}

impl<D: fmt::Display> Locked for D {}
impl<D: fmt::Display> Elem for D {
    type Tail = ();
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        write!(w.writer(), " {}", self)?;
        Ok(())
    }
}

impl<I: FnOnce(&mut ElemWriteEscapable) -> fmt::Result> Elem for ClosureEscapable<I> {
    type Tail = ();
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        (self.func)(&mut w.as_escapable())?;
        Ok(())
    }
}

///
/// An escapable closure elem
///
#[derive(Copy, Clone)]
#[must_use]
pub struct ClosureEscapable<I> {
    func: I,
}

impl<I: FnOnce(&mut ElemWriteEscapable) -> fmt::Result> ClosureEscapable<I> {
    pub fn new(func: I) -> ClosureEscapable<I> {
        ClosureEscapable { func }
    }
}

///
/// A closure elem
///
#[derive(Copy, Clone)]
#[must_use]
pub struct Closure<I> {
    func: I,
}

impl<I: FnOnce(&mut ElemWrite) -> fmt::Result> Closure<I> {
    pub fn new(func: I) -> Closure<I> {
        Closure { func }
    }
}

impl<I: FnOnce(&mut ElemWrite) -> fmt::Result> Locked for Closure<I> {}

impl<I: FnOnce(&mut ElemWrite) -> fmt::Result> Elem for Closure<I> {
    type Tail = ();
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        (self.func)(w)?;
        Ok(())
    }
}

///
/// An iterator of elems
///
#[derive(Copy, Clone)]
#[must_use]
pub struct Iter<I> {
    iter: I,
}
impl<I: IntoIterator<Item = R>, R: Elem> Iter<I> {
    pub fn new(iter: I) -> Iter<I> {
        Iter { iter }
    }
}
impl<I: IntoIterator<Item = R>, R: Locked> Locked for Iter<I> {}

impl<I: IntoIterator<Item = R>, R: Elem> Elem for Iter<I> {
    type Tail = ();
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        for i in self.iter {
            w.render_inner(i)?;
        }
        Ok(())
    }
}

///
/// A raw escapable element
///
#[derive(Copy, Clone)]
#[must_use]
pub struct RawEscapable<D> {
    data: D,
}
impl<D: fmt::Display> RawEscapable<D> {
    pub fn new(data: D) -> RawEscapable<D> {
        RawEscapable { data }
    }
}
impl<D: fmt::Display> Elem for RawEscapable<D> {
    type Tail = ();
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        write!(w.writer_escapable(), " {}", self.data)?;
        Ok(())
    }
}

use fmt::Write;

///
/// A element with no ending tag
///
#[derive(Copy, Clone)]
#[must_use]
pub struct Single<D, A, K, Z> {
    tag: D,
    attr: A,
    start: K,
    ending: Z,
}
impl<D: fmt::Display, A: Attr, K: fmt::Display, Z: fmt::Display> Locked for Single<D, A, K, Z> {}
impl<D: fmt::Display, A: Attr, K, Z> Single<D, A, K, Z> {
    pub fn with<AA: Attr>(self, attr: AA) -> Single<D, AA, K, Z> {
        Single {
            tag: self.tag,
            attr,
            ending: self.ending,
            start: self.start,
        }
    }
    pub fn with_map<AA: Attr, F: FnOnce() -> AA>(self, attr: F) -> Single<D, AA, K, Z> {
        let attr = attr();
        self.with(attr)
    }
    pub fn with_ending<ZZ: fmt::Display>(self, ending: ZZ) -> Single<D, A, K, ZZ> {
        Single {
            tag: self.tag,
            attr: self.attr,
            ending,
            start: self.start,
        }
    }
    pub fn with_start<KK: fmt::Display>(self, start: KK) -> Single<D, A, KK, Z> {
        Single {
            tag: self.tag,
            attr: self.attr,
            ending: self.ending,
            start,
        }
    }
}
impl<D: fmt::Display, A: Attr, K: fmt::Display, Z: fmt::Display> Elem for Single<D, A, K, Z> {
    type Tail = ();
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        let Single {
            tag,
            attr,
            start,
            ending,
        } = self;
        w.writer_escapable().write_char('<')?;
        write!(w.writer(), "{}{}", start, tag)?;
        w.writer().write_char(' ')?;
        attr.render(&mut w.as_attr_write())?;
        write!(w.writer(), " {}", ending)?;
        w.writer_escapable().write_str(">")?;
        Ok(())
    }
}

impl<D: fmt::Display> Single<D, (), &'static str, &'static str> {
    pub fn new(tag: D) -> Self {
        Single {
            tag,
            attr: (),
            start: "",
            ending: "/",
        }
    }
}

///
/// The tail of an element
///
#[derive(Copy, Clone)]
#[must_use]
pub struct ElementTail<D> {
    tag: D,
}

impl<D: fmt::Display> ElemTail for ElementTail<D> {
    fn render(self, w: &mut ElemWrite) -> std::fmt::Result {
        w.writer_escapable().write_str("</")?;
        write!(w.writer(), "{}", &self.tag)?;
        w.writer_escapable().write_char('>')
    }
}

///
/// A regular element with an ending tag
///
#[derive(Copy, Clone)]
#[must_use]
pub struct Element<D, A> {
    tag: D,
    attr: A,
}

impl<D: fmt::Display, A: Attr> Locked for Element<D, A> {}

impl<D: fmt::Display, A: Attr> Element<D, A> {
    pub fn with<AA: Attr>(self, attr: AA) -> Element<D, AA> {
        Element {
            tag: self.tag,
            attr,
        }
    }
    pub fn with_map<AA: Attr, F: FnOnce() -> AA>(self, attr: F) -> Element<D, AA> {
        let attr = attr();
        self.with(attr)
    }
}
impl<D: fmt::Display, A: Attr> Elem for Element<D, A> {
    type Tail = ElementTail<D>;
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        let Element { tag, attr } = self;

        w.writer_escapable().write_char('<')?;
        write!(w.writer(), "{}", tag)?;
        w.writer().write_char(' ')?;
        attr.render(&mut w.as_attr_write())?;
        w.writer_escapable().write_str(" >")?;

        Ok(ElementTail { tag })
    }
}
impl<D: fmt::Display> Element<D, ()> {
    pub fn new(tag: D) -> Self {
        Element { tag, attr: () }
    }
}

///
/// A string buffered element
///
/// If you need to render something over, and over again,
/// you can instead buffer it to a string using this struct
/// for better performance at the cost of more memory.
///
/// Notice that RenderElem is only implemented for a &BufferedElem.
///
pub struct BufferedElem {
    head: String,
    tail: String,
}

impl BufferedElem {
    pub fn new<E: Elem + Locked>(elem: E) -> Result<Self, fmt::Error> {
        let mut head = String::new();
        let mut tail = String::new();
        let t = elem.render_head(&mut ElemWrite::new(&mut head))?;
        t.render(&mut ElemWrite::new(&mut tail))?;
        head.shrink_to_fit();
        tail.shrink_to_fit();
        Ok(BufferedElem { head, tail })
    }

    pub fn into_parts(self) -> (String, String) {
        (self.head, self.tail)
    }
}

///
/// A buffered element's tail
///
pub struct BufferedTail<'a> {
    tail: &'a str,
}
impl<'a> ElemTail for BufferedTail<'a> {
    fn render(self, w: &mut ElemWrite) -> std::fmt::Result {
        write!(w.writer_escapable(), "{}", self.tail)
    }
}
impl<'a> Locked for &'a BufferedElem {}

impl<'a> Elem for &'a BufferedElem {
    type Tail = BufferedTail<'a>;
    fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
        write!(w.writer_escapable(), "{}", self.head)?;
        Ok(BufferedTail { tail: &self.tail })
    }
}

// pub struct DisplayEscapable<D, B> {
//     start: D,
//     end: B,
// }
// pub struct DisplayEscapableTail<'a, D> {
//     end: &'a D,
// }
// impl<A: fmt::Display, B: fmt::Display> DisplayEscapable<A, B> {
//     pub fn new(head: A, tail: B) -> Self {
//         DisplayEscapable {
//             start: head,
//             end: tail,
//         }
//     }
// }
// impl<'b, D: fmt::Display> RenderTail for DisplayEscapableTail<'b, D> {
//     fn render(self, w: &mut ElemWrite) -> std::fmt::Result {
//         write!(w.writer_escapable(), "{}", self.end)
//     }
// }
// impl<'a, A: fmt::Display, B: fmt::Display> Elem for &'a DisplayEscapable<A, B> {
//     type Tail = DisplayEscapableTail<'a, B>;
//     fn render_head(self, w: &mut ElemWrite) -> Result<Self::Tail, fmt::Error> {
//         write!(w.writer_escapable(), "{}", self.start)?;
//         Ok(DisplayEscapableTail { end: &self.end })
//     }
// }

impl ElemTail for () {
    fn render(self, _: &mut ElemWrite) -> std::fmt::Result {
        Ok(())
    }
}