bevy/crates/bevy_scene/src/serde.rs
Carter Anderson 6eea96366d cargo fmt
2020-06-03 20:08:20 -07:00

248 lines
7 KiB
Rust

use crate::{Entity, Scene};
use anyhow::Result;
use bevy_property::{
property_serde::{DynamicPropertiesDeserializer, DynamicPropertiesSerializer},
DynamicProperties, PropertyTypeRegistry,
};
use serde::{
de::{DeserializeSeed, Error, MapAccess, SeqAccess, Visitor},
ser::{SerializeSeq, SerializeStruct},
Deserialize, Serialize,
};
pub struct SceneSerializer<'a> {
pub scene: &'a Scene,
pub registry: &'a PropertyTypeRegistry,
}
impl<'a> SceneSerializer<'a> {
pub fn new(scene: &'a Scene, registry: &'a PropertyTypeRegistry) -> Self {
SceneSerializer { scene, registry }
}
}
impl<'a> Serialize for SceneSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_seq(Some(self.scene.entities.len()))?;
for entity in self.scene.entities.iter() {
state.serialize_element(&EntitySerializer {
entity,
registry: self.registry,
})?;
}
state.end()
}
}
pub struct EntitySerializer<'a> {
pub entity: &'a Entity,
pub registry: &'a PropertyTypeRegistry,
}
impl<'a> Serialize for EntitySerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_struct(ENTITY_STRUCT, 2)?;
state.serialize_field(ENTITY_FIELD_ENTITY, &self.entity.entity)?;
state.serialize_field(
ENTITY_FIELD_COMPONENTS,
&ComponentsSerializer {
components: &self.entity.components,
registry: self.registry,
},
)?;
state.end()
}
}
pub struct ComponentsSerializer<'a> {
pub components: &'a [DynamicProperties],
pub registry: &'a PropertyTypeRegistry,
}
impl<'a> Serialize for ComponentsSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_seq(Some(self.components.len()))?;
for dynamic_properties in self.components.iter() {
state.serialize_element(&DynamicPropertiesSerializer::new(
dynamic_properties,
self.registry,
))?;
}
state.end()
}
}
pub struct SceneDeserializer<'a> {
pub property_type_registry: &'a PropertyTypeRegistry,
}
impl<'a, 'de> DeserializeSeed<'de> for SceneDeserializer<'a> {
type Value = Scene;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut scene = Scene::default();
scene.entities = deserializer.deserialize_seq(SceneEntitySeqVisiter {
property_type_registry: self.property_type_registry,
})?;
Ok(scene)
}
}
struct SceneEntitySeqVisiter<'a> {
pub property_type_registry: &'a PropertyTypeRegistry,
}
impl<'a, 'de> Visitor<'de> for SceneEntitySeqVisiter<'a> {
type Value = Vec<Entity>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("list of entities")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut entities = Vec::new();
while let Some(entity) = seq.next_element_seed(SceneEntityDeserializer {
property_type_registry: self.property_type_registry,
})? {
entities.push(entity);
}
Ok(entities)
}
}
pub struct SceneEntityDeserializer<'a> {
pub property_type_registry: &'a PropertyTypeRegistry,
}
impl<'a, 'de> DeserializeSeed<'de> for SceneEntityDeserializer<'a> {
type Value = Entity;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_struct(
ENTITY_STRUCT,
&[ENTITY_FIELD_ENTITY, ENTITY_FIELD_COMPONENTS],
SceneEntityVisiter {
registry: self.property_type_registry,
},
)
}
}
#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
enum EntityField {
Entity,
Components,
}
pub const ENTITY_STRUCT: &str = "Entity";
pub const ENTITY_FIELD_ENTITY: &str = "entity";
pub const ENTITY_FIELD_COMPONENTS: &str = "components";
struct SceneEntityVisiter<'a> {
pub registry: &'a PropertyTypeRegistry,
}
impl<'a, 'de> Visitor<'de> for SceneEntityVisiter<'a> {
type Value = Entity;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("entities")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut id = None;
let mut components = None;
while let Some(key) = map.next_key()? {
match key {
EntityField::Entity => {
if id.is_some() {
return Err(Error::duplicate_field(ENTITY_FIELD_ENTITY));
}
id = Some(map.next_value::<u32>()?);
}
EntityField::Components => {
if components.is_some() {
return Err(Error::duplicate_field(ENTITY_FIELD_COMPONENTS));
}
components = Some(map.next_value_seed(ComponentVecDeserializer {
registry: self.registry,
})?);
}
}
}
let entity = id
.as_ref()
.ok_or_else(|| Error::missing_field(ENTITY_FIELD_ENTITY))?;
let components = components
.take()
.ok_or_else(|| Error::missing_field(ENTITY_FIELD_COMPONENTS))?;
Ok(Entity {
entity: *entity,
components,
})
}
}
pub struct ComponentVecDeserializer<'a> {
pub registry: &'a PropertyTypeRegistry,
}
impl<'a, 'de> DeserializeSeed<'de> for ComponentVecDeserializer<'a> {
type Value = Vec<DynamicProperties>;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_seq(ComponentSeqVisiter {
registry: self.registry,
})
}
}
struct ComponentSeqVisiter<'a> {
pub registry: &'a PropertyTypeRegistry,
}
impl<'a, 'de> Visitor<'de> for ComponentSeqVisiter<'a> {
type Value = Vec<DynamicProperties>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("list of components")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut dynamic_properties = Vec::new();
while let Some(entity) =
seq.next_element_seed(DynamicPropertiesDeserializer::new(self.registry))?
{
dynamic_properties.push(entity);
}
Ok(dynamic_properties)
}
}