33use core:: { fmt, fmt:: Write } ;
44
55use serde:: ser;
6+ use serde:: ser:: SerializeStruct as _;
67
78use heapless:: { consts:: * , String , Vec } ;
89
910use self :: map:: SerializeMap ;
1011use self :: seq:: SerializeSeq ;
11- use self :: struct_:: SerializeStruct ;
12+ use self :: struct_:: { SerializeStruct , SerializeStructVariant } ;
1213
1314mod map;
1415mod seq;
@@ -146,7 +147,7 @@ where
146147 type SerializeTupleVariant = Unreachable ;
147148 type SerializeMap = SerializeMap < ' a , B > ;
148149 type SerializeStruct = SerializeStruct < ' a , B > ;
149- type SerializeStructVariant = Unreachable ;
150+ type SerializeStructVariant = SerializeStructVariant < ' a , B > ;
150151
151152 fn serialize_bool ( self , v : bool ) -> Result < Self :: Ok > {
152153 if v {
@@ -234,11 +235,11 @@ where
234235 }
235236
236237 fn serialize_unit ( self ) -> Result < Self :: Ok > {
237- unreachable ! ( )
238+ self . serialize_none ( )
238239 }
239240
240241 fn serialize_unit_struct ( self , _name : & ' static str ) -> Result < Self :: Ok > {
241- unreachable ! ( )
242+ self . serialize_unit ( )
242243 }
243244
244245 fn serialize_unit_variant (
@@ -253,25 +254,29 @@ where
253254 fn serialize_newtype_struct < T : ?Sized > (
254255 self ,
255256 _name : & ' static str ,
256- _value : & T ,
257+ value : & T ,
257258 ) -> Result < Self :: Ok >
258259 where
259260 T : ser:: Serialize ,
260261 {
261- unreachable ! ( )
262+ value . serialize ( self )
262263 }
263264
264265 fn serialize_newtype_variant < T : ?Sized > (
265- self ,
266+ mut self ,
266267 _name : & ' static str ,
267268 _variant_index : u32 ,
268- _variant : & ' static str ,
269- _value : & T ,
269+ variant : & ' static str ,
270+ value : & T ,
270271 ) -> Result < Self :: Ok >
271272 where
272273 T : ser:: Serialize ,
273274 {
274- unreachable ! ( )
275+ self . buf . push ( b'{' ) ?;
276+ let mut s = SerializeStruct :: new ( & mut self ) ;
277+ s. serialize_field ( variant, value) ?;
278+ s. end ( ) ?;
279+ Ok ( ( ) )
275280 }
276281
277282 fn serialize_seq ( self , _len : Option < usize > ) -> Result < Self :: SerializeSeq > {
@@ -321,7 +326,9 @@ where
321326 _variant : & ' static str ,
322327 _len : usize ,
323328 ) -> Result < Self :: SerializeStructVariant > {
324- unreachable ! ( )
329+ self . buf . push ( b'{' ) ?;
330+
331+ Ok ( SerializeStructVariant :: new ( self ) )
325332 }
326333
327334 fn collect_str < T : ?Sized > ( self , _value : & T ) -> Result < Self :: Ok >
@@ -597,4 +604,40 @@ mod tests {
597604 r#"{"a":true,"b":false}"#
598605 ) ;
599606 }
607+
608+ #[ test]
609+ fn test_unit ( ) {
610+ let a = ( ) ;
611+ assert_eq ! ( & * crate :: to_string:: <N , _>( & a) . unwrap( ) , r#"null"# ) ;
612+ }
613+
614+ #[ test]
615+ fn test_newtype_struct ( ) {
616+ #[ derive( Serialize ) ]
617+ struct A ( pub u32 ) ;
618+ let a = A ( 54 ) ;
619+ assert_eq ! ( & * crate :: to_string:: <N , _>( & a) . unwrap( ) , r#"54"# ) ;
620+ }
621+
622+ #[ test]
623+ fn test_newtype_variant ( ) {
624+ #[ derive( Serialize ) ]
625+ enum A {
626+ A ( u32 ) ,
627+ }
628+ let a = A :: A ( 54 ) ;
629+
630+ assert_eq ! ( & * crate :: to_string:: <N , _>( & a) . unwrap( ) , r#"{"A":54}"# ) ;
631+ }
632+
633+ #[ test]
634+ fn test_struct_variant ( ) {
635+ #[ derive( Serialize ) ]
636+ enum A {
637+ A { x : u32 , y : u16 } ,
638+ }
639+ let a = A :: A { x : 54 , y : 720 } ;
640+
641+ assert_eq ! ( & * crate :: to_string:: <N , _>( & a) . unwrap( ) , r#"{"x":54,"y":720}"# ) ;
642+ }
600643}
0 commit comments