Skip to main content

extendable_assets/asset/serialize/
backend.rs

1use crate::asset::serialize::SerializedAsset;
2
3/// Trait for implementing different asset serialization backends.
4///
5/// This trait allows for pluggable serialization strategies for assets,
6/// enabling support for different formats like JSON, MessagePack, bincode, etc.
7/// Implementations must be thread-safe (Send + Sync).
8pub trait AssetSerializationBackend: Send + Sync {
9    /// Serializes a SerializedAsset into bytes using the backend's format.
10    ///
11    /// # Arguments
12    /// * `asset` - The asset to serialize
13    ///
14    /// # Returns
15    /// The serialized bytes on success, or an error if serialization fails
16    fn serialize(&self, asset: &SerializedAsset) -> anyhow::Result<Vec<u8>>;
17
18    /// Deserializes bytes back into a SerializedAsset using the backend's format.
19    ///
20    /// # Arguments
21    /// * `bytes` - The serialized data to deserialize
22    ///
23    /// # Returns
24    /// The deserialized SerializedAsset on success, or an error if deserialization fails
25    fn deserialize(&self, bytes: &[u8]) -> anyhow::Result<SerializedAsset>;
26}
27
28/// A null implementation of AssetSerializationBackend that always returns errors.
29///
30/// This backend is useful as a placeholder or for testing error conditions.
31/// All operations will fail with an "Unimplemented" error.
32pub struct NullSerializationBackend;
33impl AssetSerializationBackend for NullSerializationBackend {
34    /// Always returns an error - this backend does not implement serialization.
35    fn serialize(&self, _asset: &SerializedAsset) -> anyhow::Result<Vec<u8>> {
36        Err(anyhow::anyhow!(
37            "Unimplemented for NullSerializationBackend"
38        ))
39    }
40
41    /// Always returns an error - this backend does not implement deserialization.
42    fn deserialize(&self, _bytes: &[u8]) -> anyhow::Result<SerializedAsset> {
43        Err(anyhow::anyhow!(
44            "Unimplemented for NullSerializationBackend"
45        ))
46    }
47}