Skip to content

Commit

Permalink
chore: naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Nov 13, 2024
1 parent 4624f61 commit bb63cf2
Show file tree
Hide file tree
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
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
})? {
Expand All @@ -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>
Expand All @@ -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 {
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions src/serialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
{
Expand All @@ -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,
}
Expand Down Expand Up @@ -200,7 +200,7 @@ mod test {
}
};

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(name())
.with(health())
.with(pos())
Expand Down
19 changes: 15 additions & 4 deletions src/serialize/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
Expand All @@ -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,
Expand Down

0 comments on commit bb63cf2

Please sign in to comment.