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