1 //! A Hjson deserializer.
2 //!
3 use {
4     crate::{
5         de_enum::*,
6         de_map::*,
7         de_number::*,
8         de_seq::*,
9         error::{
10             Error,
11             ErrorCode::{self, *},
12             Result,
13         },
14     },
15     serde::de::{self, IntoDeserializer, Visitor},
16 };
17 
18 /// The deserializer. You normally don't call it directly
19 /// but use the `from_str` function available at crate's level.
20 pub struct Deserializer<'de> {
21     // the complete string we received
22     src: &'de str,
23 
24     // where we're at
25     pos: usize,
26 
27     // Make it possible to avoid reading a string as a quoteless
28     // string when a key map is waited for (for example in
29     //     {
30     //         key: value
31     //     }
32     // ) so that the key doesn't go til the end of the line.
33     pub(crate) accept_quoteless_value: bool,
34 }
35 
36 impl<'de> Deserializer<'de> {
37 
from_str(src: &'de str) -> Self38     pub fn from_str(src: &'de str) -> Self {
39         Deserializer {
40             src,
41             pos: 0,
42             accept_quoteless_value: true,
43         }
44     }
45 
46     /// compute the number of lines and columns to current pos
location(&self) -> (usize, usize)47     fn location(&self) -> (usize, usize) {
48         let (mut line, mut col) = (1, 1);
49         for ch in self.src[..self.pos].chars() {
50             if ch == '\n' {
51                 col = 1;
52                 line += 1;
53             } else {
54                 col += 1;
55             }
56         }
57         (line, col)
58     }
59 
60     /// build a syntax error
err(&self, code: ErrorCode) -> Error61     pub(crate) fn err(&self, code: ErrorCode) -> Error {
62         let (line, col) = self.location();
63         // we'll show the next 15 chars in the error message
64         let at = self.input().chars().take(15).collect();
65         Error::Syntax {
66             line,
67             col,
68             code,
69             at,
70         }
71     }
72 
73     /// convert a serde raised error into one with precise location
cook_err<T>(&self, err: Error) -> Result<T>74     pub(crate) fn cook_err<T>(&self, err: Error) -> Result<T> {
75         match err {
76             Error::RawSerde(message) => {
77                 let (line, col) = self.location();
78                 // we have no real idea where Serde found the problem
79                 // so we write the position but not the characters around
80                 Err(Error::Serde {
81                     line,
82                     col,
83                     message,
84                 })
85             }
86             e => Err(e),
87         }
88     }
89 
fail<T>(&self, code: ErrorCode) -> Result<T>90     pub(crate) fn fail<T>(&self, code: ErrorCode) -> Result<T> {
91         Err(self.err(code))
92     }
93 
94     /// return an error if there's more than just spaces
95     /// and comments in the remaining input
check_all_consumed(&mut self) -> Result<()>96     pub fn check_all_consumed(&mut self) -> Result<()> {
97         self.eat_shit().ok();
98         if self.input().is_empty() {
99             Ok(())
100         } else {
101             self.fail(TrailingCharacters)
102         }
103     }
104 
105     /// what remains to be parsed (including the
106     /// character we peeked at, if any)
107     #[inline(always)]
input(&self) -> &'de str108     pub(crate) fn input(&self) -> &'de str {
109         &self.src[self.pos..]
110     }
111 
112     /// takes all remaining characters
113     #[inline(always)]
take_all(&mut self) -> &'de str114     pub(crate) fn take_all(&mut self) -> &'de str {
115         let s = &self.src[self.pos..];
116         self.pos = self.src.len();
117         s
118     }
119 
120     /// Look at the first character in the input without consuming it.
121     #[inline(always)]
peek_char(&self) -> Result<char>122     pub(crate) fn peek_char(&self) -> Result<char> {
123         match self.input().chars().next() {
124             Some(ch) => Ok(ch),
125             _ => self.fail(Eof),
126         }
127     }
128 
129     /// read bytes_count bytes of a string.
130     /// The validity of pos + bytes_count as a valid UTF8 position must
131     /// have been checked before.
132     #[inline(always)]
take_str(&mut self, bytes_count: usize) -> Result<&str>133     pub(crate) fn take_str(&mut self, bytes_count: usize) -> Result<&str> {
134         if self.src.len() >= self.pos + bytes_count {
135             let pos = self.pos;
136             self.pos += bytes_count;
137             Ok(&self.src[pos..pos + bytes_count])
138         } else {
139             self.fail(Eof)
140         }
141     }
142 
143     /// if the next bytes are s, then advance its length and return true
144     /// otherwise return false.
145     /// We do a comparison with a &[u8] to avoid the risk of trying read
146     /// at arbitrary positions and fall between valid UTF8 positions
147     #[inline(always)]
try_read(&mut self, s: &[u8]) -> bool148     pub(crate) fn try_read(&mut self, s: &[u8]) -> bool {
149         if self.src.len() >= self.pos + s.len() {
150             if &self.src.as_bytes()[self.pos..self.pos + s.len()] == s {
151                 self.pos += s.len();
152                 return true;
153             }
154         }
155         false
156     }
157 
158     /// return the `len` first bytes of the input, without checking anything
159     /// (assuming it has been done) nor consuming anything
160     #[inline(always)]
start(&self, len: usize) -> &'de str161     pub(crate) fn start(&self, len: usize) -> &'de str {
162         &self.src[self.pos..self.pos + len]
163     }
164 
165     /// remove the next character (which is assumed to be ch)
166     #[inline(always)]
drop(&mut self, ch: char)167     pub(crate) fn drop(&mut self, ch: char) {
168         self.advance(ch.len_utf8());
169     }
170 
171     /// advance the cursor (assuming bytes_count is consistent with chars)
172     #[inline(always)]
advance(&mut self, bytes_count: usize)173     pub(crate) fn advance(&mut self, bytes_count: usize) {
174         self.pos += bytes_count;
175     }
176 
177     /// Consume the first character in the input.
178     #[inline(always)]
next_char(&mut self) -> Result<char>179     pub(crate) fn next_char(&mut self) -> Result<char> {
180         let ch = self.peek_char()?;
181         self.drop(ch);
182         Ok(ch)
183     }
184 
185     /// tells whether the next tree bytes are `'''` which
186     /// is the start or end of a multiline string literal in Hjson
187     #[inline(always)]
is_at_triple_quote(&self, offset: usize) -> bool188     pub(crate) fn is_at_triple_quote(&self, offset: usize) -> bool {
189         self.src.len() >= self.pos + offset + 3
190             && &self.src[offset + self.pos..offset + self.pos + 3] == "'''"
191     }
192 
193     #[inline(always)]
eat_line(&mut self) -> Result<()>194     pub(crate) fn eat_line(&mut self) -> Result<()> {
195         self.accept_quoteless_value = true;
196         match self.input().find('\n') {
197             Some(len) => {
198                 self.advance(len + 1);
199                 Ok(())
200             }
201             None => self.fail(Eof),
202         }
203     }
204 
205     #[inline(always)]
eat_until_star_slash(&mut self) -> Result<()>206     pub(crate) fn eat_until_star_slash(&mut self) -> Result<()> {
207         match self.input().find("*/") {
208             Some(len) => {
209                 self.advance(len + 2);
210                 Ok(())
211             }
212             None => self.fail(Eof),
213         }
214     }
215 
216     /// advance until the first non space character and
217     /// return the number of eaten characters in the last
218     /// line
eat_spaces(&mut self) -> Result<usize>219     pub(crate) fn eat_spaces(&mut self) -> Result<usize> {
220         let mut eaten_chars = 0;
221         loop {
222             let ch = self.peek_char()?;
223             if ch == '\n' {
224                 self.accept_quoteless_value = true;
225                 self.drop(ch);
226                 eaten_chars = 0;
227             } else if ch.is_whitespace() {
228                 self.drop(ch);
229                 eaten_chars += 1
230             } else {
231                 return Ok(eaten_chars);
232             }
233         }
234     }
235 
236     #[inline(always)]
eat_shit(&mut self) -> Result<()>237     pub(crate) fn eat_shit(&mut self) -> Result<()> {
238         self.eat_shit_and(None)
239     }
240 
eat_shit_and(&mut self, mut including: Option<char>) -> Result<()>241     pub(crate) fn eat_shit_and(&mut self, mut including: Option<char>) -> Result<()> {
242         let mut last_is_slash = false;
243         loop {
244             let ch = self.peek_char()?;
245             match ch {
246                 '#' => {
247                     self.eat_line()?;
248                     last_is_slash = false;
249                 }
250                 '*' => {
251                     if last_is_slash {
252                         self.eat_until_star_slash()?;
253                     } else {
254                         self.drop(ch);
255                     }
256                     last_is_slash = false;
257                 }
258                 '/' => {
259                     if last_is_slash {
260                         self.eat_line()?;
261                         last_is_slash = false;
262                     } else {
263                         self.drop(ch);
264                         last_is_slash = true;
265                     }
266                 }
267                 '\n' => {
268                     self.accept_quoteless_value = true;
269                     self.drop(ch);
270                     last_is_slash = false;
271                 }
272                 _ if including == Some(ch) => {
273                     self.drop(ch);
274                     including = None;
275                     last_is_slash = false;
276                 }
277                 _ if ch.is_whitespace() => {
278                     self.drop(ch);
279                     last_is_slash = false;
280                 }
281                 _ => {
282                     if last_is_slash {
283                         self.pos -= 1;
284                     }
285                     return Ok(());
286                 }
287             }
288         }
289     }
290 
291     /// Parse the JSON identifier `true` or `false`.
parse_bool(&mut self) -> Result<bool>292     fn parse_bool(&mut self) -> Result<bool> {
293         if self.try_read(b"true") {
294             Ok(true)
295         } else if self.try_read(b"false") {
296             Ok(false)
297         } else {
298             self.fail(ExpectedBoolean)
299         }
300     }
301 
302     /// read the characters of the coming integer, without parsing the
303     /// resulting string
read_integer(&mut self, unsigned: bool) -> Result<&'de str>304     fn read_integer(&mut self, unsigned: bool) -> Result<&'de str> {
305         self.eat_shit()?;
306         for (idx, ch) in self.input().char_indices() {
307             match ch {
308                 '-' if unsigned => {
309                     return self.fail(ExpectedPositiveInteger);
310                 }
311                 '-' if idx > 0 => {
312                     return self.fail(UnexpectedChar);
313                 }
314                 '0'..='9' | '-' => {
315                     // if it's too long, this will be handled at conversion
316                 }
317                 _ => {
318                     let s = self.start(idx);
319                     self.advance(idx); // we keep the last char
320                     return Ok(s);
321                 }
322             }
323         }
324         Ok(self.take_all())
325     }
326 
327     /// read the characters of the coming floating point number, without parsing
read_float(&mut self) -> Result<&'de str>328     fn read_float(&mut self) -> Result<&'de str> {
329         self.eat_shit()?;
330         for (idx, ch) in self.input().char_indices() {
331             match ch {
332                 '0'..='9' | '-' | '+' | '.' | 'e' | 'E' => {
333                     // if it's invalid, this will be handled at conversion
334                 }
335                 _ => {
336                     let s = self.start(idx);
337                     self.advance(idx); // we keep the last char
338                     return Ok(s);
339                 }
340             }
341         }
342         Ok(self.take_all())
343     }
344 
345     /// Parse a string until the next unescaped quote
parse_quoted_string(&mut self) -> Result<String>346     fn parse_quoted_string(&mut self) -> Result<String> {
347         let mut s = String::new();
348 
349         let starting_quote = self.next_char()?;
350 
351         loop {
352             let mut c = self.next_char()?;
353             if c == starting_quote {
354                 break;
355             } else if c == '\\' {
356                 c = match self.next_char()? {
357                     '\"' => '\"',
358                     '\'' => '\'',
359                     '\\' => '\\',
360                     '/' => '/',
361                     'b' => '\x08', // why did they put this in JSON ?
362                     'f' => '\x0c', // and this one ?!
363                     'n' => '\n',
364                     'r' => '\r',
365                     't' => '\t',
366                     'u' => {
367                         self.take_str(4).ok()
368                             .and_then(|s| u32::from_str_radix(s, 16).ok())
369                             .and_then(std::char::from_u32)
370                             .ok_or_else(|| self.err(InvalidEscapeSequence))?
371                     }
372                     _ => {
373                         return self.fail(InvalidEscapeSequence);
374                     }
375                 };
376             }
377             s.push(c);
378         }
379         Ok(s)
380     }
381 
382     /// Parse a string until end of line
parse_quoteless_str(&mut self) -> Result<&'de str>383     fn parse_quoteless_str(&mut self) -> Result<&'de str> {
384         self.eat_shit()?;
385         for (idx, ch) in self.input().char_indices() {
386             if ch == '\r' || ch == '\n' {
387                 let s = self.start(idx);
388                 self.advance(idx + 1);
389                 return Ok(s);
390             }
391         }
392         Ok(self.take_all())
393     }
394 
395     /// Parse a string until the next triple quote.
parse_multiline_string(&mut self) -> Result<String>396     fn parse_multiline_string(&mut self) -> Result<String> {
397         if !self.is_at_triple_quote(0) {
398             // We could probably assume the first three bytes
399             // are "'''" and can be dropped without check
400             return self.fail(ExpectedString);
401         }
402         self.advance(3);
403         self.eat_line()?;
404         // we count the spaces on the first line
405         let indent = self.eat_spaces()?;
406         let mut v = String::new();
407         let mut line_len = indent;
408         for (idx, ch) in self.input().char_indices() {
409             match ch {
410                 '\'' if self.is_at_triple_quote(idx) => {
411                     self.advance(idx + 3);
412                     v.truncate(v.trim_end().len()); // trimming end
413                     return Ok(v);
414                 }
415                 '\r' => {
416                     // a \r not followed by a \n is probably not
417                     // valid but I'm not sure an error would be
418                     // more useful here than silently ignoring it
419                 }
420                 '\n' => {
421                     v.push(ch);
422                     line_len = 0;
423                 }
424                 _ => {
425                     if line_len >= indent || !ch.is_whitespace() {
426                         v.push(ch);
427                     }
428                     line_len += 1;
429                 }
430             }
431         }
432         self.fail(Eof) // it's not legal to not have the triple quotes
433     }
434 
435     /// parse an identifier without quotes:
436     /// - map key
437     /// - enum variant
parse_quoteless_identifier(&mut self) -> Result<&'de str>438     fn parse_quoteless_identifier(&mut self) -> Result<&'de str> {
439         self.eat_shit()?;
440         for (idx, ch) in self.input().char_indices() {
441             match ch {
442                 '"' | ',' | '[' | ']' | '{' | '}' | ':' | '\r'| '\n' => {
443                     let s = self.start(idx);
444                     self.advance(idx);
445                     return Ok(s);
446                 }
447                 ' ' | '\t' => {
448                     let s = self.start(idx);
449                     self.advance(idx + 1);
450                     return Ok(s);
451                 }
452                 _ => {}
453             }
454         }
455         Ok(self.take_all())
456     }
457 
458     /// parse a string which may be a value
459     /// (i.e. not an map key or variant identifier )
parse_string_value(&mut self) -> Result<String>460     fn parse_string_value(&mut self) -> Result<String> {
461         self.eat_shit()?;
462         let ch = self.peek_char()?;
463         let v = match ch {
464             ',' | ':' | '[' | ']' | '{' | '}' => self.fail(UnexpectedChar),
465             '\'' if self.is_at_triple_quote(0) => self.parse_multiline_string(),
466             '"' | '\'' => self.parse_quoted_string(),
467             _ => (if self.accept_quoteless_value {
468                 self.parse_quoteless_str()
469             } else {
470                 self.parse_quoteless_identifier()
471             })
472             .map(|s| s.to_string()),
473         };
474         self.accept_quoteless_value = true;
475         v
476     }
477 
parse_identifier(&mut self) -> Result<String>478     fn parse_identifier(&mut self) -> Result<String> {
479         self.eat_shit()?;
480         let ch = self.peek_char()?;
481         // we set accept_quoteless_value to true so that a quoteless
482         // string can be accepted *after* the current identifier
483         self.accept_quoteless_value = true;
484         let r = match ch {
485             ',' | ':' | '[' | ']' | '{' | '}' => self.fail(UnexpectedChar),
486             '"' => self.parse_quoted_string(),
487             _ => self.parse_quoteless_identifier().map(|s| s.to_string())
488         };
489         r
490     }
491 }
492 
493 impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
494     type Error = Error;
495 
deserialize_any<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,496     fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
497     where
498         V: Visitor<'de>,
499     {
500         self.eat_shit()?;
501         match self.peek_char()? {
502             '"' => self.deserialize_string(visitor),
503             '0'..='9' | '-' => {
504                 let number = Number::read(self)?;
505                 number.visit(self, visitor)
506             }
507             '[' => self.deserialize_seq(visitor),
508             '{' => self.deserialize_map(visitor),
509             _ => {
510                 if self.try_read(b"null") {
511                     return visitor.visit_none();
512                 }
513                 if self.try_read(b"true") {
514                     return visitor.visit_bool(true);
515                 }
516                 if self.try_read(b"false") {
517                     return visitor.visit_bool(false);
518                 }
519                 let s = self.parse_string_value()?;
520                 visitor.visit_string(s)
521             }
522         }
523     }
524 
deserialize_bool<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,525     fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
526     where
527         V: Visitor<'de>,
528     {
529         visitor.visit_bool(self.parse_bool()?)
530     }
531 
deserialize_i8<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,532     fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
533     where
534         V: Visitor<'de>,
535     {
536         let v = self
537             .read_integer(false)
538             .and_then(|s| s.parse().map_err(|_| self.err(ExpectedI8)))?;
539         visitor.visit_i8(v)
540     }
541 
deserialize_i16<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,542     fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>
543     where
544         V: Visitor<'de>,
545     {
546         let v = self
547             .read_integer(false)
548             .and_then(|s| s.parse().map_err(|_| self.err(ExpectedI16)))?;
549         visitor.visit_i16(v)
550     }
551 
deserialize_i32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,552     fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>
553     where
554         V: Visitor<'de>,
555     {
556         let v = self
557             .read_integer(false)
558             .and_then(|s| s.parse().map_err(|_| self.err(ExpectedI32)))?;
559         visitor.visit_i32(v)
560     }
561 
deserialize_i64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,562     fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
563     where
564         V: Visitor<'de>,
565     {
566         let v = self
567             .read_integer(false)
568             .and_then(|s| s.parse().map_err(|_| self.err(ExpectedI64)))?;
569         visitor.visit_i64(v)
570     }
571 
deserialize_u8<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,572     fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
573     where
574         V: Visitor<'de>,
575     {
576         let v = self
577             .read_integer(true)
578             .and_then(|s| s.parse().map_err(|_| self.err(ExpectedU8)))?;
579         visitor.visit_u8(v)
580     }
581 
deserialize_u16<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,582     fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>
583     where
584         V: Visitor<'de>,
585     {
586         let v = self
587             .read_integer(true)
588             .and_then(|s| s.parse().map_err(|_| self.err(ExpectedU16)))?;
589         visitor.visit_u16(v)
590     }
591 
deserialize_u32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,592     fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>
593     where
594         V: Visitor<'de>,
595     {
596         let v = self
597             .read_integer(true)
598             .and_then(|s| s.parse().map_err(|_| self.err(ExpectedU32)))?;
599         visitor.visit_u32(v)
600     }
601 
deserialize_u64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,602     fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
603     where
604         V: Visitor<'de>,
605     {
606         let v = self
607             .read_integer(true)
608             .and_then(|s| s.parse().map_err(|_| self.err(ExpectedU64)))?;
609         visitor.visit_u64(v)
610     }
611 
deserialize_f32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,612     fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
613     where
614         V: Visitor<'de>,
615     {
616         let v = self
617             .read_float()
618             .and_then(|s| s.parse().map_err(|_| self.err(ExpectedF32)))?;
619         visitor.visit_f32(v)
620     }
621 
deserialize_f64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,622     fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>
623     where
624         V: Visitor<'de>,
625     {
626         let v = self
627             .read_float()
628             .and_then(|s| s.parse().map_err(|_| self.err(ExpectedF64)))?;
629         visitor.visit_f64(v)
630     }
631 
deserialize_char<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,632     fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
633     where
634         V: Visitor<'de>,
635     {
636         let c = self
637             .parse_string_value()
638             .and_then(|s| s.chars().next().ok_or_else(|| self.err(ExpectedSingleChar)))?;
639         visitor.visit_char(c)
640     }
641 
deserialize_str<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,642     fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
643     where
644         V: Visitor<'de>,
645     {
646         // we can't always borrow strs from the source as it's not possible
647         // when there's an escape sequence. So str are parsed as strings.
648         self.deserialize_string(visitor)
649     }
650 
deserialize_string<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,651     fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
652     where
653         V: Visitor<'de>,
654     {
655         visitor.visit_string(self.parse_string_value()?)
656     }
657 
deserialize_bytes<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,658     fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
659     where
660         V: Visitor<'de>,
661     {
662         self.deserialize_seq(visitor)
663     }
664 
deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,665     fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
666     where
667         V: Visitor<'de>,
668     {
669         self.deserialize_seq(visitor)
670     }
671 
deserialize_option<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,672     fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
673     where
674         V: Visitor<'de>,
675     {
676         self.eat_shit()?;
677         if self.try_read(b"null") {
678             visitor.visit_none()
679         } else {
680             visitor.visit_some(self)
681         }
682     }
683 
684     // In Serde, unit means an anonymous value containing no data.
deserialize_unit<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,685     fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
686     where
687         V: Visitor<'de>,
688     {
689         self.eat_shit()?;
690         if self.try_read(b"null") {
691             visitor.visit_unit()
692         } else {
693             self.fail(ExpectedNull)
694         }
695     }
696 
697     // Unit struct means a named value containing no data.
deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> where V: Visitor<'de>,698     fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
699     where
700         V: Visitor<'de>,
701     {
702         self.deserialize_unit(visitor)
703     }
704 
deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> where V: Visitor<'de>,705     fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
706     where
707         V: Visitor<'de>,
708     {
709         self.eat_shit()?;
710         visitor.visit_newtype_struct(self)
711     }
712 
deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,713     fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value>
714     where
715         V: Visitor<'de>,
716     {
717         self.eat_shit()?;
718         if self.next_char()? == '[' {
719             let value = visitor.visit_seq(SeqReader::new(&mut self))?;
720             if self.next_char()? == ']' {
721                 Ok(value)
722             } else {
723                 self.fail(ExpectedArrayEnd)
724             }
725         } else {
726             self.fail(ExpectedArray)
727         }
728     }
729 
deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value> where V: Visitor<'de>,730     fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
731     where
732         V: Visitor<'de>,
733     {
734         self.deserialize_seq(visitor)
735     }
736 
deserialize_tuple_struct<V>( self, _name: &'static str, _len: usize, visitor: V, ) -> Result<V::Value> where V: Visitor<'de>,737     fn deserialize_tuple_struct<V>(
738         self,
739         _name: &'static str,
740         _len: usize,
741         visitor: V,
742     ) -> Result<V::Value>
743     where
744         V: Visitor<'de>,
745     {
746         self.deserialize_seq(visitor)
747     }
748 
deserialize_map<V>(mut self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,749     fn deserialize_map<V>(mut self, visitor: V) -> Result<V::Value>
750     where
751         V: Visitor<'de>,
752     {
753         self.eat_shit()?;
754         if self.next_char()? == '{' {
755             let value = match visitor.visit_map(MapReader::new(&mut self)) {
756                 Ok(v) => v,
757                 Err(e) => {
758                     return self.cook_err(e);
759                 }
760             };
761             self.eat_shit()?;
762             if self.next_char()? == '}' {
763                 Ok(value)
764             } else {
765                 self.fail(ExpectedMapEnd)
766             }
767         } else {
768             self.fail(ExpectedMap)
769         }
770     }
771 
deserialize_struct<V>( self, _name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result<V::Value> where V: Visitor<'de>,772     fn deserialize_struct<V>(
773         self,
774         _name: &'static str,
775         _fields: &'static [&'static str],
776         visitor: V,
777     ) -> Result<V::Value>
778     where
779         V: Visitor<'de>,
780     {
781         self.deserialize_map(visitor)
782     }
783 
deserialize_enum<V>( self, _name: &'static str, _variants: &'static [&'static str], visitor: V, ) -> Result<V::Value> where V: Visitor<'de>,784     fn deserialize_enum<V>(
785         self,
786         _name: &'static str,
787         _variants: &'static [&'static str],
788         visitor: V,
789     ) -> Result<V::Value>
790     where
791         V: Visitor<'de>,
792     {
793         self.eat_shit()?;
794         match self.peek_char()? {
795             '"' => {
796                 // Visit a unit variant.
797                 visitor.visit_enum(self.parse_quoted_string()?.into_deserializer())
798             }
799             '{' => {
800                 self.advance(1);
801                 // Visit a newtype variant, tuple variant, or struct variant.
802                 let value = visitor.visit_enum(EnumReader::new(self))?;
803                 self.eat_shit()?;
804                 if self.next_char()? == '}' {
805                     Ok(value)
806                 } else {
807                     self.fail(ExpectedMapEnd)
808                 }
809             }
810             _ => {
811                 visitor.visit_enum(self.parse_quoteless_identifier()?.into_deserializer())
812             }
813         }
814     }
815 
deserialize_identifier<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,816     fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
817     where
818         V: Visitor<'de>,
819     {
820         visitor.visit_string(self.parse_identifier()?)
821     }
822 
deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,823     fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
824     where
825         V: Visitor<'de>,
826     {
827         self.deserialize_any(visitor)
828     }
829 }
830