serde_encom/des/
read.rs

1#![allow(clippy::zero_prefixed_literal)]
2
3use crate::error::{Error, ErrorCode, Result};
4// use alloc::vec::Vec;
5// use core::char;
6use core::cmp;
7use core::ops::Deref;
8use core::str;
9use debug_unsafe::slice::SliceGetter;
10
11#[cfg(feature = "std")]
12use super::iter::LineColIterator;
13#[cfg(feature = "std")]
14use crate::io;
15#[cfg(feature = "raw_value")]
16use crate::raw::BorrowedRawDeserializer;
17#[cfg(all(feature = "raw_value", feature = "std"))]
18use crate::raw::OwnedRawDeserializer;
19#[cfg(feature = "raw_value")]
20use serde::de::Visitor;
21
22/// Trait used by the deserializer for iterating over input. This is manually
23/// "specialized" for iterating over &[u8]. Once feature(specialization) is
24/// stable we can use actual specialization.
25///
26/// This trait is sealed and cannot be implemented for types outside of
27/// `serde_encom`.
28pub trait Read<'de>: private::Sealed {
29    #[doc(hidden)]
30    fn next(&mut self) -> Result<Option<u8>>;
31    #[doc(hidden)]
32    fn peek(&mut self) -> Result<Option<u8>>;
33
34    /// Only valid after a call to peek(). Discards the peeked byte.
35    #[doc(hidden)]
36    fn discard(&mut self);
37
38    /// Position of the most recent call to next().
39    ///
40    /// The most recent call was probably next() and not peek(), but this method
41    /// should try to return a sensible result if the most recent call was
42    /// actually peek() because we don't always know.
43    ///
44    /// Only called in case of an error, so performance is not important.
45    #[doc(hidden)]
46    fn position(&self) -> Position;
47
48    /// Position of the most recent call to peek().
49    ///
50    /// The most recent call was probably peek() and not next(), but this method
51    /// should try to return a sensible result if the most recent call was
52    /// actually next() because we don't always know.
53    ///
54    /// Only called in case of an error, so performance is not important.
55    #[doc(hidden)]
56    fn peek_position(&self) -> Position;
57
58    /// Offset from the beginning of the input to the next byte that would be
59    /// returned by next() or peek().
60    #[doc(hidden)]
61    fn byte_offset(&self) -> usize;
62
63    fn read_str<'s>(&'s mut self, len: usize) -> Result<&'de str>;
64
65    fn read_slice<'s>(&'s mut self, len: usize) -> Result<&'de [u8]>;
66
67    fn parse_int_any_pos(&mut self) -> Result<u64>;
68
69    // fn parse_int(&mut self) -> Result<ParserNumber>;
70
71    // fn parse_int_any(&mut self) -> Result<ParserNumber>;
72
73    fn str_from_saved(&mut self) -> Result<&'de str>;
74
75    /// Assumes the previous byte was a quotation mark. Parses an EnCom-escaped
76    /// string until the next quotation mark using the given scratch space if
77    /// necessary. The scratch space is initially empty.
78    #[doc(hidden)]
79    fn parse_str<'s>(&'s mut self) -> Result<Reference<'de, 's, str>>;
80
81    // Assumes the previous byte was a quotation mark. Parses an EnCom-escaped
82    // string until the next quotation mark using the given scratch space if
83    // necessary. The scratch space is initially empty.
84    //
85    // This function returns the raw bytes in the string with escape sequences
86    // expanded but without performing unicode validation.
87    //#[doc(hidden)]
88    //fn parse_str_raw<'s>(&'s mut self) -> Result<Reference<'de, 's, [u8]>>;
89
90    /// Assumes the previous byte was a quotation mark. Parses an EnCom-escaped
91    /// string until the next quotation mark but discards the data.
92    #[doc(hidden)]
93    fn ignore_str(&mut self) -> Result<()>;
94
95    /// Assumes the previous byte was a hex escape sequnce ('\u') in a string.
96    /// Parses next hexadecimal sequence.
97    #[doc(hidden)]
98    fn decode_hex_escape(&mut self) -> Result<u16>;
99
100    /// Switch raw buffering mode on.
101    ///
102    /// This is used when deserializing `RawValue`.
103    #[cfg(feature = "raw_value")]
104    #[doc(hidden)]
105    fn begin_raw_buffering(&mut self);
106
107    /// Switch raw buffering mode off and provides the raw buffered data to the
108    /// given visitor.
109    #[cfg(feature = "raw_value")]
110    #[doc(hidden)]
111    fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
112    where
113        V: Visitor<'de>;
114
115    /// Whether StreamDeserializer::next needs to check the failed flag. True
116    /// for IoRead, false for StrRead and SliceRead which can track failure by
117    /// truncating their input slice to avoid the extra check on every next
118    /// call.
119    #[doc(hidden)]
120    const SHOULD_EARLY_RETURN_IF_FAILED: bool;
121
122    /// Mark a persistent failure of StreamDeserializer, either by setting the
123    /// flag or by truncating the input data.
124    #[doc(hidden)]
125    fn set_failed(&mut self, failed: &mut bool);
126
127    fn save_start(&mut self);
128    fn save_end(&mut self);
129    fn clear_saved(&mut self);
130    fn get_saved(&mut self) -> &'de [u8];
131    fn saved_is_empty(&self) -> bool;
132}
133
134pub struct Position {
135    pub line: usize,
136    pub column: usize,
137}
138
139pub enum Reference<'b, 'c, T>
140where
141    T: ?Sized + 'static,
142{
143    Borrowed(&'b T),
144    Copied(&'c T),
145}
146
147impl<'b, 'c, T> Deref for Reference<'b, 'c, T>
148where
149    T: ?Sized + 'static,
150{
151    type Target = T;
152
153    fn deref(&self) -> &Self::Target {
154        match *self {
155            Reference::Borrowed(b) => b,
156            Reference::Copied(c) => c,
157        }
158    }
159}
160
161/// EnCom input source that reads from a std::io input stream.
162#[cfg(feature = "std")]
163#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
164pub struct IoRead<R>
165where
166    R: io::Read,
167{
168    iter: LineColIterator<io::Bytes<R>>,
169    /// Temporary storage of peeked byte.
170    ch: Option<u8>,
171    #[cfg(feature = "raw_value")]
172    raw_buffer: Option<Vec<u8>>,
173}
174
175/// EnCom input source that reads from a slice of bytes.
176//
177// This is more efficient than other iterators because peek() can be read-only
178// and we can compute line/col position only if an error happens.
179pub struct SliceRead<'a> {
180    slice: &'a [u8],
181    /// Index of the *next* byte that will be returned by next() or peek().
182    index: usize,
183    #[cfg(feature = "raw_value")]
184    raw_buffering_start_index: usize,
185    save_start: usize,
186    save_end: usize,
187}
188
189/// EnCom input source that reads from a UTF-8 string.
190//
191// Able to elide UTF-8 checks by assuming that the input is valid UTF-8.
192pub struct StrRead<'a> {
193    delegate: SliceRead<'a>,
194    #[cfg(feature = "raw_value")]
195    data: &'a str,
196}
197
198// Prevent users from implementing the Read trait.
199mod private {
200    pub trait Sealed {}
201}
202
203//////////////////////////////////////////////////////////////////////////////
204
205#[cfg(feature = "std")]
206impl<R> IoRead<R>
207where
208    R: io::Read,
209{
210    /// Create an EnCom input source to read from a std::io input stream.
211    pub fn new(reader: R) -> Self {
212        IoRead {
213            iter: LineColIterator::new(reader.bytes()),
214            ch: None,
215            #[cfg(feature = "raw_value")]
216            raw_buffer: None,
217        }
218    }
219}
220
221#[cfg(feature = "std")]
222impl<R> private::Sealed for IoRead<R> where R: io::Read {}
223
224#[cfg(feature = "std")]
225impl<R> IoRead<R>
226where
227    R: io::Read,
228{
229    fn parse_str_bytes<'s, T, F>(&'s mut self, validate: bool, result: F) -> Result<T>
230    where
231        T: 's,
232        F: FnOnce(&'s Self, &'s [u8]) -> Result<T>,
233    {
234        unimplemented!()
235        /* loop {
236            let ch = next_or_eof(self)?;
237            if !ESCAPE[ch as usize] {
238                scratch.push(ch);
239                continue;
240            }
241            match ch {
242                b' ' | b'\n' | b'\t' | b'\r' => {
243                    return result(self, scratch);
244                }
245                /* b'\\' => {
246                    parse_escape(self, validate, scratch)?;
247                } */
248                _ => {
249                    if validate {
250                        return error(self, ErrorCode::ControlCharacterWhileParsingString);
251                    }
252                    scratch.push(ch);
253                }
254            }
255        } */
256    }
257}
258
259#[cfg(feature = "std")]
260impl<'de, R> Read<'de> for IoRead<R>
261where
262    R: io::Read,
263{
264    #[inline]
265    fn next(&mut self) -> Result<Option<u8>> {
266        match self.ch.take() {
267            Some(ch) => {
268                #[cfg(feature = "raw_value")]
269                {
270                    if let Some(buf) = &mut self.raw_buffer {
271                        buf.push(ch);
272                    }
273                }
274                Ok(Some(ch))
275            }
276            None => match self.iter.next() {
277                Some(Err(err)) => Err(Error::io(err)),
278                Some(Ok(ch)) => {
279                    #[cfg(feature = "raw_value")]
280                    {
281                        if let Some(buf) = &mut self.raw_buffer {
282                            buf.push(ch);
283                        }
284                    }
285                    Ok(Some(ch))
286                }
287                None => Ok(None),
288            },
289        }
290    }
291
292    #[inline]
293    fn peek(&mut self) -> Result<Option<u8>> {
294        match self.ch {
295            Some(ch) => Ok(Some(ch)),
296            None => match self.iter.next() {
297                Some(Err(err)) => Err(Error::io(err)),
298                Some(Ok(ch)) => {
299                    self.ch = Some(ch);
300                    Ok(self.ch)
301                }
302                None => Ok(None),
303            },
304        }
305    }
306
307    #[cfg(not(feature = "raw_value"))]
308    #[inline]
309    fn discard(&mut self) {
310        self.ch = None;
311    }
312
313    #[cfg(feature = "raw_value")]
314    fn discard(&mut self) {
315        if let Some(ch) = self.ch.take() {
316            if let Some(buf) = &mut self.raw_buffer {
317                buf.push(ch);
318            }
319        }
320    }
321
322    fn position(&self) -> Position {
323        Position {
324            line: self.iter.line(),
325            column: self.iter.col(),
326        }
327    }
328
329    #[inline]
330    fn peek_position(&self) -> Position {
331        // The LineColIterator updates its position during peek() so it has the
332        // right one here.
333        self.position()
334    }
335
336    #[inline]
337    fn byte_offset(&self) -> usize {
338        match self.ch {
339            Some(_) => self.iter.byte_offset() - 1,
340            None => self.iter.byte_offset(),
341        }
342    }
343
344    #[inline]
345    fn read_str<'s>(&'s mut self, _len: usize) -> Result<&'de str> {
346        unimplemented!()
347    }
348
349    #[inline]
350    fn read_slice<'s>(&'s mut self, _len: usize) -> Result<&'de [u8]> {
351        unimplemented!()
352    }
353
354    #[inline]
355    fn parse_int_any_pos(&mut self) -> Result<u64> {
356        unimplemented!()
357    }
358
359    /* #[inline]
360    fn parse_int(&mut self) -> Result<ParserNumber> {
361        unimplemented!()
362    }
363
364    #[inline]
365    fn parse_int_any(&mut self) -> Result<ParserNumber> {
366        unimplemented!()
367    } */
368
369    #[inline]
370    fn str_from_saved(&mut self) -> Result<&'de str> {
371        let saved = self.get_saved();
372        as_str(self, saved)
373    }
374
375    #[inline]
376    fn parse_str<'s>(&'s mut self) -> Result<Reference<'de, 's, str>> {
377        self.parse_str_bytes(true, as_str).map(Reference::Copied)
378    }
379
380    /* #[inline]
381    fn parse_str_raw<'s>(&'s mut self) -> Result<Reference<'de, 's, [u8]>> {
382        self.parse_str_bytes(false, |_, bytes| Ok(bytes))
383            .map(Reference::Copied)
384    } */
385
386    fn ignore_str(&mut self) -> Result<()> {
387        loop {
388            let ch = next_or_eof(self)?;
389            if !ESCAPE[ch as usize] {
390                continue;
391            }
392            match ch {
393                b' ' | b'\n' | b'\t' | b'\r' => {
394                    return Ok(());
395                }
396                /* b'\\' => {
397                    ignore_escape(self)?;
398                } */
399                _ => {
400                    return error(self, ErrorCode::ControlCharacterWhileParsingString);
401                }
402            }
403        }
404    }
405
406    fn decode_hex_escape(&mut self) -> Result<u16> {
407        let mut n = 0;
408        for _ in 0..4 {
409            match decode_hex_val(next_or_eof(self)?) {
410                None => return error(self, ErrorCode::InvalidEscape),
411                Some(val) => {
412                    n = (n << 4) + val;
413                }
414            }
415        }
416        Ok(n)
417    }
418
419    #[cfg(feature = "raw_value")]
420    fn begin_raw_buffering(&mut self) {
421        self.raw_buffer = Some(Vec::new());
422    }
423
424    #[cfg(feature = "raw_value")]
425    fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
426    where
427        V: Visitor<'de>,
428    {
429        let raw = self.raw_buffer.take().unwrap();
430        let raw = match String::from_utf8(raw) {
431            Ok(raw) => raw,
432            Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
433        };
434        visitor.visit_map(OwnedRawDeserializer {
435            raw_value: Some(raw),
436        })
437    }
438
439    const SHOULD_EARLY_RETURN_IF_FAILED: bool = true;
440
441    #[inline]
442    #[cold]
443    fn set_failed(&mut self, failed: &mut bool) {
444        *failed = true;
445    }
446
447    #[inline]
448    fn save_start(&mut self) {
449        unimplemented!()
450    }
451    #[inline]
452    fn save_end(&mut self) {
453        unimplemented!()
454    }
455    #[inline]
456    fn clear_saved(&mut self) {
457        unimplemented!()
458    }
459    #[inline]
460    fn get_saved(&mut self) -> &'de [u8] {
461        unimplemented!()
462    }
463    #[inline]
464    fn saved_is_empty(&self) -> bool {
465        unimplemented!()
466    }
467}
468
469//////////////////////////////////////////////////////////////////////////////
470
471impl<'de> SliceRead<'de> {
472    /// Create an EnCom input source to read from a slice of bytes.
473    pub fn new(slice: &'de [u8]) -> Self {
474        SliceRead {
475            slice,
476            index: 0,
477            #[cfg(feature = "raw_value")]
478            raw_buffering_start_index: 0,
479            save_start: 0,
480            save_end: 0,
481        }
482    }
483
484    fn position_of_index(&self, i: usize) -> Position {
485        let mut position = Position { line: 1, column: 0 };
486        for ch in &self.slice[..i] {
487            match *ch {
488                b'\n' => {
489                    position.line += 1;
490                    position.column = 0;
491                }
492                _ => {
493                    position.column += 1;
494                }
495            }
496        }
497        position
498    }
499
500    /// The big optimization here over IoRead is that if the string contains no
501    /// backslash escape sequences, the returned &str is a slice of the raw EnCom
502    /// data so we avoid copying into the scratch space.
503    fn parse_str_bytes<'s, T, F>(
504        &'s mut self,
505        _validate: bool,
506        result: F,
507    ) -> Result<Reference<'de, 's, T>>
508    where
509        T: ?Sized + 's,
510        F: FnOnce(&'s Self, &'de [u8]) -> Result<&'de T>,
511    {
512        // Index of the first byte not yet copied into the scratch space.
513        let start = self.index;
514
515        loop {
516            /* while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
517                self.index += 1;
518            } */
519            if self.index == self.slice.len() {
520                return error(self, ErrorCode::EofWhileParsingString);
521            }
522            match self.slice[self.index] {
523                b':' | b'{' | b'[' => {
524                    // if scratch.is_empty() {
525                    // Fast path: return a slice of the raw EnCom without any
526                    // copying.
527                    let borrowed = &self.slice[start..self.index];
528                    // self.index += 1;
529                    return result(self, borrowed).map(Reference::Borrowed);
530                    /* } else {
531                        scratch.extend_from_slice(&self.slice[start..self.index]);
532                        self.index += 1;
533                        return result(self, scratch).map(Reference::Copied);
534                    } */
535                }
536                /* b'\\' => {
537                    scratch.extend_from_slice(&self.slice[start..self.index]);
538                    self.index += 1;
539                    parse_escape(self, validate, scratch)?;
540                    start = self.index;
541                } */
542                _ => {
543                    self.index += 1;
544                    /* if validate {
545                        return error(self, ErrorCode::ControlCharacterWhileParsingString);
546                    } */
547                }
548            }
549        }
550    }
551}
552
553impl<'de> private::Sealed for SliceRead<'de> {}
554
555impl<'de> Read<'de> for SliceRead<'de> {
556    #[inline]
557    fn next(&mut self) -> Result<Option<u8>> {
558        // `Ok(self.slice.get(self.index).map(|ch| { self.index += 1; *ch }))`
559        // is about 10% slower.
560        Ok(if self.index < self.slice.len() {
561            let ch = self.slice[self.index];
562            self.index += 1;
563            Some(ch)
564        } else {
565            None
566        })
567    }
568
569    #[inline]
570    fn peek(&mut self) -> Result<Option<u8>> {
571        // `Ok(self.slice.get(self.index).map(|ch| *ch))` is about 10% slower
572        // for some reason.
573        Ok(if self.index < self.slice.len() {
574            Some(self.slice[self.index])
575        } else {
576            None
577        })
578    }
579
580    #[inline]
581    fn discard(&mut self) {
582        self.index += 1;
583    }
584
585    #[inline]
586    fn position(&self) -> Position {
587        self.position_of_index(self.index)
588    }
589
590    #[inline]
591    fn peek_position(&self) -> Position {
592        // Cap it at slice.len() just in case the most recent call was next()
593        // and it returned the last byte.
594        self.position_of_index(cmp::min(self.slice.len(), self.index + 1))
595    }
596
597    #[inline]
598    fn byte_offset(&self) -> usize {
599        self.index
600    }
601
602    #[inline]
603    fn read_str<'s>(&'s mut self, len: usize) -> Result<&'de str> {
604        let start = self.index;
605        self.index += len;
606        as_str(self, &self.slice[start..self.index])
607    }
608
609    #[inline]
610    fn read_slice<'s>(&'s mut self, len: usize) -> Result<&'de [u8]> {
611        let start = self.index;
612        self.index += len;
613        Ok(&self.slice[start..self.index])
614    }
615
616    #[inline]
617    fn parse_int_any_pos(&mut self) -> Result<u64> {
618        let (res, i) = atoi_simd::parse_any_pos(self.slice.get_safe_unchecked(self.index..))?;
619        self.index += i;
620        Ok(res)
621    }
622
623    /* #[inline]
624    fn parse_int(&mut self) -> Result<ParserNumber> {
625        let res = if *self.slice.first().ok_or(AtoiSimdError::Empty)? == b'-' {
626            self.index += 1;
627            ParserNumber::I64(atoi_simd::parse_neg(
628                &self.slice.get_safe_unchecked(self.index..),
629            )?)
630        } else {
631            ParserNumber::U64(atoi_simd::parse_pos(&self.slice[self.index..])?)
632        };
633        self.index = self.slice.len();
634        Ok(res)
635    }
636
637    #[inline]
638    fn parse_int_any(&mut self) -> Result<ParserNumber> {
639        let (res, i) = if *self.slice.first().ok_or(AtoiSimdError::Empty)? == b'-' {
640            self.index += 1;
641            let (v, l) =
642                atoi_simd::parse_any_neg(&self.slice.get_safe_unchecked(self.index..))?;
643            (ParserNumber::I64(v), l)
644        } else {
645            let (v, l) = atoi_simd::parse_any_pos(&self.slice[self.index..])?;
646            (ParserNumber::U64(v), l)
647        };
648        self.index += i;
649        Ok(res)
650    } */
651
652    #[inline]
653    fn str_from_saved(&mut self) -> Result<&'de str> {
654        let saved = self.get_saved();
655        as_str(self, saved)
656    }
657
658    #[inline]
659    fn parse_str<'s>(&'s mut self) -> Result<Reference<'de, 's, str>> {
660        self.parse_str_bytes(true, as_str)
661    }
662
663    /* #[inline]
664    fn parse_str_raw<'s>(&'s mut self) -> Result<Reference<'de, 's, [u8]>> {
665        self.parse_str_bytes(false, |_, bytes| Ok(bytes))
666    } */
667
668    fn ignore_str(&mut self) -> Result<()> {
669        // loop {
670        while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
671            self.index += 1;
672        }
673        if self.index == self.slice.len() {
674            return error(self, ErrorCode::EofWhileParsingString);
675        }
676        match self.slice[self.index] {
677            b' ' | b'\n' | b'\t' | b'\r' => {
678                self.index += 1;
679                Ok(())
680            }
681            /* b'\\' => {
682                self.index += 1;
683                ignore_escape(self)?;
684            } */
685            _ => error(self, ErrorCode::ControlCharacterWhileParsingString),
686        }
687        // }
688    }
689
690    fn decode_hex_escape(&mut self) -> Result<u16> {
691        if self.index + 4 > self.slice.len() {
692            self.index = self.slice.len();
693            return error(self, ErrorCode::EofWhileParsingString);
694        }
695
696        let mut n = 0;
697        for _ in 0..4 {
698            let ch = decode_hex_val(self.slice[self.index]);
699            self.index += 1;
700            match ch {
701                None => return error(self, ErrorCode::InvalidEscape),
702                Some(val) => {
703                    n = (n << 4) + val;
704                }
705            }
706        }
707        Ok(n)
708    }
709
710    #[cfg(feature = "raw_value")]
711    fn begin_raw_buffering(&mut self) {
712        self.raw_buffering_start_index = self.index;
713    }
714
715    #[cfg(feature = "raw_value")]
716    fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
717    where
718        V: Visitor<'de>,
719    {
720        let raw = &self.slice[self.raw_buffering_start_index..self.index];
721        let raw = match str::from_utf8(raw) {
722            Ok(raw) => raw,
723            Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
724        };
725        visitor.visit_map(BorrowedRawDeserializer {
726            raw_value: Some(raw),
727        })
728    }
729
730    const SHOULD_EARLY_RETURN_IF_FAILED: bool = false;
731
732    #[inline]
733    #[cold]
734    fn set_failed(&mut self, _failed: &mut bool) {
735        self.slice = &self.slice[..self.index];
736    }
737
738    #[inline]
739    fn save_start(&mut self) {
740        self.save_start = self.index;
741    }
742    #[inline]
743    fn save_end(&mut self) {
744        self.save_end = self.index;
745    }
746    #[inline]
747    fn clear_saved(&mut self) {
748        self.save_end = self.save_start;
749    }
750    #[inline]
751    fn get_saved(&mut self) -> &'de [u8] {
752        &self.slice[self.save_start..self.save_end]
753    }
754    #[inline]
755    fn saved_is_empty(&self) -> bool {
756        self.save_end == self.save_start
757    }
758}
759
760//////////////////////////////////////////////////////////////////////////////
761
762impl<'a> StrRead<'a> {
763    /// Create an EnCom input source to read from a UTF-8 string.
764    pub fn new(s: &'a str) -> Self {
765        StrRead {
766            delegate: SliceRead::new(s.as_bytes()),
767            #[cfg(feature = "raw_value")]
768            data: s,
769        }
770    }
771}
772
773impl<'de> private::Sealed for StrRead<'de> {}
774
775impl<'de> Read<'de> for StrRead<'de> {
776    #[inline]
777    fn next(&mut self) -> Result<Option<u8>> {
778        self.delegate.next()
779    }
780
781    #[inline]
782    fn peek(&mut self) -> Result<Option<u8>> {
783        self.delegate.peek()
784    }
785
786    #[inline]
787    fn discard(&mut self) {
788        self.delegate.discard();
789    }
790
791    #[inline]
792    fn position(&self) -> Position {
793        self.delegate.position()
794    }
795
796    #[inline]
797    fn peek_position(&self) -> Position {
798        self.delegate.peek_position()
799    }
800
801    #[inline]
802    fn byte_offset(&self) -> usize {
803        self.delegate.byte_offset()
804    }
805
806    #[inline]
807    fn read_str<'s>(&'s mut self, len: usize) -> Result<&'de str> {
808        self.delegate.read_str(len)
809    }
810
811    #[inline]
812    fn read_slice<'s>(&'s mut self, len: usize) -> Result<&'de [u8]> {
813        self.delegate.read_slice(len)
814    }
815
816    #[inline]
817    fn parse_int_any_pos(&mut self) -> Result<u64> {
818        self.delegate.parse_int_any_pos()
819    }
820
821    /* #[inline]
822    fn parse_int(&mut self) -> Result<ParserNumber> {
823        self.delegate.parse_int()
824    }
825
826    #[inline]
827    fn parse_int_any(&mut self) -> Result<ParserNumber> {
828        self.delegate.parse_int_any()
829    } */
830
831    #[inline]
832    fn str_from_saved(&mut self) -> Result<&'de str> {
833        unsafe { Ok(str::from_utf8_unchecked(self.get_saved())) }
834    }
835
836    #[inline]
837    fn parse_str<'s>(&'s mut self) -> Result<Reference<'de, 's, str>> {
838        self.delegate.parse_str_bytes(true, |_, bytes| {
839            // The deserialization input came in as &str with a UTF-8 guarantee,
840            // and the \u-escapes are checked along the way, so don't need to
841            // check here.
842            Ok(unsafe { str::from_utf8_unchecked(bytes) })
843        })
844    }
845
846    /* fn parse_str_raw<'s>(&'s mut self) -> Result<Reference<'de, 's, [u8]>> {
847        self.delegate.parse_str_raw()
848    } */
849
850    #[inline]
851    fn ignore_str(&mut self) -> Result<()> {
852        self.delegate.ignore_str()
853    }
854
855    #[inline]
856    fn decode_hex_escape(&mut self) -> Result<u16> {
857        self.delegate.decode_hex_escape()
858    }
859
860    #[cfg(feature = "raw_value")]
861    #[inline]
862    fn begin_raw_buffering(&mut self) {
863        self.delegate.begin_raw_buffering();
864    }
865
866    #[cfg(feature = "raw_value")]
867    fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
868    where
869        V: Visitor<'de>,
870    {
871        let raw = &self.data[self.delegate.raw_buffering_start_index..self.delegate.index];
872        visitor.visit_map(BorrowedRawDeserializer {
873            raw_value: Some(raw),
874        })
875    }
876
877    const SHOULD_EARLY_RETURN_IF_FAILED: bool = false;
878
879    #[inline]
880    #[cold]
881    fn set_failed(&mut self, failed: &mut bool) {
882        self.delegate.set_failed(failed);
883    }
884
885    #[inline]
886    fn save_start(&mut self) {
887        self.delegate.save_start()
888    }
889    #[inline]
890    fn save_end(&mut self) {
891        self.delegate.save_end()
892    }
893    #[inline]
894    fn clear_saved(&mut self) {
895        self.delegate.clear_saved()
896    }
897    #[inline]
898    fn get_saved(&mut self) -> &'de [u8] {
899        self.delegate.get_saved()
900    }
901    #[inline]
902    fn saved_is_empty(&self) -> bool {
903        self.delegate.saved_is_empty()
904    }
905}
906
907//////////////////////////////////////////////////////////////////////////////
908
909impl<'de, R> private::Sealed for &mut R where R: Read<'de> {}
910
911impl<'de, R> Read<'de> for &mut R
912where
913    R: Read<'de>,
914{
915    #[inline]
916    fn next(&mut self) -> Result<Option<u8>> {
917        R::next(self)
918    }
919
920    #[inline]
921    fn peek(&mut self) -> Result<Option<u8>> {
922        R::peek(self)
923    }
924
925    #[inline]
926    fn discard(&mut self) {
927        R::discard(self);
928    }
929
930    #[inline]
931    fn position(&self) -> Position {
932        R::position(self)
933    }
934
935    #[inline]
936    fn peek_position(&self) -> Position {
937        R::peek_position(self)
938    }
939
940    #[inline]
941    fn byte_offset(&self) -> usize {
942        R::byte_offset(self)
943    }
944
945    #[inline]
946    fn read_str<'s>(&'s mut self, len: usize) -> Result<&'de str> {
947        R::read_str(self, len)
948    }
949
950    #[inline]
951    fn read_slice<'s>(&'s mut self, len: usize) -> Result<&'de [u8]> {
952        R::read_slice(self, len)
953    }
954
955    #[inline]
956    fn parse_int_any_pos(&mut self) -> Result<u64> {
957        R::parse_int_any_pos(self)
958    }
959
960    /* #[inline]
961    fn parse_int(&mut self) -> Result<ParserNumber> {
962        R::parse_int(self)
963    }
964
965    #[inline]
966    fn parse_int_any(&mut self) -> Result<ParserNumber> {
967        R::parse_int_any(self)
968    } */
969
970    #[inline]
971    fn str_from_saved(&mut self) -> Result<&'de str> {
972        R::str_from_saved(self)
973    }
974
975    #[inline]
976    fn parse_str<'s>(&'s mut self) -> Result<Reference<'de, 's, str>> {
977        R::parse_str(self)
978    }
979
980    /* #[inline]
981    fn parse_str_raw<'s>(&'s mut self) -> Result<Reference<'de, 's, [u8]>> {
982        R::parse_str_raw(self)
983    } */
984
985    #[inline]
986    fn ignore_str(&mut self) -> Result<()> {
987        R::ignore_str(self)
988    }
989
990    #[inline]
991    fn decode_hex_escape(&mut self) -> Result<u16> {
992        R::decode_hex_escape(self)
993    }
994
995    #[cfg(feature = "raw_value")]
996    #[inline]
997    fn begin_raw_buffering(&mut self) {
998        R::begin_raw_buffering(self);
999    }
1000
1001    #[cfg(feature = "raw_value")]
1002    #[inline]
1003    fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
1004    where
1005        V: Visitor<'de>,
1006    {
1007        R::end_raw_buffering(self, visitor)
1008    }
1009
1010    const SHOULD_EARLY_RETURN_IF_FAILED: bool = R::SHOULD_EARLY_RETURN_IF_FAILED;
1011
1012    #[inline]
1013    fn set_failed(&mut self, failed: &mut bool) {
1014        R::set_failed(self, failed);
1015    }
1016
1017    #[inline]
1018    fn save_start(&mut self) {
1019        R::save_start(self)
1020    }
1021    #[inline]
1022    fn save_end(&mut self) {
1023        R::save_end(self)
1024    }
1025    #[inline]
1026    fn clear_saved(&mut self) {
1027        R::clear_saved(self)
1028    }
1029    #[inline]
1030    fn get_saved(&mut self) -> &'de [u8] {
1031        R::get_saved(self)
1032    }
1033    #[inline]
1034    fn saved_is_empty(&self) -> bool {
1035        R::saved_is_empty(self)
1036    }
1037}
1038
1039//////////////////////////////////////////////////////////////////////////////
1040
1041/// Marker for whether StreamDeserializer can implement FusedIterator.
1042pub trait Fused: private::Sealed {}
1043impl<'a> Fused for SliceRead<'a> {}
1044impl<'a> Fused for StrRead<'a> {}
1045
1046// Lookup table of bytes that must be escaped. A value of true at index i means
1047// that byte i requires an escape sequence in the input.
1048static ESCAPE: [bool; 256] = {
1049    const CT: bool = true; // control character \x00..=\x1F
1050    const QU: bool = true; // quote \x22
1051    const BS: bool = true; // backslash \x5C
1052    const __: bool = false; // allow unescaped
1053    [
1054        //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
1055        CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 0
1056        CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 1
1057        __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
1058        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3
1059        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4
1060        __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5
1061        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6
1062        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
1063        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
1064        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
1065        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
1066        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
1067        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
1068        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
1069        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
1070        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
1071    ]
1072};
1073
1074#[inline]
1075fn next_or_eof<'de, R>(read: &mut R) -> Result<u8>
1076where
1077    R: ?Sized + Read<'de>,
1078{
1079    match read.next()? {
1080        Some(b) => Ok(b),
1081        None => error(read, ErrorCode::EofWhileParsingString),
1082    }
1083}
1084
1085/* #[inline]
1086fn peek_or_eof<'de, R>(read: &mut R) -> Result<u8>
1087where
1088    R: ?Sized + Read<'de>,
1089{
1090    match read.peek()? {
1091        Some(b) => Ok(b),
1092        None => error(read, ErrorCode::EofWhileParsingString),
1093    }
1094} */
1095
1096fn error<'de, R, T>(read: &R, reason: ErrorCode) -> Result<T>
1097where
1098    R: ?Sized + Read<'de>,
1099{
1100    let position = read.position();
1101    Err(Error::syntax(reason, position.line, position.column))
1102}
1103
1104#[inline]
1105fn as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str> {
1106    str::from_utf8(slice).or_else(|_| error(read, ErrorCode::InvalidUnicodeCodePoint))
1107}
1108
1109/// Parses an EnCom escape sequence and appends it into the scratch space. Assumes
1110/// the previous byte read was a backslash.
1111/* fn parse_escape<'de, R: Read<'de>>(
1112    read: &mut R,
1113    validate: bool,
1114    scratch: &mut Vec<u8>,
1115) -> Result<()> {
1116    let ch = next_or_eof(read)?;
1117
1118    match ch {
1119        b'"' => scratch.push(b'"'),
1120        b'\\' => scratch.push(b'\\'),
1121        b'/' => scratch.push(b'/'),
1122        b'b' => scratch.push(b'\x08'),
1123        b'f' => scratch.push(b'\x0c'),
1124        b'n' => scratch.push(b'\n'),
1125        b'r' => scratch.push(b'\r'),
1126        b't' => scratch.push(b'\t'),
1127        b'u' => {
1128            fn encode_surrogate(scratch: &mut Vec<u8>, n: u16) {
1129                scratch.extend_from_slice(&[
1130                    (n >> 12 & 0b0000_1111) as u8 | 0b1110_0000,
1131                    (n >> 6 & 0b0011_1111) as u8 | 0b1000_0000,
1132                    (n & 0b0011_1111) as u8 | 0b1000_0000,
1133                ]);
1134            }
1135
1136            let c = match read.decode_hex_escape()? {
1137                n @ 0xDC00..=0xDFFF => {
1138                    return if validate {
1139                        error(read, ErrorCode::LoneLeadingSurrogateInHexEscape)
1140                    } else {
1141                        encode_surrogate(scratch, n);
1142                        Ok(())
1143                    };
1144                }
1145
1146                // Non-BMP characters are encoded as a sequence of two hex
1147                // escapes, representing UTF-16 surrogates. If deserializing a
1148                // utf-8 string the surrogates are required to be paired,
1149                // whereas deserializing a byte string accepts lone surrogates.
1150                n1 @ 0xD800..=0xDBFF => {
1151                    if peek_or_eof(read)? == b'\\' {
1152                        read.discard();
1153                    } else {
1154                        return if validate {
1155                            read.discard();
1156                            error(read, ErrorCode::UnexpectedEndOfHexEscape)
1157                        } else {
1158                            encode_surrogate(scratch, n1);
1159                            Ok(())
1160                        };
1161                    }
1162
1163                    if peek_or_eof(read)? == b'u' {
1164                        read.discard();
1165                    } else {
1166                        return if validate {
1167                            read.discard();
1168                            error(read, ErrorCode::UnexpectedEndOfHexEscape)
1169                        } else {
1170                            encode_surrogate(scratch, n1);
1171                            // The \ prior to this byte started an escape sequence,
1172                            // so we need to parse that now. This recursive call
1173                            // does not blow the stack on malicious input because
1174                            // the escape is not \u, so it will be handled by one
1175                            // of the easy nonrecursive cases.
1176                            parse_escape(read, validate, scratch)
1177                        };
1178                    }
1179
1180                    let n2 = read.decode_hex_escape()?;
1181
1182                    if n2 < 0xDC00 || n2 > 0xDFFF {
1183                        return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
1184                    }
1185
1186                    let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
1187
1188                    match char::from_u32(n) {
1189                        Some(c) => c,
1190                        None => {
1191                            return error(read, ErrorCode::InvalidUnicodeCodePoint);
1192                        }
1193                    }
1194                }
1195
1196                // Every u16 outside of the surrogate ranges above is guaranteed
1197                // to be a legal char.
1198                n => char::from_u32(n as u32).unwrap(),
1199            };
1200
1201            scratch.extend_from_slice(c.encode_utf8(&mut [0_u8; 4]).as_bytes());
1202        }
1203        _ => {
1204            return error(read, ErrorCode::InvalidEscape);
1205        }
1206    }
1207
1208    Ok(())
1209} */
1210
1211/// Parses an EnCom escape sequence and discards the value. Assumes the previous
1212/// byte read was a backslash.
1213/* fn ignore_escape<'de, R>(read: &mut R) -> Result<()>
1214where
1215    R: ?Sized + Read<'de>,
1216{
1217    let ch = next_or_eof(read)?;
1218
1219    match ch {
1220        b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => {}
1221        b'u' => {
1222            // At this point we don't care if the codepoint is valid. We just
1223            // want to consume it. We don't actually know what is valid or not
1224            // at this point, because that depends on if this string will
1225            // ultimately be parsed into a string or a byte buffer in the "real"
1226            // parse.
1227
1228            read.decode_hex_escape()?;
1229        }
1230        _ => {
1231            return error(read, ErrorCode::InvalidEscape);
1232        }
1233    }
1234
1235    Ok(())
1236} */
1237
1238static HEX: [u8; 256] = {
1239    const __: u8 = 255; // not a hex digit
1240    [
1241        //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
1242        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 0
1243        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 1
1244        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
1245        00, 01, 02, 03, 04, 05, 06, 07, 08, 09, __, __, __, __, __, __, // 3
1246        __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, // 4
1247        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 5
1248        __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, // 6
1249        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
1250        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
1251        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
1252        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
1253        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
1254        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
1255        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
1256        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
1257        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
1258    ]
1259};
1260
1261fn decode_hex_val(val: u8) -> Option<u16> {
1262    let n = HEX[val as usize] as u16;
1263    if n == 255 {
1264        None
1265    } else {
1266        Some(n)
1267    }
1268}