use crate::{database::path::AssetPath, fetch::AssetFetch};
use anput::{bundle::DynamicBundle, world::World};
use std::error::Error;
pub struct RewriteAssetFetch<Fetch: AssetFetch> {
fetch: Fetch,
#[allow(clippy::type_complexity)]
callback: Box<dyn Fn(AssetPath) -> Result<AssetPath, Box<dyn Error>> + Send + Sync>,
}
impl<Fetch: AssetFetch> RewriteAssetFetch<Fetch> {
pub fn new(
fetch: Fetch,
callback: impl Fn(AssetPath) -> Result<AssetPath, Box<dyn Error>> + Send + Sync + 'static,
) -> Self {
Self {
fetch,
callback: Box::new(callback),
}
}
}
impl<Fetch: AssetFetch> AssetFetch for RewriteAssetFetch<Fetch> {
fn load_bytes(&self, path: AssetPath) -> Result<DynamicBundle, Box<dyn Error>> {
let path = (self.callback)(path)?;
self.fetch.load_bytes(path)
}
fn maintain(&mut self, storage: &mut World) -> Result<(), Box<dyn Error>> {
self.fetch.maintain(storage)
}
}