khora_lanes/scene_lane/strategies/mod.rs
1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Defines the abstract contract for serialization strategies and their associated types.
16//!
17//! The core of this module is the [`SerializationStrategy`] trait, which provides
18//! a unified interface for all serialization `Lanes`. This allows the `SerializationAgent`
19//! to manage and dispatch tasks to different strategies polymorphically.
20
21mod archetype_lane;
22mod definition_lane;
23mod recipe_lane;
24
25pub use archetype_lane::*;
26pub use definition_lane::*;
27pub use recipe_lane::*;
28
29use khora_data::ecs::World;
30use std::fmt;
31
32/// An error that can occur during the serialization process.
33#[derive(Debug)]
34pub enum SerializationError {
35 /// Indicates a failure during I/O or data conversion.
36 ProcessingFailed(String),
37}
38
39/// An error that can occur during the deserialization process.
40#[derive(Debug)]
41pub enum DeserializationError {
42 /// The data is corrupted or does not match the expected format.
43 InvalidFormat(String),
44 /// A failure occurred while populating the world.
45 WorldPopulationFailed(String),
46}
47
48impl fmt::Display for SerializationError {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 match self {
51 SerializationError::ProcessingFailed(msg) => {
52 write!(f, "Serialization failed: {}", msg)
53 }
54 }
55 }
56}
57
58impl fmt::Display for DeserializationError {
59 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60 match self {
61 DeserializationError::InvalidFormat(msg) => {
62 write!(f, "Deserialization failed: Invalid format - {}", msg)
63 }
64 DeserializationError::WorldPopulationFailed(msg) => {
65 write!(f, "Deserialization failed: World population - {}", msg)
66 }
67 }
68 }
69}
70
71/// The abstract contract for a scene serialization strategy `Lane`.
72///
73/// Each concrete implementation of this trait represents a different method
74/// of converting a `World` to and from a persistent format.
75pub trait SerializationStrategy: Send + Sync {
76 /// Returns the unique, versioned string identifier for this strategy.
77 ///
78 /// This ID is written to the `SceneHeader` and used by the `SerializationAgent`
79 /// to look up the correct strategy from its registry during deserialization.
80 /// Example: `"KH_RECIPE_V1"`.
81 fn get_strategy_id(&self) -> &'static str;
82
83 /// Serializes the given `World` into a byte payload.
84 ///
85 /// # Arguments
86 /// * `world` - A reference to the world to be serialized.
87 ///
88 /// # Returns
89 /// A `Result` containing the binary payload `Vec<u8>` or a `SerializationError`.
90 fn serialize(&self, world: &World) -> Result<Vec<u8>, SerializationError>;
91
92 /// Deserializes data from a byte payload to populate the given `World`.
93 ///
94 /// This method should assume the `SceneHeader` has already been parsed and validated,
95 /// and that `data` is the correct payload for this strategy.
96 ///
97 /// # Arguments
98 /// * `data` - The raw byte payload to deserialize.
99 /// * `world` - A mutable reference to the world to be populated.
100 ///
101 /// # Returns
102 /// A `Result` indicating success or a `DeserializationError`.
103 fn deserialize(&self, data: &[u8], world: &mut World) -> Result<(), DeserializationError>;
104}