Path: blob/main/crates/bevy_reflect/src/serde/de/registrations.rs
6600 views
use crate::{serde::de::error_utils::make_custom_error, TypeRegistration, TypeRegistry};1use core::{fmt, fmt::Formatter};2use serde::de::{DeserializeSeed, Error, Visitor};34/// A deserializer for type registrations.5///6/// This will return a [`&TypeRegistration`] corresponding to the given type.7/// This deserializer expects a string containing the _full_ [type path] of the8/// type to find the `TypeRegistration` of.9///10/// [`&TypeRegistration`]: TypeRegistration11/// [type path]: crate::TypePath::type_path12pub struct TypeRegistrationDeserializer<'a> {13registry: &'a TypeRegistry,14}1516impl<'a> TypeRegistrationDeserializer<'a> {17/// Creates a new [`TypeRegistrationDeserializer`].18pub fn new(registry: &'a TypeRegistry) -> Self {19Self { registry }20}21}2223impl<'a, 'de> DeserializeSeed<'de> for TypeRegistrationDeserializer<'a> {24type Value = &'a TypeRegistration;2526fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>27where28D: serde::Deserializer<'de>,29{30struct TypeRegistrationVisitor<'a>(&'a TypeRegistry);3132impl<'de, 'a> Visitor<'de> for TypeRegistrationVisitor<'a> {33type Value = &'a TypeRegistration;3435fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {36formatter.write_str("string containing `type` entry for the reflected value")37}3839fn visit_str<E>(self, type_path: &str) -> Result<Self::Value, E>40where41E: Error,42{43self.0.get_with_type_path(type_path).ok_or_else(|| {44make_custom_error(format_args!("no registration found for `{type_path}`"))45})46}47}4849deserializer.deserialize_str(TypeRegistrationVisitor(self.registry))50}51}525354