Skip to content

Commit

Permalink
Reorganize code
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Mar 26, 2024
1 parent 106a298 commit abd1d60
Showing 1 changed file with 72 additions and 71 deletions.
143 changes: 72 additions & 71 deletions crates/bevy_reflect/src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,54 @@ impl<'de> Deserialize<'de> for Ident {
}
}

/// A deserializer for type registrations.
///
/// This will return a [`&TypeRegistration`] corresponding to the given type.
/// This deserializer expects a string containing the _full_ [type path] of the
/// type to find the `TypeRegistration` of.
///
/// [`&TypeRegistration`]: TypeRegistration
/// [type path]: crate::TypePath::type_path
pub struct TypeRegistrationDeserializer<'a> {
registry: &'a TypeRegistry,
}

impl<'a> TypeRegistrationDeserializer<'a> {
pub fn new(registry: &'a TypeRegistry) -> Self {
Self { registry }
}
}

impl<'a, 'de> DeserializeSeed<'de> for TypeRegistrationDeserializer<'a> {
type Value = &'a TypeRegistration;

fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
struct TypeRegistrationVisitor<'a>(&'a TypeRegistry);

impl<'de, 'a> Visitor<'de> for TypeRegistrationVisitor<'a> {
type Value = &'a TypeRegistration;

fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
formatter.write_str("string containing `type` entry for the reflected value")
}

fn visit_str<E>(self, type_path: &str) -> Result<Self::Value, E>
where
E: Error,
{
self.0.get_with_type_path(type_path).ok_or_else(|| {
Error::custom(format_args!("No registration found for `{type_path}`"))
})
}
}

deserializer.deserialize_str(TypeRegistrationVisitor(self.registry))
}
}

/// A general purpose deserializer for reflected types.
///
/// This is the deserializer counterpart to [`ReflectSerializer`].
Expand Down Expand Up @@ -336,89 +384,42 @@ impl<'a, 'de> DeserializeSeed<'de> for ReflectDeserializer<'a> {
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_map(UntypedReflectDeserializerVisitor {
registry: self.registry,
})
}
}

/// A deserializer for type registrations.
///
/// This will return a [`&TypeRegistration`] corresponding to the given type.
/// This deserializer expects a string containing the _full_ [type path] of the
/// type to find the `TypeRegistration` of.
///
/// [`&TypeRegistration`]: TypeRegistration
/// [type path]: crate::TypePath::type_path
pub struct TypeRegistrationDeserializer<'a> {
registry: &'a TypeRegistry,
}

impl<'a> TypeRegistrationDeserializer<'a> {
pub fn new(registry: &'a TypeRegistry) -> Self {
Self { registry }
}
}

impl<'a, 'de> DeserializeSeed<'de> for TypeRegistrationDeserializer<'a> {
type Value = &'a TypeRegistration;

fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
struct TypeRegistrationVisitor<'a>(&'a TypeRegistry);
struct UntypedReflectDeserializerVisitor<'a> {
registry: &'a TypeRegistry,
}

impl<'de, 'a> Visitor<'de> for TypeRegistrationVisitor<'a> {
type Value = &'a TypeRegistration;
impl<'a, 'de> Visitor<'de> for UntypedReflectDeserializerVisitor<'a> {
type Value = Box<dyn Reflect>;

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

fn visit_str<E>(self, type_path: &str) -> Result<Self::Value, E>
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
E: Error,
A: MapAccess<'de>,
{
self.0.get_with_type_path(type_path).ok_or_else(|| {
Error::custom(format_args!("No registration found for `{type_path}`"))
})
}
}

deserializer.deserialize_str(TypeRegistrationVisitor(self.registry))
}
}
let registration = map
.next_key_seed(TypeRegistrationDeserializer::new(self.registry))?
.ok_or_else(|| Error::invalid_length(0, &"a single entry"))?;

struct UntypedReflectDeserializerVisitor<'a> {
registry: &'a TypeRegistry,
}

impl<'a, 'de> Visitor<'de> for UntypedReflectDeserializerVisitor<'a> {
type Value = Box<dyn Reflect>;

fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
formatter.write_str("map containing `type` and `value` entries for the reflected value")
}

fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let registration = map
.next_key_seed(TypeRegistrationDeserializer::new(self.registry))?
.ok_or_else(|| Error::invalid_length(0, &"a single entry"))?;
let value = map.next_value_seed(TypedReflectDeserializer {
registration,
registry: self.registry,
})?;

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

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

Ok(value)
deserializer.deserialize_map(UntypedReflectDeserializerVisitor {
registry: self.registry,
})
}
}

Expand Down

0 comments on commit abd1d60

Please sign in to comment.