@@ -35,6 +35,14 @@ trait TupleLikeInfo {
35
35
fn get_field_len ( & self ) -> usize ;
36
36
}
37
37
38
+ trait Container {
39
+ fn get_field_registration < ' a , E : Error > (
40
+ & self ,
41
+ index : usize ,
42
+ registry : & ' a TypeRegistry ,
43
+ ) -> Result < & ' a TypeRegistration , E > ;
44
+ }
45
+
38
46
impl StructLikeInfo for StructInfo {
39
47
fn get_name ( & self ) -> & str {
40
48
self . type_name ( )
@@ -49,6 +57,23 @@ impl StructLikeInfo for StructInfo {
49
57
}
50
58
}
51
59
60
+ impl Container for StructInfo {
61
+ fn get_field_registration < ' a , E : Error > (
62
+ & self ,
63
+ index : usize ,
64
+ registry : & ' a TypeRegistry ,
65
+ ) -> Result < & ' a TypeRegistration , E > {
66
+ let field = self . field_at ( index) . ok_or_else ( || {
67
+ de:: Error :: custom ( format_args ! (
68
+ "no field at index {} on struct {}" ,
69
+ index,
70
+ self . type_name( ) ,
71
+ ) )
72
+ } ) ?;
73
+ get_registration ( field. type_id ( ) , field. type_name ( ) , registry)
74
+ }
75
+ }
76
+
52
77
impl StructLikeInfo for StructVariantInfo {
53
78
fn get_name ( & self ) -> & str {
54
79
self . name ( )
@@ -63,6 +88,23 @@ impl StructLikeInfo for StructVariantInfo {
63
88
}
64
89
}
65
90
91
+ impl Container for StructVariantInfo {
92
+ fn get_field_registration < ' a , E : Error > (
93
+ & self ,
94
+ index : usize ,
95
+ registry : & ' a TypeRegistry ,
96
+ ) -> Result < & ' a TypeRegistration , E > {
97
+ let field = self . field_at ( index) . ok_or_else ( || {
98
+ de:: Error :: custom ( format_args ! (
99
+ "no field at index {} on variant {}" ,
100
+ index,
101
+ self . name( ) ,
102
+ ) )
103
+ } ) ?;
104
+ get_registration ( field. type_id ( ) , field. type_name ( ) , registry)
105
+ }
106
+ }
107
+
66
108
impl TupleLikeInfo for TupleInfo {
67
109
fn get_name ( & self ) -> & str {
68
110
self . type_name ( )
@@ -151,6 +193,23 @@ impl<'de> Deserialize<'de> for Ident {
151
193
}
152
194
}
153
195
196
+ struct U32Visitor ;
197
+
198
+ impl < ' de > Visitor < ' de > for U32Visitor {
199
+ type Value = u32 ;
200
+
201
+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
202
+ formatter. write_str ( "u32" )
203
+ }
204
+
205
+ fn visit_u32 < E > ( self , v : u32 ) -> Result < Self :: Value , E >
206
+ where
207
+ E : Error ,
208
+ {
209
+ Ok ( v)
210
+ }
211
+ }
212
+
154
213
/// A general purpose deserializer for reflected types.
155
214
///
156
215
/// This will return a [`Box<dyn Reflect>`] containing the deserialized data.
@@ -192,7 +251,7 @@ impl<'a, 'de> DeserializeSeed<'de> for UntypedReflectDeserializer<'a> {
192
251
where
193
252
D : serde:: Deserializer < ' de > ,
194
253
{
195
- deserializer. deserialize_any ( UntypedReflectDeserializerVisitor {
254
+ deserializer. deserialize_map ( UntypedReflectDeserializerVisitor {
196
255
registry : self . registry ,
197
256
} )
198
257
}
@@ -396,6 +455,30 @@ impl<'a, 'de> Visitor<'de> for StructVisitor<'a> {
396
455
{
397
456
visit_struct ( & mut map, self . struct_info , self . registry )
398
457
}
458
+
459
+ fn visit_seq < A > ( self , mut seq : A ) -> Result < Self :: Value , A :: Error >
460
+ where
461
+ A : SeqAccess < ' de > ,
462
+ {
463
+ let mut index = 0usize ;
464
+ let mut output = DynamicStruct :: default ( ) ;
465
+
466
+ while let Some ( value) = seq. next_element_seed ( TypedReflectDeserializer {
467
+ registration : self
468
+ . struct_info
469
+ . get_field_registration ( index, self . registry ) ?,
470
+ registry : self . registry ,
471
+ } ) ? {
472
+ let name = self . struct_info . field_at ( index) . unwrap ( ) . name ( ) ;
473
+ output. insert_boxed ( name, value) ;
474
+ index += 1 ;
475
+ if index >= self . struct_info . field_len ( ) {
476
+ break ;
477
+ }
478
+ }
479
+
480
+ Ok ( output)
481
+ }
399
482
}
400
483
401
484
struct TupleStructVisitor < ' a > {
@@ -607,15 +690,10 @@ impl<'a, 'de> Visitor<'de> for EnumVisitor<'a> {
607
690
A : EnumAccess < ' de > ,
608
691
{
609
692
let mut dynamic_enum = DynamicEnum :: default ( ) ;
610
- let ( Ident ( variant_name) , variant) = data. variant ( ) . unwrap ( ) ;
611
- let variant_info = self . enum_info . variant ( & variant_name) . ok_or_else ( || {
612
- let names = self . enum_info . iter ( ) . map ( |variant| variant. name ( ) ) ;
613
- Error :: custom ( format_args ! (
614
- "unknown variant `{}`, expected one of {:?}" ,
615
- variant_name,
616
- ExpectedValues ( names. collect( ) )
617
- ) )
693
+ let ( variant_info, variant) = data. variant_seed ( VariantDeserializer {
694
+ enum_info : self . enum_info ,
618
695
} ) ?;
696
+
619
697
let value: DynamicVariant = match variant_info {
620
698
VariantInfo :: Unit ( ..) => variant. unit_variant ( ) ?. into ( ) ,
621
699
VariantInfo :: Struct ( struct_info) => variant
@@ -650,11 +728,63 @@ impl<'a, 'de> Visitor<'de> for EnumVisitor<'a> {
650
728
. into ( ) ,
651
729
} ;
652
730
653
- dynamic_enum. set_variant ( variant_name , value) ;
731
+ dynamic_enum. set_variant ( variant_info . name ( ) , value) ;
654
732
Ok ( dynamic_enum)
655
733
}
656
734
}
657
735
736
+ struct VariantDeserializer {
737
+ enum_info : & ' static EnumInfo ,
738
+ }
739
+
740
+ impl < ' de > DeserializeSeed < ' de > for VariantDeserializer {
741
+ type Value = & ' static VariantInfo ;
742
+
743
+ fn deserialize < D > ( self , deserializer : D ) -> Result < Self :: Value , D :: Error >
744
+ where
745
+ D : serde:: Deserializer < ' de > ,
746
+ {
747
+ struct VariantVisitor ( & ' static EnumInfo ) ;
748
+
749
+ impl < ' de > Visitor < ' de > for VariantVisitor {
750
+ type Value = & ' static VariantInfo ;
751
+
752
+ fn expecting ( & self , formatter : & mut Formatter ) -> fmt:: Result {
753
+ formatter. write_str ( "expected either a variant index or variant name" )
754
+ }
755
+
756
+ fn visit_str < E > ( self , variant_name : & str ) -> Result < Self :: Value , E >
757
+ where
758
+ E : Error ,
759
+ {
760
+ self . 0 . variant ( variant_name) . ok_or_else ( || {
761
+ let names = self . 0 . iter ( ) . map ( |variant| variant. name ( ) ) ;
762
+ Error :: custom ( format_args ! (
763
+ "unknown variant `{}`, expected one of {:?}" ,
764
+ variant_name,
765
+ ExpectedValues ( names. collect( ) )
766
+ ) )
767
+ } )
768
+ }
769
+
770
+ fn visit_u32 < E > ( self , variant_index : u32 ) -> Result < Self :: Value , E >
771
+ where
772
+ E : Error ,
773
+ {
774
+ self . 0 . variant_at ( variant_index as usize ) . ok_or_else ( || {
775
+ Error :: custom ( format_args ! (
776
+ "no variant found at index `{}` on enum `{}`" ,
777
+ variant_index,
778
+ self . 0 . name( )
779
+ ) )
780
+ } )
781
+ }
782
+ }
783
+
784
+ deserializer. deserialize_identifier ( VariantVisitor ( self . enum_info ) )
785
+ }
786
+ }
787
+
658
788
struct StructVariantVisitor < ' a > {
659
789
struct_info : & ' static StructVariantInfo ,
660
790
registry : & ' a TypeRegistry ,
@@ -673,6 +803,30 @@ impl<'a, 'de> Visitor<'de> for StructVariantVisitor<'a> {
673
803
{
674
804
visit_struct ( & mut map, self . struct_info , self . registry )
675
805
}
806
+
807
+ fn visit_seq < A > ( self , mut seq : A ) -> Result < Self :: Value , A :: Error >
808
+ where
809
+ A : SeqAccess < ' de > ,
810
+ {
811
+ let mut index = 0usize ;
812
+ let mut output = DynamicStruct :: default ( ) ;
813
+
814
+ while let Some ( value) = seq. next_element_seed ( TypedReflectDeserializer {
815
+ registration : self
816
+ . struct_info
817
+ . get_field_registration ( index, self . registry ) ?,
818
+ registry : self . registry ,
819
+ } ) ? {
820
+ let name = self . struct_info . field_at ( index) . unwrap ( ) . name ( ) ;
821
+ output. insert_boxed ( name, value) ;
822
+ index += 1 ;
823
+ if index >= self . struct_info . field_len ( ) {
824
+ break ;
825
+ }
826
+ }
827
+
828
+ Ok ( output)
829
+ }
676
830
}
677
831
678
832
struct TupleVariantVisitor < ' a > {
@@ -1166,4 +1320,55 @@ mod tests {
1166
1320
} ) ;
1167
1321
assert ! ( expected. reflect_partial_eq( output. as_ref( ) ) . unwrap( ) ) ;
1168
1322
}
1323
+
1324
+ #[ test]
1325
+ fn should_deserialize_binary ( ) {
1326
+ let mut map = HashMap :: new ( ) ;
1327
+ map. insert ( 64 , 32 ) ;
1328
+
1329
+ let expected = MyStruct {
1330
+ primitive_value : 123 ,
1331
+ option_value : Some ( String :: from ( "Hello world!" ) ) ,
1332
+ option_value_complex : Some ( SomeStruct { foo : 123 } ) ,
1333
+ tuple_value : ( PI , 1337 ) ,
1334
+ list_value : vec ! [ -2 , -1 , 0 , 1 , 2 ] ,
1335
+ array_value : [ -2 , -1 , 0 , 1 , 2 ] ,
1336
+ map_value : map,
1337
+ struct_value : SomeStruct { foo : 999999999 } ,
1338
+ tuple_struct_value : SomeTupleStruct ( String :: from ( "Tuple Struct" ) ) ,
1339
+ unit_enum : SomeEnum :: Unit ,
1340
+ newtype_enum : SomeEnum :: NewType ( 123 ) ,
1341
+ tuple_enum : SomeEnum :: Tuple ( 1.23 , 3.21 ) ,
1342
+ struct_enum : SomeEnum :: Struct {
1343
+ foo : String :: from ( "Struct variant value" ) ,
1344
+ } ,
1345
+ custom_deserialize : CustomDeserialize {
1346
+ value : 100 ,
1347
+ inner_struct : SomeDeserializableStruct { foo : 101 } ,
1348
+ } ,
1349
+ } ;
1350
+
1351
+ let input = vec ! [
1352
+ 129 , 217 , 40 , 98 , 101 , 118 , 121 , 95 , 114 , 101 , 102 , 108 , 101 , 99 , 116 , 58 , 58 , 115 ,
1353
+ 101 , 114 , 100 , 101 , 58 , 58 , 100 , 101 , 58 , 58 , 116 , 101 , 115 , 116 , 115 , 58 , 58 , 77 , 121 ,
1354
+ 83 , 116 , 114 , 117 , 99 , 116 , 158 , 123 , 172 , 72 , 101 , 108 , 108 , 111 , 32 , 119 , 111 , 114 ,
1355
+ 108 , 100 , 33 , 145 , 123 , 146 , 202 , 64 , 73 , 15 , 219 , 205 , 5 , 57 , 149 , 254 , 255 , 0 , 1 , 2 ,
1356
+ 149 , 254 , 255 , 0 , 1 , 2 , 129 , 64 , 32 , 145 , 206 , 59 , 154 , 201 , 255 , 145 , 172 , 84 , 117 ,
1357
+ 112 , 108 , 101 , 32 , 83 , 116 , 114 , 117 , 99 , 116 , 164 , 85 , 110 , 105 , 116 , 129 , 167 , 78 ,
1358
+ 101 , 119 , 84 , 121 , 112 , 101 , 123 , 129 , 165 , 84 , 117 , 112 , 108 , 101 , 146 , 202 , 63 , 157 ,
1359
+ 112 , 164 , 202 , 64 , 77 , 112 , 164 , 129 , 166 , 83 , 116 , 114 , 117 , 99 , 116 , 145 , 180 , 83 ,
1360
+ 116 , 114 , 117 , 99 , 116 , 32 , 118 , 97 , 114 , 105 , 97 , 110 , 116 , 32 , 118 , 97 , 108 , 117 ,
1361
+ 101 , 146 , 100 , 145 , 101 ,
1362
+ ] ;
1363
+
1364
+ let registry = get_registry ( ) ;
1365
+ let deserializer = UntypedReflectDeserializer :: new ( & registry) ;
1366
+
1367
+ let dynamic_output = deserializer
1368
+ . deserialize ( & mut rmp_serde:: Deserializer :: new ( input. as_slice ( ) ) )
1369
+ . unwrap ( ) ;
1370
+
1371
+ let output = <MyStruct as FromReflect >:: from_reflect ( dynamic_output. as_ref ( ) ) . unwrap ( ) ;
1372
+ assert_eq ! ( expected, output) ;
1373
+ }
1169
1374
}
0 commit comments