sonic_rs/
input.rs

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
use bytes::Bytes;
use faststr::FastStr;

use crate::{parser::as_str, util::private::Sealed};

/// JsonSlice is a wrapper for different json input.
#[doc(hidden)]
#[non_exhaustive]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum JsonSlice<'de> {
    Raw(&'de [u8]),
    FastStr(FastStr), // note: FastStr maybe inlined and in the stack.
}

impl<'de> JsonSlice<'de> {
    pub fn slice_ref(&self, subset: &'de [u8]) -> Self {
        match self {
            JsonSlice::Raw(_) => JsonSlice::Raw(subset),
            JsonSlice::FastStr(f) => JsonSlice::FastStr(f.slice_ref(as_str(subset))),
        }
    }
}

impl<'de> From<FastStr> for JsonSlice<'de> {
    fn from(value: FastStr) -> Self {
        JsonSlice::FastStr(value)
    }
}

impl<'de> From<Bytes> for JsonSlice<'de> {
    fn from(value: Bytes) -> Self {
        JsonSlice::FastStr(unsafe { FastStr::from_bytes_unchecked(value) })
    }
}

impl<'de> From<&'de [u8]> for JsonSlice<'de> {
    fn from(value: &'de [u8]) -> Self {
        JsonSlice::Raw(value)
    }
}

impl<'de> From<&'de str> for JsonSlice<'de> {
    fn from(value: &'de str) -> Self {
        JsonSlice::Raw(value.as_bytes())
    }
}

impl<'de> From<&'de String> for JsonSlice<'de> {
    fn from(value: &'de String) -> Self {
        JsonSlice::Raw(value.as_bytes())
    }
}

impl From<String> for JsonSlice<'_> {
    fn from(value: String) -> Self {
        JsonSlice::FastStr(FastStr::new(value))
    }
}

impl<'de> AsRef<[u8]> for JsonSlice<'de> {
    fn as_ref(&self) -> &[u8] {
        match self {
            Self::Raw(r) => r,
            Self::FastStr(s) => s.as_bytes(),
        }
    }
}

/// A trait for string/bytes-like types that can be parsed into JSON.
pub trait JsonInput<'de>: Sealed {
    fn need_utf8_valid(&self) -> bool;
    fn to_json_slice(&self) -> JsonSlice<'de>;
    #[allow(clippy::wrong_self_convention)]
    fn from_subset(&self, sub: &'de [u8]) -> JsonSlice<'de>;
    fn to_u8_slice(&self) -> &'de [u8];
}

impl<'de> JsonInput<'de> for &'de [u8] {
    fn need_utf8_valid(&self) -> bool {
        true
    }

    fn to_json_slice(&self) -> JsonSlice<'de> {
        JsonSlice::Raw(self)
    }

    fn from_subset(&self, sub: &'de [u8]) -> JsonSlice<'de> {
        sub.into()
    }

    fn to_u8_slice(&self) -> &'de [u8] {
        self
    }
}

impl<'de> JsonInput<'de> for &'de str {
    fn need_utf8_valid(&self) -> bool {
        false
    }
    fn to_json_slice(&self) -> JsonSlice<'de> {
        JsonSlice::Raw((*self).as_bytes())
    }

    fn from_subset(&self, sub: &'de [u8]) -> JsonSlice<'de> {
        sub.into()
    }

    fn to_u8_slice(&self) -> &'de [u8] {
        (*self).as_bytes()
    }
}

impl<'de> JsonInput<'de> for &'de Bytes {
    fn need_utf8_valid(&self) -> bool {
        true
    }

    fn to_json_slice(&self) -> JsonSlice<'de> {
        let bytes = self.as_ref();
        let newed = self.slice_ref(bytes);
        JsonSlice::FastStr(unsafe { FastStr::from_bytes_unchecked(newed) })
    }

    fn from_subset(&self, sub: &'de [u8]) -> JsonSlice<'de> {
        self.slice_ref(sub).into()
    }

    fn to_u8_slice(&self) -> &'de [u8] {
        (*self).as_ref()
    }
}

impl<'de> JsonInput<'de> for &'de FastStr {
    fn need_utf8_valid(&self) -> bool {
        false
    }

    fn to_json_slice(&self) -> JsonSlice<'de> {
        JsonSlice::FastStr((**self).clone())
    }

    fn from_subset(&self, sub: &'de [u8]) -> JsonSlice<'de> {
        self.slice_ref(as_str(sub)).into()
    }

    fn to_u8_slice(&self) -> &'de [u8] {
        (*self).as_ref()
    }
}

impl<'de> JsonInput<'de> for &'de String {
    fn need_utf8_valid(&self) -> bool {
        false
    }

    fn to_json_slice(&self) -> JsonSlice<'de> {
        JsonSlice::Raw(self.as_bytes())
    }

    fn from_subset(&self, sub: &'de [u8]) -> JsonSlice<'de> {
        sub.into()
    }

    fn to_u8_slice(&self) -> &'de [u8] {
        (*self).as_bytes()
    }
}