bevy_reflect/serde/de/
registrations.rs1use crate::{serde::de::error_utils::make_custom_error, TypeRegistration, TypeRegistry};
2use core::{fmt, fmt::Formatter};
3use serde::de::{DeserializeSeed, Error, Visitor};
4
5pub struct TypeRegistrationDeserializer<'a> {
14 registry: &'a TypeRegistry,
15}
16
17impl<'a> TypeRegistrationDeserializer<'a> {
18 pub fn new(registry: &'a TypeRegistry) -> Self {
20 Self { registry }
21 }
22}
23
24impl<'a, 'de> DeserializeSeed<'de> for TypeRegistrationDeserializer<'a> {
25 type Value = &'a TypeRegistration;
26
27 fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
28 where
29 D: serde::Deserializer<'de>,
30 {
31 struct TypeRegistrationVisitor<'a>(&'a TypeRegistry);
32
33 impl<'de, 'a> Visitor<'de> for TypeRegistrationVisitor<'a> {
34 type Value = &'a TypeRegistration;
35
36 fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
37 formatter.write_str("string containing `type` entry for the reflected value")
38 }
39
40 fn visit_str<E>(self, type_path: &str) -> Result<Self::Value, E>
41 where
42 E: Error,
43 {
44 self.0.get_with_type_path(type_path).ok_or_else(|| {
45 make_custom_error(format_args!("no registration found for `{type_path}`"))
46 })
47 }
48 }
49
50 deserializer.deserialize_str(TypeRegistrationVisitor(self.registry))
51 }
52}