mason_rs/serde/
de.rs

1//! Deserialize MASON data to a Rust data structure.
2
3use std::io::{self, BufRead, Read};
4
5use pastey::paste;
6use serde::Deserialize;
7use serde::de::value::StringDeserializer;
8use serde::de::{
9    self, DeserializeSeed, EnumAccess, Error as _, IntoDeserializer, MapAccess, SeqAccess,
10    Unexpected, VariantAccess, Visitor,
11};
12
13use crate::peek_reader::PeekReader;
14use crate::{deserialize, utils};
15
16use super::error::{Error, Result};
17
18/// A structure that deserializes MASON into Rust values.
19pub struct Deserializer<R: Read> {
20    reader: PeekReader<R>,
21    depth: usize,
22}
23
24impl<R: Read> Deserializer<R> {
25    /// Creates a MASON deserializer from an `io::Read`.
26    ///
27    /// Reader-based deserializers do not support deserializing borrowed types
28    /// like `&str`, since the `std::io::Read` trait has no non-copying methods
29    /// -- everything it does involves copying bytes out of the data source.
30    pub fn from_reader(reader: R) -> Self {
31        Self {
32            reader: PeekReader::new(reader),
33            depth: 0,
34        }
35    }
36}
37
38impl<'de> Deserializer<&'de [u8]> {
39    /// Creates a MASON deserializer from a `&[u8]`.
40    pub fn from_slice(input: &'de [u8]) -> Self {
41        Self::from_reader(input)
42    }
43
44    #[allow(clippy::should_implement_trait)]
45    /// Creates a MASON deserializer from a `&str`.
46    pub fn from_str(input: &'de str) -> Self {
47        Self::from_reader(input.as_bytes())
48    }
49}
50
51/// Deserialize an instance of type `T` from an I/O stream of MASON.
52///
53/// The content of the I/O stream is buffered in memory using a [`std::io::BufReader`].
54///
55/// It is expected that the input stream ends after the deserialized value.
56/// If the stream does not end, such as in the case of a persistent socket connection,
57/// this function will not return.
58///
59/// # Example
60///
61/// Reading the contents of a file.
62///
63/// ```
64/// use serde::Deserialize;
65///
66/// use std::error::Error;
67/// use std::fs::File;
68/// use std::io::BufReader;
69/// use std::path::Path;
70///
71/// #[derive(Deserialize, Debug)]
72/// struct User {
73///     fingerprint: String,
74///     location: String,
75/// }
76///
77/// fn read_user_from_file<P: AsRef<Path>>(path: P) -> Result<User, Box<dyn Error>> {
78///     // Open the file in read-only mode with buffer.
79///     let file = File::open(path)?;
80///     let reader = BufReader::new(file);
81///
82///     // Read the MASON contents of the file as an instance of `User`.
83///     let u = mason_rs::from_reader(reader)?;
84///
85///     // Return the `User`.
86///     Ok(u)
87/// }
88///
89/// fn main() {
90/// # }
91/// # fn fake_main() {
92///     let u = read_user_from_file("test.mason").unwrap();
93///     println!("{:#?}", u);
94/// }
95/// ```
96///
97/// Reading from a persistent socket connection.
98///
99/// ```
100/// use serde::Deserialize;
101///
102/// use std::error::Error;
103/// use std::io::BufReader;
104/// use std::net::{TcpListener, TcpStream};
105///
106/// #[derive(Deserialize, Debug)]
107/// struct User {
108///     fingerprint: String,
109///     location: String,
110/// }
111///
112/// fn read_user_from_stream(stream: &mut BufReader<TcpStream>) -> Result<User, Box<dyn Error>> {
113///     let mut de = mason_rs::serde::de::Deserializer::from_reader(stream);
114///     let u = User::deserialize(&mut de)?;
115///
116///     Ok(u)
117/// }
118///
119/// fn main() {
120/// # }
121/// # fn fake_main() {
122///     let listener = TcpListener::bind("127.0.0.1:4000").unwrap();
123///
124///     for tcp_stream in listener.incoming() {
125///         let mut buffered = BufReader::new(tcp_stream.unwrap());
126///         println!("{:#?}", read_user_from_stream(&mut buffered));
127///     }
128/// }
129/// ```
130///
131/// # Errors
132///
133/// This conversion can fail if the structure of the input does not match the
134/// structure expected by `T`, for example if `T` is a struct type but the input
135/// contains something other than a MASON map. It can also fail if the structure
136/// is correct but `T`'s implementation of `Deserialize` decides that something
137/// is wrong with the data, for example required struct fields are missing from
138/// the MASON map or some number is too big to fit in the expected primitive
139/// type.
140pub fn from_reader<'de, T, R>(reader: R) -> Result<T>
141where
142    T: Deserialize<'de>,
143    R: Read + 'de,
144{
145    let mut deserializer = Deserializer::from_reader(reader);
146    let t = T::deserialize(&mut deserializer)?;
147    deserialize::skip_whitespace(&mut deserializer.reader)?;
148    if let Some(garbage) = deserializer.reader.peek()? {
149        Err(Error::from(io::Error::new(
150            io::ErrorKind::InvalidData,
151            format!(
152                "Trailing garbage after document: {:?}",
153                utils::to_char(garbage)
154            ),
155        )))
156    } else {
157        Ok(t)
158    }
159}
160
161/// Deserialize an instance of type `T` from bytes of MASON text.
162///
163/// # Example
164///
165/// ```
166/// use serde::Deserialize;
167///
168/// #[derive(Deserialize, Debug)]
169/// struct User {
170///     fingerprint: String,
171///     location: String,
172/// }
173///
174/// // The type of `j` is `&[u8]`
175/// let j = b"
176///     fingerprint: \"0xF9BA143B95FF6D82\",
177///     location: \"Menlo Park, CA\"
178/// ";
179///
180/// let u: User = mason_rs::from_slice(j).unwrap();
181/// println!("{:#?}", u);
182/// ```
183///
184/// # Errors
185///
186/// This conversion can fail if the structure of the input does not match the
187/// structure expected by `T`, for example if `T` is a struct type but the input
188/// contains something other than a MASON map. It can also fail if the structure
189/// is correct but `T`'s implementation of `Deserialize` decides that something
190/// is wrong with the data, for example required struct fields are missing from
191/// the MASON map or some number is too big to fit in the expected primitive
192/// type.
193pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T>
194where
195    T: Deserialize<'de>,
196{
197    from_reader(bytes)
198}
199
200/// Deserialize an instance of type `T` from a string of MASON text.
201///
202/// # Example
203///
204/// ```
205/// use serde::Deserialize;
206///
207/// #[derive(Deserialize, Debug)]
208/// struct User {
209///     fingerprint: String,
210///     location: String,
211/// }
212///
213/// // The type of `j` is `&str`
214/// let j = "
215///     fingerprint: \"0xF9BA143B95FF6D82\"
216///     location: \"Menlo Park, CA\"
217/// ";
218///
219/// let u: User = mason_rs::from_str(j).unwrap();
220/// println!("{:#?}", u);
221/// ```
222///
223/// # Errors
224///
225/// This conversion can fail if the structure of the input does not match the
226/// structure expected by `T`, for example if `T` is a struct type but the input
227/// contains something other than a MASON map. It can also fail if the structure
228/// is correct but `T`'s implementation of `Deserialize` decides that something
229/// is wrong with the data, for example required struct fields are missing from
230/// the MASON map or some number is too big to fit in the expected primitive
231/// type.
232pub fn from_str<'de, T>(string: &'de str) -> Result<T>
233where
234    T: Deserialize<'de>,
235{
236    from_reader(string.as_bytes())
237}
238
239impl<R: Read> Deserializer<R> {
240    // read_byte, but return Error::Eof on EOF
241    fn expect_read_byte(&mut self) -> Result<u8> {
242        match self.reader.read_byte() {
243            Ok(Some(byte)) => Ok(byte),
244            Ok(None) => Err(Error::eof()),
245            Err(err) => Err(Error::from(err)),
246        }
247    }
248
249    // peek, but return Error::Eof on EOF
250    fn expect_peek(&mut self) -> Result<u8> {
251        match self.reader.peek() {
252            Ok(Some(byte)) => Ok(byte),
253            Ok(None) => Err(Error::eof()),
254            Err(err) => Err(Error::from(err)),
255        }
256    }
257
258    // peek2, but return Error::Eof on EOF
259    fn expect_peek2(&mut self) -> Result<[u8; 2]> {
260        match self.reader.peek2() {
261            Ok(Some(bytes)) => Ok(bytes),
262            Ok(None) => Err(Error::eof()),
263            Err(err) => Err(Error::from(err)),
264        }
265    }
266}
267
268/// Deserialize an f64, and see if it can be converted into the given type
269macro_rules! deserialize_integer {
270    ($type:ty) => {
271        paste! {
272            fn [<deserialize_ $type>]<V>(self, visitor: V) -> Result<V::Value>
273            where
274                V: Visitor<'de>,
275            {
276                let num = $crate::deserialize::parse_number(&mut self.reader)?;
277                if num.fract() != 0.0 || num >  $type::MAX as f64 || num <  $type::MIN as f64 {
278                    Err(Error::invalid_type(
279                        Unexpected::Float(num),
280                        &stringify!($type),
281                    ))
282                } else {
283                    visitor.[<visit_ $type>](num as  $type)
284                }
285            }
286        }
287    };
288}
289
290impl<'de, R: Read + 'de> de::Deserializer<'de> for &mut Deserializer<R> {
291    type Error = Error;
292
293    // Look at the input data to decide what Serde data model type to
294    // deserialize as. Not all data formats are able to support this operation.
295    // Formats that support `deserialize_any` are known as self-describing.
296    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
297    where
298        V: Visitor<'de>,
299    {
300        if self.depth == 100 {
301            return Err(Error::custom("reached maximum depth"));
302        }
303
304        deserialize::skip_whitespace(&mut self.reader)?;
305
306        let first_byte = self.expect_peek()?;
307        match first_byte {
308            b'{' => return self.deserialize_map(visitor),
309            b'[' => return self.deserialize_seq(visitor),
310            b'"' => {
311                let string = deserialize::parse_string(&mut self.reader)?;
312                if self.depth == 0 {
313                    deserialize::skip_whitespace(&mut self.reader)?;
314                    if self.reader.peek()? == Some(b':') {
315                        return visitor
316                            .visit_map(SepSeparated::with_initial_key(self, false, string));
317                    }
318                }
319                return string.into_deserializer().deserialize_string(visitor);
320            }
321            b'r' => {
322                if let Some([_, second_byte]) = self.reader.peek2()? {
323                    if matches!(second_byte, b'"' | b'#') {
324                        return self.deserialize_string(visitor);
325                    }
326                }
327            }
328            b'|' => return self.deserialize_string(visitor),
329            b'b' => {
330                if let Some([_, second_byte]) = self.reader.peek2()? {
331                    if matches!(second_byte, b'"') {
332                        return self.deserialize_bytes(visitor);
333                    }
334                }
335            }
336            _ => {}
337        }
338
339        if first_byte.is_ascii_digit() || matches!(first_byte, b'+' | b'-' | b'.') {
340            self.deserialize_f64(visitor)
341        } else {
342            let identifier = deserialize::parse_identifier(&mut self.reader)?;
343            if self.depth == 0 {
344                deserialize::skip_whitespace(&mut self.reader)?;
345                if self.reader.peek()? == Some(b':') {
346                    return visitor
347                        .visit_map(SepSeparated::with_initial_key(self, false, identifier));
348                }
349            }
350            match identifier.as_str() {
351                "true" => visitor.visit_bool(true),
352                "false" => visitor.visit_bool(false),
353                "null" => visitor.visit_unit(),
354                _ => Err(Error::custom(format!("malformed value: {identifier}"))),
355            }
356        }
357    }
358
359    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
360    where
361        V: Visitor<'de>,
362    {
363        let expected: &[u8] = if self.expect_peek()? == b't' {
364            b"true"
365        } else {
366            b"false"
367        };
368        for b in expected {
369            let byte = self.expect_read_byte()?;
370            if byte != *b {
371                return Err(Error::invalid_type(
372                    Unexpected::Char(utils::to_char(*b)),
373                    &"bool",
374                ));
375            }
376        }
377        visitor.visit_bool(expected == b"true")
378    }
379
380    deserialize_integer!(i8);
381    deserialize_integer!(i16);
382    deserialize_integer!(i32);
383    deserialize_integer!(i64);
384
385    deserialize_integer!(u8);
386    deserialize_integer!(u16);
387    deserialize_integer!(u32);
388    deserialize_integer!(u64);
389
390    fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
391    where
392        V: Visitor<'de>,
393    {
394        let num = deserialize::parse_number(&mut self.reader)?;
395        let num_f32 = num as f32;
396
397        // se if num is representable as an f32
398        if (num - f64::from(num_f32)).abs() > 10.0 * f64::from(f32::EPSILON) {
399            Err(Error::invalid_type(Unexpected::Float(num), &"f32"))
400        } else {
401            visitor.visit_f32(num_f32)
402        }
403    }
404
405    fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>
406    where
407        V: Visitor<'de>,
408    {
409        visitor.visit_f64(deserialize::parse_number(&mut self.reader)?)
410    }
411
412    fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
413    where
414        V: Visitor<'de>,
415    {
416        let string = deserialize::parse_string(&mut self.reader)?;
417        let mut chars = string.chars();
418        match (chars.next(), chars.next()) {
419            (Some(c), None) => visitor.visit_char(c),
420            _ => Err(Error::invalid_type(Unexpected::Str(&string), &"char")),
421        }
422    }
423
424    // Can we get a 'de str here? I don't think so, right?
425    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
426    where
427        V: Visitor<'de>,
428    {
429        self.deserialize_string(visitor)
430    }
431
432    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
433    where
434        V: Visitor<'de>,
435    {
436        let byte = self.expect_peek()?;
437        match byte {
438            b'"' => visitor.visit_string(deserialize::parse_string(&mut self.reader)?),
439            b'r' => visitor.visit_string(deserialize::parse_raw_string(&mut self.reader)?),
440            b'|' => visitor.visit_string(deserialize::parse_multi_line_string(&mut self.reader)?),
441            _ => Err(Error::invalid_type(
442                Unexpected::Char(utils::to_char(byte)),
443                &"string",
444            )),
445        }
446    }
447
448    // Can we get a 'de [u8] here? I don't think so, right?
449    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
450    where
451        V: Visitor<'de>,
452    {
453        self.deserialize_byte_buf(visitor)
454    }
455
456    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
457    where
458        V: Visitor<'de>,
459    {
460        visitor.visit_byte_buf(deserialize::parse_byte_string(&mut self.reader)?)
461    }
462
463    // An absent optional is represented as the MASON `null` and a present
464    // optional is represented as just the contained value.
465    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
466    where
467        V: Visitor<'de>,
468    {
469        // TODO: the first two bytes being 'nu' does not actually guarantee
470        // that the value is 'null'
471        if &self.expect_peek2()? == b"nu" {
472            self.reader.consume(4);
473            visitor.visit_none()
474        } else {
475            visitor.visit_some(self)
476        }
477    }
478
479    // In Serde, unit means an anonymous value containing no data.
480    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
481    where
482        V: Visitor<'de>,
483    {
484        for b in b"null" {
485            let byte = self.expect_read_byte()?;
486            if byte != *b {
487                return Err(Error::invalid_type(
488                    Unexpected::Char(utils::to_char(byte)),
489                    &"unit",
490                ));
491            }
492        }
493        visitor.visit_unit()
494    }
495
496    // Unit struct means a named value containing no data.
497    fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
498    where
499        V: Visitor<'de>,
500    {
501        self.deserialize_unit(visitor)
502    }
503
504    // As is done here, serializers are encouraged to treat newtype structs as
505    // insignificant wrappers around the data they contain. That means not
506    // parsing anything other than the contained value.
507    fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
508    where
509        V: Visitor<'de>,
510    {
511        visitor.visit_newtype_struct(self)
512    }
513
514    // Deserialization of compound types like sequences and maps happens by
515    // passing the visitor an "Access" object that gives it the ability to
516    // iterate through the data contained in the sequence.
517    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
518    where
519        V: Visitor<'de>,
520    {
521        // Parse the opening bracket of the sequence.
522        let byte = self.expect_read_byte()?;
523        if byte == b'[' {
524            // Give the visitor access to each element of the sequence.
525            let value = visitor.visit_seq(SepSeparated::new(self, true))?;
526            // Parse the closing bracket of the sequence.
527            let byte = self.expect_read_byte()?;
528            if byte == b']' {
529                Ok(value)
530            } else {
531                Err(Error::invalid_type(
532                    Unexpected::Char(utils::to_char(byte)),
533                    &"array end",
534                ))
535            }
536        } else {
537            Err(Error::invalid_type(
538                Unexpected::Char(utils::to_char(byte)),
539                &"seq",
540            ))
541        }
542    }
543
544    // Tuples look just like sequences in MASON.
545    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
546    where
547        V: Visitor<'de>,
548    {
549        self.deserialize_seq(visitor)
550    }
551
552    // Tuple structs look just like sequences in MASON.
553    fn deserialize_tuple_struct<V>(
554        self,
555        _name: &'static str,
556        _len: usize,
557        visitor: V,
558    ) -> Result<V::Value>
559    where
560        V: Visitor<'de>,
561    {
562        self.deserialize_seq(visitor)
563    }
564
565    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
566    where
567        V: Visitor<'de>,
568    {
569        if self.depth == 0 {
570            // If depth is 0, struct does not need to be surrounded by braces
571            let mut has_opening_brace = false;
572            if self.expect_peek()? == b'{' {
573                self.reader.read_byte()?;
574                has_opening_brace = true;
575            }
576
577            self.depth += 1;
578            let value = visitor.visit_map(SepSeparated::new(self, has_opening_brace))?;
579            self.depth -= 1;
580
581            match (has_opening_brace, self.reader.peek()? == Some(b'}')) {
582                (true, true) => {
583                    self.reader.read_byte()?;
584                }
585                (false, true) => {
586                    return Err(Error::custom(
587                        "got closing bracket without an opening bracket",
588                    ));
589                }
590                (true, false) => return Err(Error::custom("unclosed bracket")),
591                (false, false) => {}
592            }
593            return Ok(value);
594        }
595
596        let byte = self.expect_read_byte()?;
597        if byte == b'{' {
598            self.depth += 1;
599            let value = visitor.visit_map(SepSeparated::new(self, true))?;
600            self.depth -= 1;
601
602            let byte = self.expect_read_byte()?;
603            if byte == b'}' {
604                Ok(value)
605            } else {
606                Err(Error::invalid_type(
607                    Unexpected::Char(utils::to_char(byte)),
608                    &"map end",
609                ))
610            }
611        } else {
612            Err(Error::invalid_type(
613                Unexpected::Char(utils::to_char(byte)),
614                &"map",
615            ))
616        }
617    }
618
619    // Structs look just like maps in MASON.
620    fn deserialize_struct<V>(
621        self,
622        _name: &'static str,
623        _fields: &'static [&'static str],
624        visitor: V,
625    ) -> Result<V::Value>
626    where
627        V: Visitor<'de>,
628    {
629        self.deserialize_map(visitor)
630    }
631
632    fn deserialize_enum<V>(
633        self,
634        _name: &'static str,
635        _variants: &'static [&'static str],
636        visitor: V,
637    ) -> Result<V::Value>
638    where
639        V: Visitor<'de>,
640    {
641        let variant = deserialize::parse_identifier(&mut self.reader)?;
642        deserialize::skip_whitespace(&mut self.reader)?;
643
644        if self.reader.peek()? != Some(b':') {
645            // Visit a unit variant.
646            visitor.visit_enum(variant.into_deserializer())
647        } else if self.depth == 0 {
648            // skip colon
649            self.reader.read_byte()?;
650            deserialize::skip_whitespace(&mut self.reader)?;
651
652            Ok(visitor.visit_enum(Enum::new(self, variant))?)
653        } else {
654            todo!()
655        }
656    }
657
658    // An identifier in Serde is the type that identifies a field of a struct or
659    // the variant of an enum. In MASON, struct fields and enum variants are
660    // represented as strings. In other formats they may be represented as
661    // numeric indices.
662    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
663    where
664        V: Visitor<'de>,
665    {
666        visitor.visit_string(deserialize::parse_identifier(&mut self.reader)?)
667    }
668
669    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
670    where
671        V: Visitor<'de>,
672    {
673        self.deserialize_any(visitor)
674    }
675}
676
677// In order to handle seps correctly when deserializing a MASON array or map,
678// we need to track whether we are on the first element or past the first
679// element.
680struct SepSeparated<'a, R: Read> {
681    de: &'a mut Deserializer<R>,
682    first: bool,
683    // should we expect a closing bracket?
684    expect_closing: bool,
685    first_key: Option<String>,
686    // a multi line string is always a valid sep
687    previously_parsed_multi_line_string: bool,
688}
689
690impl<'a, R: Read> SepSeparated<'a, R> {
691    fn new(de: &'a mut Deserializer<R>, expect_closing: bool) -> Self {
692        SepSeparated {
693            de,
694            first: true,
695            expect_closing,
696            first_key: None,
697            previously_parsed_multi_line_string: false,
698        }
699    }
700
701    fn with_initial_key(
702        de: &'a mut Deserializer<R>,
703        expect_closing: bool,
704        first_key: String,
705    ) -> Self {
706        SepSeparated {
707            de,
708            first: true,
709            expect_closing,
710            first_key: Some(first_key),
711            previously_parsed_multi_line_string: false,
712        }
713    }
714}
715
716// `SeqAccess` is provided to the `Visitor` to give it the ability to iterate
717// through elements of the sequence.
718impl<'de, R: Read + 'de> SeqAccess<'de> for SepSeparated<'_, R> {
719    type Error = Error;
720
721    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
722    where
723        T: DeserializeSeed<'de>,
724    {
725        if !self.first {
726            let valid_sep = self.previously_parsed_multi_line_string
727                || deserialize::parse_sep(&mut self.de.reader)?;
728            deserialize::skip_whitespace(&mut self.de.reader)?;
729
730            if !valid_sep {
731                if self.de.expect_peek()? == b']' {
732                    return Ok(None);
733                } else {
734                    return Err(Error::custom("array missing sep"));
735                }
736            }
737        }
738        self.first = false;
739
740        deserialize::skip_whitespace(&mut self.de.reader)?;
741
742        // Check if there are no more elements.
743        if self.de.expect_peek()? == b']' {
744            return Ok(None);
745        }
746
747        // Deserialize an array element.
748        self.de.depth += 1;
749        self.previously_parsed_multi_line_string = self.de.reader.peek()? == Some(b'|');
750        let result = seed.deserialize(&mut *self.de).map(Some);
751        self.de.depth -= 1;
752
753        result
754    }
755}
756
757// `MapAccess` is provided to the `Visitor` to give it the ability to iterate
758// through entries of the map.
759impl<'de, R: Read + 'de> MapAccess<'de> for SepSeparated<'_, R> {
760    type Error = Error;
761
762    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
763    where
764        K: DeserializeSeed<'de>,
765    {
766        if let Some(key) = self.first_key.take() {
767            self.first = false;
768            return seed.deserialize(key.into_deserializer()).map(Some);
769        }
770
771        let valid_sep = if !self.first {
772            self.previously_parsed_multi_line_string || deserialize::parse_sep(&mut self.de.reader)?
773        } else {
774            true
775        };
776        deserialize::skip_whitespace(&mut self.de.reader)?;
777
778        match (self.de.reader.peek()?, self.expect_closing) {
779            (Some(b'}'), true) | (None, false) => return Ok(None),
780            (Some(b'}'), false) => {
781                return Err(Error::custom(
782                    "got closing bracket without an opening bracket",
783                ));
784            }
785            (None, true) => return Err(Error::custom("unclosed bracket")),
786            _ => {}
787        }
788
789        if !valid_sep {
790            return Err(Error::custom("map missing sep"));
791        }
792        self.first = false;
793
794        let key = if self.de.expect_peek()? == b'"' {
795            deserialize::parse_string(&mut self.de.reader)?
796        } else {
797            deserialize::parse_identifier(&mut self.de.reader)?
798        };
799
800        seed.deserialize(key.into_deserializer()).map(Some)
801    }
802
803    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
804    where
805        V: DeserializeSeed<'de>,
806    {
807        deserialize::skip_whitespace(&mut self.de.reader)?;
808        let byte = self.de.expect_read_byte()?;
809        if byte != b':' {
810            return Err(Error::invalid_type(
811                Unexpected::Char(utils::to_char(byte)),
812                &"map colon",
813            ));
814        }
815        deserialize::skip_whitespace(&mut self.de.reader)?;
816
817        // Deserialize a map value.
818        self.de.depth += 1;
819        self.previously_parsed_multi_line_string = self.de.reader.peek()? == Some(b'|');
820        let result = seed.deserialize(&mut *self.de);
821        self.de.depth -= 1;
822
823        result
824    }
825}
826
827struct Enum<'a, R: Read> {
828    de: &'a mut Deserializer<R>,
829    variant: Option<String>,
830}
831
832impl<'a, R: Read> Enum<'a, R> {
833    fn new(de: &'a mut Deserializer<R>, variant: String) -> Self {
834        Enum {
835            de,
836            variant: Some(variant),
837        }
838    }
839}
840
841// `EnumAccess` is provided to the `Visitor` to give it the ability to determine
842// which variant of the enum is supposed to be deserialized.
843//
844// Note that all enum deserialization methods in Serde refer exclusively to the
845// "externally tagged" enum representation.
846impl<'de, R: Read + 'de> EnumAccess<'de> for Enum<'_, R> {
847    type Error = Error;
848    type Variant = Self;
849
850    fn variant_seed<V>(mut self, seed: V) -> Result<(V::Value, Self::Variant)>
851    where
852        V: DeserializeSeed<'de>,
853    {
854        // We have already parsed the enum varian, so we don't need to do anything here
855        let Some(variant) = self.variant.take() else {
856            return Err(Error::custom("variant_seed got called more than once?"));
857        };
858        let string_deserializer: StringDeserializer<Error> = variant.into_deserializer();
859        Ok((seed.deserialize(string_deserializer)?, self))
860    }
861}
862
863// `VariantAccess` is provided to the `Visitor` to give it the ability to see
864// the content of the single variant that it decided to deserialize.
865impl<'de, R: Read + 'de> VariantAccess<'de> for Enum<'_, R> {
866    type Error = Error;
867
868    // If the `Visitor` expected this variant to be a unit variant, the input
869    // should have been the plain string case handled in `deserialize_enum`.
870    fn unit_variant(self) -> Result<()> {
871        Err(Error::invalid_type(
872            Unexpected::UnitVariant,
873            &"visitor should have handled unit variant case",
874        ))
875    }
876
877    // Newtype variants are represented in MASON as `{ NAME: VALUE }` so
878    // deserialize the value here.
879    fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
880    where
881        T: DeserializeSeed<'de>,
882    {
883        seed.deserialize(self.de)
884    }
885
886    // Tuple variants are represented in MASON as `{ NAME: [DATA...] }` so
887    // deserialize the sequence of data here.
888    fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
889    where
890        V: Visitor<'de>,
891    {
892        de::Deserializer::deserialize_seq(self.de, visitor)
893    }
894
895    // Struct variants are represented in MASON as `{ NAME: { K: V, ... } }` so
896    // deserialize the inner map here.
897    fn struct_variant<V>(self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value>
898    where
899        V: Visitor<'de>,
900    {
901        de::Deserializer::deserialize_map(self.de, visitor)
902    }
903}
904
905#[cfg(test)]
906mod tests {
907    use std::collections::HashMap;
908
909    use super::*;
910
911    #[test]
912    fn test_struct() {
913        #[derive(Deserialize, PartialEq, Debug)]
914        struct Test {
915            int: u32,
916            seq: Vec<String>,
917        }
918
919        let j = "\
920int: 1
921seq: [\"a\", \"b\"]";
922        let expected = Test {
923            int: 1,
924            seq: vec!["a".to_owned(), "b".to_owned()],
925        };
926        assert_eq!(expected, from_str(j).unwrap());
927    }
928
929    #[test]
930    fn test_enum() {
931        #[derive(Deserialize, PartialEq, Debug)]
932        enum E {
933            Unit,
934            Newtype(u32),
935            Tuple(u32, u32),
936            Struct { a: u32 },
937        }
938
939        let j = "Unit";
940        let expected = E::Unit;
941        assert_eq!(expected, from_str(j).unwrap());
942
943        let j = "Newtype: 1";
944        let expected = E::Newtype(1);
945        assert_eq!(expected, from_str(j).unwrap());
946
947        let j = "Tuple: [1, 2]";
948        let expected = E::Tuple(1, 2);
949        assert_eq!(expected, from_str(j).unwrap());
950
951        let j = "\
952Struct: {
953    \"a\": 1
954}";
955        let expected = E::Struct { a: 1 };
956        assert_eq!(expected, from_str(j).unwrap());
957    }
958
959    #[test]
960    fn test_complicated() {
961        #[derive(Deserialize, PartialEq, Debug)]
962        struct Complicated {
963            map: HashMap<String, Vec<f32>>,
964            bytes: Vec<u8>,
965            option: Option<String>,
966            nothing: (),
967        }
968
969        let j = "\
970map: {
971    \"a \\\" \\\\ \\\\\\\" difficult key 🏳️‍⚧️\": [-1000000000, 1230, 0.000000000321]
972}
973bytes: [66, 121, 116, 101, 115, 33]
974option: null
975nothing: null";
976        let expected = Complicated {
977            map: HashMap::from([(
978                "a \" \\ \\\" difficult key 🏳️‍⚧️".into(),
979                vec![-1e9, 1.23e3, 3.21e-10],
980            )]),
981            bytes: b"Bytes!".to_vec(),
982            option: None,
983            nothing: (),
984        };
985        assert_eq!(expected, from_str(j).unwrap());
986    }
987}