use amethyst::ecs::world::Generation;
use amethyst::ecs::Entity;
use serde::ser::SerializeStruct;
use serde::Serialize;
use serde::Serializer;
use std::fmt::{self, Debug, Formatter};
#[derive(Clone, Copy)]
pub struct SerializableEntity(pub Entity);
impl SerializableEntity {
pub fn new(entity: Entity) -> Self {
SerializableEntity(entity)
}
pub fn id(self) -> u32 {
self.0.id()
}
pub fn gen(self) -> Generation {
self.0.gen()
}
}
impl Serialize for SerializableEntity {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("Entity", 2)?;
state.serialize_field("id", &self.0.id())?;
state.serialize_field("generation", &self.0.gen().id())?;
state.end()
}
}
impl Debug for SerializableEntity {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), fmt::Error> {
self.0.fmt(formatter)
}
}
impl From<Entity> for SerializableEntity {
fn from(from: Entity) -> Self {
SerializableEntity(from)
}
}
impl From<SerializableEntity> for Entity {
fn from(from: SerializableEntity) -> Self {
from.0
}
}
#[derive(Debug, Clone, Copy, Deserialize)]
pub struct DeserializableEntity {
pub(crate) id: u32,
pub(crate) generation: i32,
}