Skip to content

Commit abd1d60

Browse files
committed
Reorganize code
1 parent 106a298 commit abd1d60

File tree

1 file changed

+72
-71
lines changed
  • crates/bevy_reflect/src/serde

1 file changed

+72
-71
lines changed

crates/bevy_reflect/src/serde/de.rs

Lines changed: 72 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,54 @@ impl<'de> Deserialize<'de> for Ident {
240240
}
241241
}
242242

243+
/// A deserializer for type registrations.
244+
///
245+
/// This will return a [`&TypeRegistration`] corresponding to the given type.
246+
/// This deserializer expects a string containing the _full_ [type path] of the
247+
/// type to find the `TypeRegistration` of.
248+
///
249+
/// [`&TypeRegistration`]: TypeRegistration
250+
/// [type path]: crate::TypePath::type_path
251+
pub struct TypeRegistrationDeserializer<'a> {
252+
registry: &'a TypeRegistry,
253+
}
254+
255+
impl<'a> TypeRegistrationDeserializer<'a> {
256+
pub fn new(registry: &'a TypeRegistry) -> Self {
257+
Self { registry }
258+
}
259+
}
260+
261+
impl<'a, 'de> DeserializeSeed<'de> for TypeRegistrationDeserializer<'a> {
262+
type Value = &'a TypeRegistration;
263+
264+
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
265+
where
266+
D: serde::Deserializer<'de>,
267+
{
268+
struct TypeRegistrationVisitor<'a>(&'a TypeRegistry);
269+
270+
impl<'de, 'a> Visitor<'de> for TypeRegistrationVisitor<'a> {
271+
type Value = &'a TypeRegistration;
272+
273+
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
274+
formatter.write_str("string containing `type` entry for the reflected value")
275+
}
276+
277+
fn visit_str<E>(self, type_path: &str) -> Result<Self::Value, E>
278+
where
279+
E: Error,
280+
{
281+
self.0.get_with_type_path(type_path).ok_or_else(|| {
282+
Error::custom(format_args!("No registration found for `{type_path}`"))
283+
})
284+
}
285+
}
286+
287+
deserializer.deserialize_str(TypeRegistrationVisitor(self.registry))
288+
}
289+
}
290+
243291
/// A general purpose deserializer for reflected types.
244292
///
245293
/// This is the deserializer counterpart to [`ReflectSerializer`].
@@ -336,89 +384,42 @@ impl<'a, 'de> DeserializeSeed<'de> for ReflectDeserializer<'a> {
336384
where
337385
D: serde::Deserializer<'de>,
338386
{
339-
deserializer.deserialize_map(UntypedReflectDeserializerVisitor {
340-
registry: self.registry,
341-
})
342-
}
343-
}
344-
345-
/// A deserializer for type registrations.
346-
///
347-
/// This will return a [`&TypeRegistration`] corresponding to the given type.
348-
/// This deserializer expects a string containing the _full_ [type path] of the
349-
/// type to find the `TypeRegistration` of.
350-
///
351-
/// [`&TypeRegistration`]: TypeRegistration
352-
/// [type path]: crate::TypePath::type_path
353-
pub struct TypeRegistrationDeserializer<'a> {
354-
registry: &'a TypeRegistry,
355-
}
356-
357-
impl<'a> TypeRegistrationDeserializer<'a> {
358-
pub fn new(registry: &'a TypeRegistry) -> Self {
359-
Self { registry }
360-
}
361-
}
362-
363-
impl<'a, 'de> DeserializeSeed<'de> for TypeRegistrationDeserializer<'a> {
364-
type Value = &'a TypeRegistration;
365-
366-
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
367-
where
368-
D: serde::Deserializer<'de>,
369-
{
370-
struct TypeRegistrationVisitor<'a>(&'a TypeRegistry);
387+
struct UntypedReflectDeserializerVisitor<'a> {
388+
registry: &'a TypeRegistry,
389+
}
371390

372-
impl<'de, 'a> Visitor<'de> for TypeRegistrationVisitor<'a> {
373-
type Value = &'a TypeRegistration;
391+
impl<'a, 'de> Visitor<'de> for UntypedReflectDeserializerVisitor<'a> {
392+
type Value = Box<dyn Reflect>;
374393

375394
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
376-
formatter.write_str("string containing `type` entry for the reflected value")
395+
formatter
396+
.write_str("map containing `type` and `value` entries for the reflected value")
377397
}
378398

379-
fn visit_str<E>(self, type_path: &str) -> Result<Self::Value, E>
399+
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
380400
where
381-
E: Error,
401+
A: MapAccess<'de>,
382402
{
383-
self.0.get_with_type_path(type_path).ok_or_else(|| {
384-
Error::custom(format_args!("No registration found for `{type_path}`"))
385-
})
386-
}
387-
}
388-
389-
deserializer.deserialize_str(TypeRegistrationVisitor(self.registry))
390-
}
391-
}
403+
let registration = map
404+
.next_key_seed(TypeRegistrationDeserializer::new(self.registry))?
405+
.ok_or_else(|| Error::invalid_length(0, &"a single entry"))?;
392406

393-
struct UntypedReflectDeserializerVisitor<'a> {
394-
registry: &'a TypeRegistry,
395-
}
396-
397-
impl<'a, 'de> Visitor<'de> for UntypedReflectDeserializerVisitor<'a> {
398-
type Value = Box<dyn Reflect>;
399-
400-
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
401-
formatter.write_str("map containing `type` and `value` entries for the reflected value")
402-
}
403-
404-
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
405-
where
406-
A: MapAccess<'de>,
407-
{
408-
let registration = map
409-
.next_key_seed(TypeRegistrationDeserializer::new(self.registry))?
410-
.ok_or_else(|| Error::invalid_length(0, &"a single entry"))?;
407+
let value = map.next_value_seed(TypedReflectDeserializer {
408+
registration,
409+
registry: self.registry,
410+
})?;
411411

412-
let value = map.next_value_seed(TypedReflectDeserializer {
413-
registration,
414-
registry: self.registry,
415-
})?;
412+
if map.next_key::<IgnoredAny>()?.is_some() {
413+
return Err(Error::invalid_length(2, &"a single entry"));
414+
}
416415

417-
if map.next_key::<IgnoredAny>()?.is_some() {
418-
return Err(Error::invalid_length(2, &"a single entry"));
416+
Ok(value)
417+
}
419418
}
420419

421-
Ok(value)
420+
deserializer.deserialize_map(UntypedReflectDeserializerVisitor {
421+
registry: self.registry,
422+
})
422423
}
423424
}
424425

0 commit comments

Comments
 (0)