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
// Based on https://github.com/utkarshkukreti/markup.rs/blob/master/markup/src/lib.rs
use std::fmt::{self, Display};

use v_htmlescape::escape;

/// Render trait, used for wrap unsafe expressions `{{ ... }}` when it's in a html template
pub trait Render {
    fn render(&self, f: &mut fmt::Formatter) -> fmt::Result;
}

/// Auto ref trait
pub trait RenderA {
    /// Render in buffer will html escape the string type
    ///
    /// # Safety
    /// Possible overlap if you have a chance to implement:
    /// have a buffer reference in your data type
    fn __renders_it(&self, buf: &mut fmt::Formatter) -> fmt::Result;
}

impl<T: Render + ?Sized> RenderA for T {
    #[inline(always)]
    fn __renders_it(&self, buf: &mut fmt::Formatter) -> fmt::Result {
        Render::render(self, buf)
    }
}
impl Render for str {
    #[inline(always)]
    fn render(&self, f: &mut fmt::Formatter) -> fmt::Result {
        escape(self).fmt(f)
    }
}

impl Render for String {
    #[inline(always)]
    fn render(&self, f: &mut fmt::Formatter) -> fmt::Result {
        escape(self.as_str()).fmt(f)
    }
}

macro_rules! itoa_display {
    ($($ty:ty)*) => {
        $(
            impl Render for $ty {
                #[inline(always)]
                fn render(&self, f: &mut fmt::Formatter) -> fmt::Result {
                    f.write_str(itoa::Buffer::new().format(*self))
                    .map(|_| ())
                    .map_err(|_| fmt::Error)
                }
            }
        )*
    };
}

#[rustfmt::skip]
itoa_display! {
    u8 u16 u32 u64 u128 usize
    i8 i16 i32 i64 i128 isize
}

macro_rules! dtoa_display {
    ($($ty:ty)*) => {
        $(
            impl Render for $ty {
                #[inline(always)]
                fn render(&self, f: &mut fmt::Formatter) -> fmt::Result {
                    f.write_str(dtoa::Buffer::new().format(*self))
                    .map(|_| ())
                    .map_err(|_| fmt::Error)
                }
            }
        )*
    };
}

#[rustfmt::skip]
dtoa_display! {
    f32 f64
}

// TODO: in the future, your future.
impl Render for char {
    #[inline(always)]
    fn render(&self, f: &mut fmt::Formatter) -> fmt::Result {
        escape(&self.to_string()).fmt(f)
    }
}

macro_rules! raw_display {
    ($($ty:ty)*) => {
        $(
            impl Render for $ty {
                #[inline(always)]
                fn render(&self, f: &mut fmt::Formatter) -> fmt::Result {
                    self.fmt(f)
                }
            }
        )*
    };
}

#[rustfmt::skip]
raw_display! {
    bool
}

#[cfg(feature = "json")]
mod json {
    use super::*;
    use crate::at_helpers::{Json, JsonPretty};
    use crate::helpers::io_fmt::IoFmt;
    use serde::Serialize;
    use serde_json::{to_writer, to_writer_pretty};

    impl<'a, S: Serialize> Render for Json<'a, S> {
        #[inline(always)]
        fn render(&self, f: &mut fmt::Formatter) -> fmt::Result {
            to_writer(IoFmt::new(f), self.0).map_err(|_| fmt::Error)
        }
    }

    impl<'a, D: Serialize> Render for JsonPretty<'a, D> {
        #[inline(always)]
        fn render(&self, f: &mut fmt::Formatter) -> fmt::Result {
            to_writer_pretty(IoFmt::new(f), self.0).map_err(|_| fmt::Error)
        }
    }
}