Skip to content

Commit

Permalink
chore: naming
Browse files Browse the repository at this point in the history
ten3roberts committed Nov 13, 2024
1 parent 4624f61 commit bb63cf2
Showing 3 changed files with 46 additions and 15 deletions.
28 changes: 24 additions & 4 deletions src/serialize/de.rs
Original file line number Diff line number Diff line change
@@ -137,6 +137,25 @@ impl DeserializeContext {
deserializer.deserialize_enum("World", &["row", "col"], WorldVisitor { context: self })
}

/// Deserializes an entity into the provided builder
pub fn deserialize_entity<'de, D>(
&self,
deserializer: D,
builder: &mut EntityBuilder,
) -> core::result::Result<Entity, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_tuple_struct(
"Entity",
2,
EntityVisitor {
context: self,
builder,
},
)
}

fn get(&self, key: &str) -> Result<&Slot, String> {
self.slots
.get(key)
@@ -206,7 +225,7 @@ impl<'de, 'a> Visitor<'de> for DeserializeEntities<'a> {
A: SeqAccess<'de>,
{
let mut builder = EntityBuilder::new();
while let Some(id) = seq.next_element_seed(DeserializeEntity {
while let Some(id) = seq.next_element_seed(EntityVisitor {
context: self.context,
builder: &mut builder,
})? {
@@ -221,12 +240,12 @@ impl<'de, 'a> Visitor<'de> for DeserializeEntities<'a> {
}

/// (id, components)
struct DeserializeEntity<'a> {
struct EntityVisitor<'a> {
context: &'a DeserializeContext,
builder: &'a mut EntityBuilder,
}

impl<'de, 'a> DeserializeSeed<'de> for DeserializeEntity<'a> {
impl<'de, 'a> DeserializeSeed<'de> for EntityVisitor<'a> {
type Value = Entity;

fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
@@ -237,7 +256,7 @@ impl<'de, 'a> DeserializeSeed<'de> for DeserializeEntity<'a> {
}
}

impl<'de, 'a> Visitor<'de> for DeserializeEntity<'a> {
impl<'de, 'a> Visitor<'de> for EntityVisitor<'a> {
type Value = Entity;

fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
@@ -262,6 +281,7 @@ impl<'de, 'a> Visitor<'de> for DeserializeEntity<'a> {
}
}

/// Deserialize the entity data into the provided entity builder
struct DeserializeEntityData<'a> {
context: &'a DeserializeContext,
builder: &'a mut EntityBuilder,
14 changes: 7 additions & 7 deletions src/serialize/mod.rs
Original file line number Diff line number Diff line change
@@ -48,12 +48,12 @@ pub enum SerializeFormat {

/// Allows constructing a serialize and deserialize context with the same
/// supported types allowing for easier roundtrips.
pub struct SerdeBuilder<F = All> {
pub struct SerializationContextBuilder<F = All> {
ser: SerializeBuilder<F>,
de: DeserializeBuilder,
}

impl SerdeBuilder {
impl SerializationContextBuilder {
/// Creates a new builder which simultaneously constructs a serialialization
/// and deserialization context
pub fn new() -> Self {
@@ -64,13 +64,13 @@ impl SerdeBuilder {
}
}

impl Default for SerdeBuilder {
impl Default for SerializationContextBuilder {
fn default() -> Self {
Self::new()
}
}

impl<F> SerdeBuilder<F>
impl<F> SerializationContextBuilder<F>
where
F: StaticFilter + 'static + Clone,
{
@@ -96,8 +96,8 @@ where
}

/// Add a new filter to specify which entities will be serialized.
pub fn with_filter<G>(self, filter: G) -> SerdeBuilder<And<F, G>> {
SerdeBuilder {
pub fn with_filter<G>(self, filter: G) -> SerializationContextBuilder<And<F, G>> {
SerializationContextBuilder {
ser: self.ser.with_filter(filter),
de: self.de,
}
@@ -200,7 +200,7 @@ mod test {
}
};

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(name())
.with(health())
.with(pos())
19 changes: 15 additions & 4 deletions src/serialize/ser.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use crate::{
component::{ComponentKey, ComponentValue},
components::component_info,
filter::{All, And, StaticFilter},
Component, Entity, World,
Component, Entity, EntityRef, World,
};

use alloc::{boxed::Box, collections::BTreeMap, string::String};
@@ -127,6 +127,16 @@ impl SerializeContext {
}
}

/// Serialize a single entity
pub fn serialize_entity<'a>(&'a self, entity: &'a EntityRef) -> EntitySerializer {
EntitySerializer {
slot: entity.loc.slot,
arch: entity.arch,
id: entity.id,
context: self,
}
}

fn archetypes<'a>(
&'a self,
world: &'a World,
@@ -202,7 +212,7 @@ impl<'a> Serialize for SerializeEntities<'a> {

for (_, arch) in self.context.archetypes(self.world) {
for slot in arch.slots() {
seq.serialize_element(&SerializeEntity {
seq.serialize_element(&EntitySerializer {
slot,
arch,
id: arch.entity(slot).expect("Invalid slot"),
@@ -215,14 +225,15 @@ impl<'a> Serialize for SerializeEntities<'a> {
}
}

struct SerializeEntity<'a> {
/// Serializes an entity
pub struct EntitySerializer<'a> {
slot: usize,
arch: &'a Archetype,
id: Entity,
context: &'a SerializeContext,
}

impl<'a> Serialize for SerializeEntity<'a> {
impl<'a> Serialize for EntitySerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,

0 comments on commit bb63cf2

Please sign in to comment.