extendable_assets/saver.rs
1use std::sync::Arc;
2
3use crate::asset::AssetData;
4use crate::manager::AssetManagerContext;
5
6use thiserror::Error;
7
8/// Errors that can occur during asset saving.
9#[derive(Debug, Error)]
10#[non_exhaustive]
11pub enum AssetSaveError {
12 /// The asset type is not supported by this saver.
13 #[error("Unsupported asset type")]
14 UnsupportedType,
15
16 /// Failed to serialize the asset data to bytes.
17 #[error("Serialization error: {0}")]
18 Serialization(anyhow::Error),
19
20 /// Any other error that occurred during saving.
21 #[error(transparent)]
22 Other(anyhow::Error),
23}
24
25/// Trait for saving asset data to raw byte data.
26///
27/// Asset savers are responsible for converting typed asset data back into raw bytes
28/// that can be written to files or transmitted over the network.
29pub trait AssetDataSaver {
30 /// Converts asset data into raw bytes.
31 ///
32 /// This method is called by the asset manager when an asset needs to be
33 /// saved to persistent storage or transmitted over the network.
34 ///
35 /// # Arguments
36 ///
37 /// * `asset` - The asset data to serialize into bytes
38 /// * `context` - Optional context providing access to asset manager state
39 /// and configuration during the saving process
40 ///
41 /// # Returns
42 ///
43 /// The serialized byte data on success, or an error if saving failed.
44 ///
45 /// # Errors
46 ///
47 /// Returns an error if:
48 /// * The asset type is not supported by this saver
49 /// * Serialization of the asset data fails
50 /// * Any other error occurs during the saving process
51 fn asset_to_bytes(
52 &self,
53 asset: &dyn AssetData,
54 context: Option<Arc<dyn AssetManagerContext>>,
55 ) -> Result<Vec<u8>, AssetSaveError>;
56}