Skip to main content

extendable_assets/
loader.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 loading.
9#[derive(Debug, Error)]
10#[non_exhaustive]
11pub enum AssetLoadError {
12    /// Failed to deserialize the asset data from bytes.
13    #[error("Deserialization error: {0}")]
14    Deserialization(anyhow::Error),
15
16    /// Any other error that occurred during loading.
17    #[error(transparent)]
18    Other(anyhow::Error),
19}
20
21/// Trait for loading asset data from raw byte data.
22///
23/// Asset data loaders are responsible for converting raw bytes (typically read from files)
24/// into typed asset data that can be used by the application.
25pub trait AssetDataLoader {
26    /// Converts raw bytes into asset data.
27    ///
28    /// This method is called by the asset manager when raw asset data needs to be
29    /// deserialized into a typed asset object that can be used by the application.
30    ///
31    /// # Arguments
32    ///
33    /// * `bytes` - The raw byte data to deserialize into an asset
34    /// * `context` - Optional context providing access to asset manager state
35    ///   and configuration during the loading process
36    ///
37    /// # Returns
38    ///
39    /// The loaded asset data on success, or an error if loading failed.
40    ///
41    /// # Errors
42    ///
43    /// Returns an error if:
44    /// * Deserialization of the byte data fails
45    /// * The data format is invalid or corrupted
46    /// * Any other error occurs during the loading process
47    fn asset_from_bytes(
48        &self,
49        bytes: &[u8],
50        context: Option<Arc<dyn AssetManagerContext>>,
51    ) -> Result<Box<dyn AssetData>, AssetLoadError>;
52}