@@ -67,7 +67,7 @@ impl ser::Serializer for Serializer {
6767 type SerializeTupleVariant = ser:: Impossible < Self :: Ok , Self :: Error > ;
6868 type SerializeMap = MapSerializer ;
6969 type SerializeStruct = StructSerializer ;
70- type SerializeStructVariant = ser :: Impossible < Self :: Ok , Self :: Error > ;
70+ type SerializeStructVariant = StructVariantSerializer ;
7171
7272 fn serialize_bool ( self , v : bool ) -> Result < Self :: Ok > {
7373 Ok ( Num ( NumKind :: Bool ( v) ) )
@@ -195,13 +195,13 @@ impl ser::Serializer for Serializer {
195195 self ,
196196 _name : & ' static str ,
197197 _variant_index : u32 ,
198- _variant : & ' static str ,
198+ variant : & ' static str ,
199199 _len : usize ,
200200 ) -> Result < Self :: SerializeStructVariant > {
201- Err ( ErrorKind :: Serialize (
202- "Unsupported data for serialization: struct variant" . to_owned ( ) ,
203- )
204- . into ( ) )
201+ Ok ( StructVariantSerializer {
202+ variant,
203+ value : BTreeMap :: new ( ) ,
204+ } )
205205 }
206206
207207 fn serialize_tuple ( self , _len : usize ) -> Result < Self :: SerializeTuple > {
@@ -339,6 +339,32 @@ impl ser::SerializeStruct for StructSerializer {
339339 }
340340}
341341
342+ struct StructVariantSerializer {
343+ variant : & ' static str ,
344+ value : BTreeMap < String , SimpleValue > ,
345+ }
346+
347+ impl ser:: SerializeStructVariant for StructVariantSerializer {
348+ type Ok = SimpleValue ;
349+ type Error = Error ;
350+
351+ fn serialize_field < T > ( & mut self , key : & ' static str , val : & T ) -> Result < ( ) >
352+ where
353+ T : ?Sized + ser:: Serialize ,
354+ {
355+ let val: SimpleValue = val. serialize ( Serializer ) ?;
356+ self . value . insert ( key. into ( ) , val) ;
357+ Ok ( ( ) )
358+ }
359+
360+ fn end ( self ) -> Result < Self :: Ok > {
361+ Ok ( SimpleValue :: Union (
362+ self . variant . to_string ( ) ,
363+ Some ( Box :: new ( Record ( self . value ) ) ) ,
364+ ) )
365+ }
366+ }
367+
342368impl serde:: ser:: Serialize for SimpleValue {
343369 fn serialize < S > (
344370 & self ,
0 commit comments