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
use crate::{escape::escape_str, JsonBuffer};

/// A value that is able to be written directly into JSON.
pub trait WriteToJson<S: JsonBuffer> {
    fn write_to_json(self, out: &mut S);
}

macro_rules! impl_int {
    ($($ty:ty),*) => {
        $(
            impl<S: JsonBuffer> WriteToJson<S> for $ty {
                #[inline(always)]
                fn write_to_json(self, out: &mut S) {
                    let mut int_buf = itoa::Buffer::new();
                    out.push_str(int_buf.format(self));
                }
            }
        )*

    }
}

macro_rules! impl_float {
    ($($ty:ty),*) => {
        $(
            impl<S: JsonBuffer> WriteToJson<S> for $ty {
                #[inline(always)]
                fn write_to_json(self, out: &mut S) {
                    let mut float_buf = ryu::Buffer::new();
                    out.push_str(float_buf.format(self));
                }
            }
        )*

    }
}

impl_int!(u8, u16, u32, u64, i8, i16, i32, i64);
impl_float!(f32, f64);

impl<S: JsonBuffer> WriteToJson<S> for &str {
    #[inline(always)]
    fn write_to_json(self, out: &mut S) {
        out.push('"');
        escape_str(self, out);
        out.push('"');
    }
}

impl<S: JsonBuffer> WriteToJson<S> for bool {
    #[inline(always)]
    fn write_to_json(self, out: &mut S) {
        match self {
            true => out.push_str("true"),
            false => out.push_str("false"),
        }
    }
}

/// The JSON null value!
pub struct Null;

impl<S: JsonBuffer> WriteToJson<S> for () {
    #[inline(always)]
    fn write_to_json(self, out: &mut S) {
        out.push_str("null")
    }
}

impl<S: JsonBuffer> WriteToJson<S> for Null {
    #[inline(always)]
    fn write_to_json(self, out: &mut S) {
        out.push_str("null")
    }
}

impl<S: JsonBuffer, T> WriteToJson<S> for &T
where
    T: Copy + WriteToJson<S>,
{
    #[inline(always)]
    fn write_to_json(self, out: &mut S) {
        (*self).write_to_json(out)
    }
}

impl<S: JsonBuffer, T> WriteToJson<S> for Option<T>
where
    T: WriteToJson<S>,
{
    #[inline(always)]
    fn write_to_json(self, out: &mut S) {
        match self {
            Some(v) => v.write_to_json(out),
            None => Null.write_to_json(out),
        }
    }
}

/// A string that will *not* have escapes applied to it. You should only use this if you're *absolutely* sure you don't need them.
#[repr(transparent)]
pub struct UnescapedStr<'a>(&'a str);

impl<'a> UnescapedStr<'a> {
    #[inline(always)]
    pub fn create(val: &'a str) -> UnescapedStr<'a> {
        debug_assert!(
            val.as_bytes()
                .iter()
                .all(|val| { crate::escape::ESCAPE[*val as usize] == 0 }),
            "string contains characters that need to be escaped!"
        );

        UnescapedStr(val)
    }
}

impl<'a, S: JsonBuffer> WriteToJson<S> for UnescapedStr<'a> {
    #[inline(always)]
    fn write_to_json(self, out: &mut S) {
        out.push('"');
        out.push_str(self.0);
        out.push('"');
    }
}