11/// Deserialization module.
22pub use crate :: error:: { Error , ErrorCode , Result } ;
33pub use crate :: parse:: Position ;
4+ use crate :: { Map , Value } ;
45
56use serde:: de:: { self , DeserializeSeed , Deserializer as SerdeError , Visitor } ;
7+ use std:: cell:: RefCell ;
68use std:: { borrow:: Cow , io, str} ;
79
810use self :: { id:: IdDeserializer , tag:: TagDeserializer } ;
@@ -24,6 +26,7 @@ mod value;
2426pub struct Deserializer < ' de > {
2527 bytes : Bytes < ' de > ,
2628 newtype_variant : bool ,
29+ struct_name : Option < String > ,
2730}
2831
2932impl < ' de > Deserializer < ' de > {
@@ -37,6 +40,7 @@ impl<'de> Deserializer<'de> {
3740 Ok ( Deserializer {
3841 bytes : Bytes :: new ( input) ?,
3942 newtype_variant : false ,
43+ struct_name : None ,
4044 } )
4145 }
4246
@@ -150,14 +154,17 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
150154 // `identifier` does not change state if it fails
151155 let ident = self . bytes . identifier ( ) . ok ( ) ;
152156
153- if ident . is_some ( ) {
157+ if let Some ( ident ) = ident {
154158 self . bytes . skip_ws ( ) ?;
155-
159+ self . struct_name = Some ( String :: from_utf8 ( ident . to_vec ( ) ) . unwrap ( ) ) ;
156160 return self . handle_any_struct ( visitor) ;
157161 }
158162
159163 match self . bytes . peek_or_eof ( ) ? {
160- b'(' => self . handle_any_struct ( visitor) ,
164+ b'(' => {
165+ self . struct_name = None ;
166+ self . handle_any_struct ( visitor)
167+ }
161168 b'[' => self . deserialize_seq ( visitor) ,
162169 b'{' => self . deserialize_map ( visitor) ,
163170 b'0' ..=b'9' | b'+' | b'-' => {
@@ -415,7 +422,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
415422 V : Visitor < ' de > ,
416423 {
417424 if self . bytes . consume ( "[" ) {
418- let value = visitor. visit_seq ( CommaSeparated :: new ( b']' , & mut self ) ) ?;
425+ let value = visitor. visit_seq ( CommaSeparated :: new ( b']' , None , & mut self ) ) ?;
419426 self . bytes . comma ( ) ?;
420427
421428 if self . bytes . consume ( "]" ) {
@@ -436,7 +443,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
436443 let old_newtype_variant = self . newtype_variant ;
437444 self . newtype_variant = false ;
438445
439- let value = visitor. visit_seq ( CommaSeparated :: new ( b')' , & mut self ) ) ?;
446+ let value = visitor. visit_seq ( CommaSeparated :: new ( b')' , None , & mut self ) ) ?;
440447 self . bytes . comma ( ) ?;
441448
442449 if old_newtype_variant || self . bytes . consume ( ")" ) {
@@ -470,7 +477,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
470477 V : Visitor < ' de > ,
471478 {
472479 if self . bytes . consume ( "{" ) {
473- let value = visitor. visit_map ( CommaSeparated :: new ( b'}' , & mut self ) ) ?;
480+ let value = visitor. visit_map ( CommaSeparated :: new ( b'}' , None , & mut self ) ) ?;
474481 self . bytes . comma ( ) ?;
475482
476483 if self . bytes . consume ( "}" ) {
@@ -502,7 +509,17 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
502509 let old_newtype_variant = self . newtype_variant ;
503510 self . newtype_variant = false ;
504511
505- let value = visitor. visit_map ( CommaSeparated :: new ( b')' , & mut self ) ) ?;
512+ let value = visitor. visit_map ( CommaSeparated :: new (
513+ b')' ,
514+ Some ( (
515+ self . struct_name
516+ . as_ref ( )
517+ . map ( |s| s. as_bytes ( ) . into ( ) )
518+ . unwrap_or_default ( ) ,
519+ RefCell :: new ( 0 ) ,
520+ ) ) ,
521+ & mut self ,
522+ ) ) ?;
506523 self . bytes . comma ( ) ?;
507524
508525 if old_newtype_variant || self . bytes . consume ( ")" ) {
@@ -546,14 +563,26 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
546563
547564struct CommaSeparated < ' a , ' de : ' a > {
548565 de : & ' a mut Deserializer < ' de > ,
566+ // If we are parsing a struct, this is set to the name of the struct and a counter
567+ // that indicates how many bytes of the name have been read by the ValueVisitor
568+ // constructing the Map/Struct.
569+ // Serde does not provide a way to distinguish between structs and maps. However,
570+ // we can abuse multiple calls to the `size_hint` method to access this information
571+ // one byte at a time.
572+ struct_name : Option < ( Vec < u8 > , RefCell < usize > ) > ,
549573 terminator : u8 ,
550574 had_comma : bool ,
551575}
552576
553577impl < ' a , ' de > CommaSeparated < ' a , ' de > {
554- fn new ( terminator : u8 , de : & ' a mut Deserializer < ' de > ) -> Self {
578+ fn new (
579+ terminator : u8 ,
580+ struct_name : Option < ( Vec < u8 > , RefCell < usize > ) > ,
581+ de : & ' a mut Deserializer < ' de > ,
582+ ) -> Self {
555583 CommaSeparated {
556584 de,
585+ struct_name,
557586 terminator,
558587 had_comma : true ,
559588 }
@@ -626,6 +655,22 @@ impl<'de, 'a> de::MapAccess<'de> for CommaSeparated<'a, 'de> {
626655 self . err ( ErrorCode :: ExpectedMapColon )
627656 }
628657 }
658+
659+ fn size_hint ( & self ) -> Option < usize > {
660+ // trolololololol
661+ match & self . struct_name {
662+ Some ( ( bytes, offset) ) if * offset. borrow ( ) <= bytes. len ( ) => {
663+ let i = * offset. borrow ( ) ;
664+ * offset. borrow_mut ( ) += 1 ;
665+ if bytes. len ( ) == i {
666+ Some ( 0 )
667+ } else {
668+ Some ( bytes[ i] as usize )
669+ }
670+ }
671+ _ => None ,
672+ }
673+ }
629674}
630675
631676struct Enum < ' a , ' de : ' a > {
0 commit comments