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
// Copyright (c) 2013-2015 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//! UTF-8 encoded text.
//!
//! A `text::Reader<'a>` wraps a `&'a [u8]` that is expected but not guaranteed
//! to contain UTF-8 encoded text.

use core::str;

use crate::Result;

#[derive(Copy, Clone)]
pub struct Owned(());

impl crate::traits::Owned for Owned {
    type Reader<'a> = Reader<'a>;
    type Builder<'a> = Builder<'a>;
}

impl crate::introspect::Introspect for Owned {
    fn introspect() -> crate::introspect::Type {
        crate::introspect::TypeVariant::Text.into()
    }
}

/// Wrapper around utf-8 encoded text.
/// This is defined as a tuple struct to allow pattern matching
/// on it via byte literals (for example `text::Reader(b"hello")`).
#[derive(Copy, Clone, PartialEq, PartialOrd)]
pub struct Reader<'a>(pub &'a [u8]);

impl<'a> core::cmp::PartialEq<&'a str> for Reader<'a> {
    #[inline]
    fn eq(&self, other: &&'a str) -> bool {
        self.as_bytes() == other.as_bytes()
    }
}

impl<'a> core::cmp::PartialEq<Reader<'a>> for &'a str {
    #[inline]
    fn eq(&self, other: &Reader<'a>) -> bool {
        self.as_bytes() == other.as_bytes()
    }
}

#[cfg(feature = "alloc")]
impl<'a> core::cmp::PartialEq<alloc::string::String> for Reader<'a> {
    #[inline]
    fn eq(&self, other: &alloc::string::String) -> bool {
        self.as_bytes() == other.as_bytes()
    }
}

#[cfg(feature = "alloc")]
impl<'a> core::cmp::PartialEq<Reader<'a>> for alloc::string::String {
    #[inline]
    fn eq(&self, other: &Reader<'a>) -> bool {
        self.as_bytes() == other.as_bytes()
    }
}

impl<'a> core::cmp::PartialOrd<&'a str> for Reader<'a> {
    #[inline]
    fn partial_cmp(&self, other: &&'a str) -> Option<core::cmp::Ordering> {
        self.as_bytes().partial_cmp(other.as_bytes())
    }
}

impl<'a> core::cmp::PartialOrd<Reader<'a>> for &'a str {
    #[inline]
    fn partial_cmp(&self, other: &Reader<'a>) -> Option<core::cmp::Ordering> {
        self.as_bytes().partial_cmp(other.as_bytes())
    }
}

impl<'a> core::fmt::Debug for Reader<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self.to_str() {
            Ok(s) => write!(f, "{:?}", s),
            Err(_) => write!(f, "<invalid utf-8: {:?}>", self.as_bytes()),
        }
    }
}

impl<'a> From<&'a str> for Reader<'a> {
    #[inline]
    fn from(value: &'a str) -> Self {
        Self(value.as_bytes())
    }
}

impl<'a> From<&'a [u8]> for Reader<'a> {
    #[inline]
    fn from(value: &'a [u8]) -> Self {
        Self(value)
    }
}

impl<'a, const N: usize> From<&'a [u8; N]> for Reader<'a> {
    fn from(value: &'a [u8; N]) -> Self {
        Self(&value[..])
    }
}

impl<'a> TryFrom<Reader<'a>> for &'a str {
    type Error = core::str::Utf8Error;
    fn try_from(value: Reader<'a>) -> core::result::Result<&'a str, core::str::Utf8Error> {
        let Reader(v) = value;
        str::from_utf8(v)
    }
}

impl<'a> crate::traits::FromPointerReader<'a> for Reader<'a> {
    fn get_from_pointer(
        reader: &crate::private::layout::PointerReader<'a>,
        default: Option<&'a [crate::Word]>,
    ) -> Result<Reader<'a>> {
        reader.get_text(default)
    }
}

impl<'a> Reader<'a> {
    /// The string's length, in bytes.
    #[inline]
    pub fn len(&self) -> usize {
        self.as_bytes().len()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline]
    pub fn as_bytes(self) -> &'a [u8] {
        let Self(d) = self;
        d
    }

    /// Converts to a `str`, returning a error if the data contains invalid utf-8.
    #[inline]
    pub fn to_str(self) -> core::result::Result<&'a str, core::str::Utf8Error> {
        let Self(s) = self;
        str::from_utf8(s)
    }

    #[cfg(feature = "alloc")]
    /// Converts to a `String`, returning a error if the data contains invalid utf-8.
    #[inline]
    pub fn to_string(self) -> core::result::Result<alloc::string::String, core::str::Utf8Error> {
        Ok(self.to_str()?.into())
    }
}

pub struct Builder<'a> {
    /// Does not include the trailing null byte.
    bytes: &'a mut [u8],

    /// Position at which `push_ascii()` and `push_str()` will write to.
    pos: usize,
}

impl<'a> core::cmp::PartialEq for Builder<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.bytes == other.bytes
    }
}

impl<'a> core::cmp::PartialEq<&'a str> for Builder<'a> {
    fn eq(&self, other: &&'a str) -> bool {
        self.bytes == other.as_bytes()
    }
}

impl<'a> core::cmp::PartialEq<Builder<'a>> for &'a str {
    fn eq(&self, other: &Builder<'a>) -> bool {
        self.as_bytes() == other.bytes
    }
}

impl<'a> Builder<'a> {
    #[inline]
    pub fn new(bytes: &mut [u8]) -> Builder<'_> {
        Builder { bytes, pos: 0 }
    }

    #[inline]
    pub fn with_pos(bytes: &mut [u8], pos: usize) -> Builder<'_> {
        Builder { bytes, pos }
    }

    /// The string's length, in bytes.
    #[inline]
    pub fn len(&self) -> usize {
        self.bytes.len()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline]
    pub fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }

    /// Converts to a `str`, returning a error if the data contains invalid utf-8.
    #[inline]
    pub fn to_str(self) -> core::result::Result<&'a str, core::str::Utf8Error> {
        str::from_utf8(self.bytes)
    }

    #[cfg(feature = "alloc")]
    /// Converts to a `String`, returning a error if the data contains invalid utf-8.
    #[inline]
    pub fn to_string(self) -> core::result::Result<alloc::string::String, core::str::Utf8Error> {
        Ok(self.to_str()?.into())
    }

    #[inline]
    pub fn as_bytes_mut(self) -> &'a mut [u8] {
        &mut self.bytes[..]
    }

    /// Writes a single ascii character at position `pos` and increments `pos`.
    #[inline]
    pub fn push_ascii(&mut self, ascii: u8) {
        assert!(ascii < 128);
        self.bytes[self.pos] = ascii;
        self.pos += 1;
    }

    /// Writes a string at position `pos` and increases `pos` a corresponding amount.
    #[inline]
    pub fn push_str(&mut self, string: &str) {
        let bytes = string.as_bytes();
        self.bytes[self.pos..(self.pos + bytes.len())].copy_from_slice(bytes);
        self.pos += bytes.len();
    }

    /// Zeroes all data and resets `pos`.
    pub fn clear(&mut self) {
        for b in &mut self.bytes[..self.pos] {
            *b = 0;
        }
        self.pos = 0;
    }

    #[inline]
    pub fn reborrow(&mut self) -> Builder<'_> {
        Builder {
            bytes: self.bytes,
            pos: self.pos,
        }
    }

    #[inline]
    pub fn into_reader(self) -> Reader<'a> {
        Reader(self.bytes)
    }

    #[inline]
    pub fn reborrow_as_reader(&self) -> Reader<'_> {
        Reader(self.bytes)
    }
}

impl<'a> core::fmt::Debug for Builder<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self.reborrow_as_reader().to_str() {
            Ok(s) => write!(f, "{:?}", s),
            Err(_) => write!(f, "<invalid utf-8>"),
        }
    }
}

impl<'a> crate::traits::FromPointerBuilder<'a> for Builder<'a> {
    fn init_pointer(builder: crate::private::layout::PointerBuilder<'a>, size: u32) -> Builder<'a> {
        builder.init_text(size)
    }
    fn get_from_pointer(
        builder: crate::private::layout::PointerBuilder<'a>,
        default: Option<&'a [crate::Word]>,
    ) -> Result<Builder<'a>> {
        builder.get_text(default)
    }
}

impl<'a> crate::traits::SetterInput<Owned> for Reader<'a> {
    #[inline]
    fn set_pointer_builder<'b>(
        mut pointer: crate::private::layout::PointerBuilder<'b>,
        value: Reader<'a>,
        _canonicalize: bool,
    ) -> Result<()> {
        pointer.set_text(value);
        Ok(())
    }
}

// Allow text fields to be set with &str or String or anything
// else that implements `AsRef<str>`.
impl<T: AsRef<str>> crate::traits::SetterInput<Owned> for T {
    #[inline]
    fn set_pointer_builder(
        mut pointer: crate::private::layout::PointerBuilder<'_>,
        value: T,
        _canonicalize: bool,
    ) -> Result<()> {
        pointer.set_text(value.as_ref().into());
        Ok(())
    }
}

impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
    fn from(t: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
        crate::dynamic_value::Reader::Text(t)
    }
}

impl<'a> From<&'a str> for crate::dynamic_value::Reader<'a> {
    fn from(t: &'a str) -> crate::dynamic_value::Reader<'a> {
        crate::dynamic_value::Reader::Text(t.into())
    }
}

impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
    fn from(t: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
        crate::dynamic_value::Builder::Text(t)
    }
}