-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathschematics.rs
148 lines (121 loc) · 4.51 KB
/
schematics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::fmt::Formatter;
use bevy::reflect::serde::{TypeRegistrationDeserializer, TypedReflectDeserializer};
use bevy::reflect::TypeRegistryInternal;
use serde::de::{DeserializeSeed, Error, MapAccess, Visitor};
use serde::Deserializer;
use bevy_proto_backend::schematics::{ReflectSchematic, Schematics};
pub(crate) struct SchematicsDeserializer<'a> {
registry: &'a TypeRegistryInternal,
}
impl<'a> SchematicsDeserializer<'a> {
pub fn new(registry: &'a TypeRegistryInternal) -> Self {
Self { registry }
}
}
impl<'de, 'a> DeserializeSeed<'de> for SchematicsDeserializer<'a> {
type Value = Schematics;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
struct SchematicsVisitor<'a> {
registry: &'a TypeRegistryInternal,
}
impl<'de, 'a> Visitor<'de> for SchematicsVisitor<'a> {
type Value = Schematics;
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
write!(formatter, "map of schematics")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let size_hint = map.size_hint().unwrap_or_default();
let mut schematics = Schematics::with_capacity(size_hint);
while let Some(registration) =
map.next_key_seed(TypeRegistrationDeserializer::new(self.registry))?
{
if schematics.contains_by_name(registration.type_name()) {
return Err(Error::custom(format_args!(
"duplicate schematic: `{}`",
registration.type_name()
)));
}
let reflect_schematic =
registration.data::<ReflectSchematic>().ok_or_else(|| {
Error::custom(format_args!(
"missing `ReflectSchematic` registration for schematic: `{}`",
registration.type_name()
))
})?;
let input_registration = reflect_schematic.input_registration();
let input = map.next_value_seed(TypedReflectDeserializer::new(
&input_registration,
self.registry,
))?;
let schematic = reflect_schematic
.create_dynamic(input)
.map_err(Error::custom)?;
schematics.insert_dynamic(schematic);
}
Ok(schematics)
}
}
deserializer.deserialize_map(SchematicsVisitor {
registry: self.registry,
})
}
}
#[cfg(test)]
mod tests {
use bevy::prelude::Component;
use bevy::reflect::{Reflect, TypeRegistryInternal};
use bevy_proto_backend::schematics::Schematic;
use super::*;
#[derive(Reflect, Component, Schematic, Eq, PartialEq, Debug)]
struct MySchematic {
foo: usize,
}
#[test]
fn should_deserialize_schematics() {
let mut registry = TypeRegistryInternal::new();
registry.register::<MySchematic>();
registry.register_type_data::<MySchematic, ReflectSchematic>();
let input = r#"
{
"bevy_proto::schematics::tests::MySchematic": (
foo: 123
)
}"#;
let deserializer = SchematicsDeserializer::new(®istry);
let schematics = deserializer
.deserialize(&mut ron::de::Deserializer::from_str(input).unwrap())
.unwrap();
assert_eq!(1, schematics.len());
assert_eq!(
&MySchematic { foo: 123 },
schematics
.get::<MySchematic>()
.unwrap()
.input()
.downcast_ref::<MySchematic>()
.unwrap()
);
}
#[test]
#[should_panic(expected = "missing `ReflectSchematic` registration for schematic")]
fn should_not_deserialize_schematics() {
let mut registry = TypeRegistryInternal::new();
registry.register::<MySchematic>();
let input = r#"
{
"bevy_proto::schematics::tests::MySchematic": (
foo: 123
)
}"#;
let deserializer = SchematicsDeserializer::new(®istry);
deserializer
.deserialize(&mut ron::de::Deserializer::from_str(input).unwrap())
.unwrap();
}
}