1
1
use std:: fmt;
2
2
3
- use super :: ReflectPathError ;
3
+ use super :: { AccessError , ReflectPathError } ;
4
4
use crate :: { Reflect , ReflectMut , ReflectRef , VariantType } ;
5
5
use thiserror:: Error ;
6
6
@@ -13,34 +13,43 @@ pub(super) enum Error<'a> {
13
13
access. kind( ) ,
14
14
access. display_value( ) ,
15
15
) ]
16
- Access { ty : Type , access : AccessRef < ' a > } ,
16
+ Access {
17
+ ty : TypeShape ,
18
+ access : AccessRef < ' a > ,
19
+ } ,
17
20
18
21
#[ error( "invalid type shape: expected {expected} but found a reflect {actual}" ) ]
19
- Type { expected : Type , actual : Type } ,
22
+ Type {
23
+ expected : TypeShape ,
24
+ actual : TypeShape ,
25
+ } ,
20
26
21
27
#[ error( "invalid enum access: expected {expected} variant but found {actual} variant" ) ]
22
- Enum { expected : Type , actual : Type } ,
28
+ Enum {
29
+ expected : TypeShape ,
30
+ actual : TypeShape ,
31
+ } ,
23
32
}
24
33
25
34
impl < ' a > Error < ' a > {
26
35
fn with_offset ( self , offset : usize ) -> ReflectPathError < ' a > {
27
- let error = super :: AccessError ( self ) ;
36
+ let error = AccessError ( self ) ;
28
37
ReflectPathError :: InvalidAccess { offset, error }
29
38
}
30
39
}
31
40
impl Error < ' static > {
32
- fn bad_enum ( expected : Type , actual : impl Into < Type > ) -> Self {
41
+ fn bad_enum_variant ( expected : TypeShape , actual : impl Into < TypeShape > ) -> Self {
33
42
let actual = actual. into ( ) ;
34
43
Error :: Enum { expected, actual }
35
44
}
36
- fn bad_type ( expected : Type , actual : impl Into < Type > ) -> Self {
45
+ fn bad_type ( expected : TypeShape , actual : impl Into < TypeShape > ) -> Self {
37
46
let actual = actual. into ( ) ;
38
47
Error :: Type { expected, actual }
39
48
}
40
49
}
41
50
42
51
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
43
- pub ( super ) enum Type {
52
+ pub ( super ) enum TypeShape {
44
53
Struct ,
45
54
TupleStruct ,
46
55
Tuple ,
@@ -52,42 +61,42 @@ pub(super) enum Type {
52
61
Unit ,
53
62
}
54
63
55
- impl fmt:: Display for Type {
64
+ impl fmt:: Display for TypeShape {
56
65
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
57
66
let name = match self {
58
- Type :: Struct => "struct" ,
59
- Type :: TupleStruct => "tuple struct" ,
60
- Type :: Tuple => "tuple" ,
61
- Type :: List => "list" ,
62
- Type :: Array => "array" ,
63
- Type :: Map => "map" ,
64
- Type :: Enum => "enum" ,
65
- Type :: Value => "value" ,
66
- Type :: Unit => "unit" ,
67
+ TypeShape :: Struct => "struct" ,
68
+ TypeShape :: TupleStruct => "tuple struct" ,
69
+ TypeShape :: Tuple => "tuple" ,
70
+ TypeShape :: List => "list" ,
71
+ TypeShape :: Array => "array" ,
72
+ TypeShape :: Map => "map" ,
73
+ TypeShape :: Enum => "enum" ,
74
+ TypeShape :: Value => "value" ,
75
+ TypeShape :: Unit => "unit" ,
67
76
} ;
68
77
write ! ( f, "{name}" )
69
78
}
70
79
}
71
- impl < ' a > From < ReflectRef < ' a > > for Type {
80
+ impl < ' a > From < ReflectRef < ' a > > for TypeShape {
72
81
fn from ( value : ReflectRef < ' a > ) -> Self {
73
82
match value {
74
- ReflectRef :: Struct ( _) => Type :: Struct ,
75
- ReflectRef :: TupleStruct ( _) => Type :: TupleStruct ,
76
- ReflectRef :: Tuple ( _) => Type :: Tuple ,
77
- ReflectRef :: List ( _) => Type :: List ,
78
- ReflectRef :: Array ( _) => Type :: Array ,
79
- ReflectRef :: Map ( _) => Type :: Map ,
80
- ReflectRef :: Enum ( _) => Type :: Enum ,
81
- ReflectRef :: Value ( _) => Type :: Value ,
83
+ ReflectRef :: Struct ( _) => TypeShape :: Struct ,
84
+ ReflectRef :: TupleStruct ( _) => TypeShape :: TupleStruct ,
85
+ ReflectRef :: Tuple ( _) => TypeShape :: Tuple ,
86
+ ReflectRef :: List ( _) => TypeShape :: List ,
87
+ ReflectRef :: Array ( _) => TypeShape :: Array ,
88
+ ReflectRef :: Map ( _) => TypeShape :: Map ,
89
+ ReflectRef :: Enum ( _) => TypeShape :: Enum ,
90
+ ReflectRef :: Value ( _) => TypeShape :: Value ,
82
91
}
83
92
}
84
93
}
85
- impl From < VariantType > for Type {
94
+ impl From < VariantType > for TypeShape {
86
95
fn from ( value : VariantType ) -> Self {
87
96
match value {
88
- VariantType :: Struct => Type :: Struct ,
89
- VariantType :: Tuple => Type :: Tuple ,
90
- VariantType :: Unit => Type :: Unit ,
97
+ VariantType :: Struct => TypeShape :: Struct ,
98
+ VariantType :: Tuple => TypeShape :: Tuple ,
99
+ VariantType :: Unit => TypeShape :: Unit ,
91
100
}
92
101
}
93
102
}
@@ -116,6 +125,17 @@ impl fmt::Display for Access {
116
125
}
117
126
}
118
127
128
+ impl Access {
129
+ pub ( super ) fn as_ref ( & self ) -> AccessRef < ' _ > {
130
+ match self {
131
+ Self :: Field ( value) => AccessRef :: Field ( value) ,
132
+ Self :: FieldIndex ( value) => AccessRef :: FieldIndex ( * value) ,
133
+ Self :: TupleIndex ( value) => AccessRef :: TupleIndex ( * value) ,
134
+ Self :: ListIndex ( value) => AccessRef :: ListIndex ( * value) ,
135
+ }
136
+ }
137
+ }
138
+
119
139
/// A singular borrowed element access within a path.
120
140
///
121
141
/// Can be applied to a `dyn Reflect` to get a reference to the targeted element.
@@ -130,17 +150,6 @@ pub(super) enum AccessRef<'a> {
130
150
ListIndex ( usize ) ,
131
151
}
132
152
133
- impl Access {
134
- pub ( super ) fn as_ref ( & self ) -> AccessRef < ' _ > {
135
- match self {
136
- Self :: Field ( value) => AccessRef :: Field ( value) ,
137
- Self :: FieldIndex ( value) => AccessRef :: FieldIndex ( * value) ,
138
- Self :: TupleIndex ( value) => AccessRef :: TupleIndex ( * value) ,
139
- Self :: ListIndex ( value) => AccessRef :: ListIndex ( * value) ,
140
- }
141
- }
142
- }
143
-
144
153
impl < ' a > AccessRef < ' a > {
145
154
pub ( super ) fn to_owned ( self ) -> Access {
146
155
match self {
@@ -175,29 +184,30 @@ impl<'a> AccessRef<'a> {
175
184
. and_then ( |maybe| maybe. ok_or ( Error :: Access { ty, access : self } ) )
176
185
. map_err ( |err| err. with_offset ( offset) )
177
186
}
187
+
178
188
fn element_inner ( self , base : & dyn Reflect ) -> InnerResult < & dyn Reflect > {
179
189
use ReflectRef :: * ;
180
190
match ( self , base. reflect_ref ( ) ) {
181
191
( Self :: Field ( field) , Struct ( struct_ref) ) => Ok ( struct_ref. field ( field) ) ,
182
192
( Self :: Field ( field) , Enum ( enum_ref) ) => match enum_ref. variant_type ( ) {
183
193
VariantType :: Struct => Ok ( enum_ref. field ( field) ) ,
184
- actual => Err ( Error :: bad_enum ( Type :: Struct , actual) ) ,
194
+ actual => Err ( Error :: bad_enum_variant ( TypeShape :: Struct , actual) ) ,
185
195
} ,
186
196
( Self :: FieldIndex ( index) , Struct ( struct_ref) ) => Ok ( struct_ref. field_at ( index) ) ,
187
197
( Self :: FieldIndex ( index) , Enum ( enum_ref) ) => match enum_ref. variant_type ( ) {
188
198
VariantType :: Struct => Ok ( enum_ref. field_at ( index) ) ,
189
- actual => Err ( Error :: bad_enum ( Type :: Struct , actual) ) ,
199
+ actual => Err ( Error :: bad_enum_variant ( TypeShape :: Struct , actual) ) ,
190
200
} ,
191
201
( Self :: TupleIndex ( index) , TupleStruct ( tuple) ) => Ok ( tuple. field ( index) ) ,
192
202
( Self :: TupleIndex ( index) , Tuple ( tuple) ) => Ok ( tuple. field ( index) ) ,
193
203
( Self :: TupleIndex ( index) , Enum ( enum_ref) ) => match enum_ref. variant_type ( ) {
194
204
VariantType :: Tuple => Ok ( enum_ref. field_at ( index) ) ,
195
- actual => Err ( Error :: bad_enum ( Type :: Tuple , actual) ) ,
205
+ actual => Err ( Error :: bad_enum_variant ( TypeShape :: Tuple , actual) ) ,
196
206
} ,
197
207
( Self :: ListIndex ( index) , List ( list) ) => Ok ( list. get ( index) ) ,
198
208
( Self :: ListIndex ( index) , Array ( list) ) => Ok ( list. get ( index) ) ,
199
- ( Self :: ListIndex ( _) , actual) => Err ( Error :: bad_type ( Type :: List , actual) ) ,
200
- ( _, actual) => Err ( Error :: bad_type ( Type :: Struct , actual) ) ,
209
+ ( Self :: ListIndex ( _) , actual) => Err ( Error :: bad_type ( TypeShape :: List , actual) ) ,
210
+ ( _, actual) => Err ( Error :: bad_type ( TypeShape :: Struct , actual) ) ,
201
211
}
202
212
}
203
213
@@ -211,30 +221,31 @@ impl<'a> AccessRef<'a> {
211
221
. and_then ( |maybe| maybe. ok_or ( Error :: Access { ty, access : self } ) )
212
222
. map_err ( |err| err. with_offset ( offset) )
213
223
}
224
+
214
225
fn element_inner_mut ( self , base : & mut dyn Reflect ) -> InnerResult < & mut dyn Reflect > {
215
226
use ReflectMut :: * ;
216
- let base_kind: Type = base. reflect_ref ( ) . into ( ) ;
227
+ let base_kind: TypeShape = base. reflect_ref ( ) . into ( ) ;
217
228
match ( self , base. reflect_mut ( ) ) {
218
229
( Self :: Field ( field) , Struct ( struct_mut) ) => Ok ( struct_mut. field_mut ( field) ) ,
219
230
( Self :: Field ( field) , Enum ( enum_mut) ) => match enum_mut. variant_type ( ) {
220
231
VariantType :: Struct => Ok ( enum_mut. field_mut ( field) ) ,
221
- actual => Err ( Error :: bad_enum ( Type :: Struct , actual) ) ,
232
+ actual => Err ( Error :: bad_enum_variant ( TypeShape :: Struct , actual) ) ,
222
233
} ,
223
234
( Self :: FieldIndex ( index) , Struct ( struct_mut) ) => Ok ( struct_mut. field_at_mut ( index) ) ,
224
235
( Self :: FieldIndex ( index) , Enum ( enum_mut) ) => match enum_mut. variant_type ( ) {
225
236
VariantType :: Struct => Ok ( enum_mut. field_at_mut ( index) ) ,
226
- actual => Err ( Error :: bad_enum ( Type :: Struct , actual) ) ,
237
+ actual => Err ( Error :: bad_enum_variant ( TypeShape :: Struct , actual) ) ,
227
238
} ,
228
239
( Self :: TupleIndex ( index) , TupleStruct ( tuple) ) => Ok ( tuple. field_mut ( index) ) ,
229
240
( Self :: TupleIndex ( index) , Tuple ( tuple) ) => Ok ( tuple. field_mut ( index) ) ,
230
241
( Self :: TupleIndex ( index) , Enum ( enum_mut) ) => match enum_mut. variant_type ( ) {
231
242
VariantType :: Tuple => Ok ( enum_mut. field_at_mut ( index) ) ,
232
- actual => Err ( Error :: bad_enum ( Type :: Tuple , actual) ) ,
243
+ actual => Err ( Error :: bad_enum_variant ( TypeShape :: Tuple , actual) ) ,
233
244
} ,
234
245
( Self :: ListIndex ( index) , List ( list) ) => Ok ( list. get_mut ( index) ) ,
235
246
( Self :: ListIndex ( index) , Array ( list) ) => Ok ( list. get_mut ( index) ) ,
236
- ( Self :: ListIndex ( _) , _) => Err ( Error :: bad_type ( Type :: List , base_kind) ) ,
237
- ( _, _) => Err ( Error :: bad_type ( Type :: Struct , base_kind) ) ,
247
+ ( Self :: ListIndex ( _) , _) => Err ( Error :: bad_type ( TypeShape :: List , base_kind) ) ,
248
+ ( _, _) => Err ( Error :: bad_type ( TypeShape :: Struct , base_kind) ) ,
238
249
}
239
250
}
240
251
}
0 commit comments