bevy_assets_extensions 0.7.0

Extensions for bevy assets, with support of collection as assets and a loader manager.
Documentation
use core::str;

use bevy::{asset::AssetLoader, prelude::*};

use crate::prelude::*;

/// Bundle of assets
#[derive(Asset, Debug, TypePath)]
pub struct Text {
    text: String,
}

impl Text {
    /// New text
    pub fn new(text: String) -> Text {
        Text { text }
    }
    /// Return a reference to the text.
    pub fn text_ref(&self) -> &String {
        &self.text
    }
}

#[derive(Default)]
pub(crate) struct TextLoader;

impl AssetLoader for TextLoader {
    type Asset = Text;
    type Settings = ();
    type Error = Error;
    async fn load(
        &self,
        reader: &mut dyn bevy::asset::io::Reader,
        _: &Self::Settings,
        _: &mut bevy::asset::LoadContext<'_>,
    ) -> std::result::Result<Self::Asset, Self::Error> {
        let mut bytes = Vec::new();
        reader.read_to_end(&mut bytes).await?;
        let text = String::from_utf8(bytes)?;
        Ok(Text { text })
    }
    fn extensions(&self) -> &[&str] {
        &["txt"]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bevy::asset::LoadState;
    use std::time::Duration;

    fn is_loading(load_state: LoadState) -> bool {
        matches!(load_state, LoadState::Loading)
    }

    #[test]
    fn test_text_asset_loading() {
        let mut app = App::new();
        app.add_plugins((MinimalPlugins, AssetPlugin::default()));
        app.init_asset::<Text>();
        app.init_asset_loader::<TextLoader>();
        let handle: Handle<assets::Text> = app
            .world()
            .resource::<AssetServer>()
            .load("test_relative/hello.txt");
        app.update();
        while is_loading(
            app.world()
                .resource::<AssetServer>()
                .get_load_state(&handle)
                .unwrap(),
        ) {
            app.update();
            std::thread::sleep(Duration::from_secs(1));
        }
        let hello = app
            .world()
            .resource::<Assets<assets::Text>>()
            .get(&handle)
            .unwrap();
        assert_eq!(hello.text_ref(), "hello");
    }
}