Trait veloren_common_assets::DirLoadable

pub trait DirLoadable: Storable {
    // Required method
    fn select_ids(
        cache: AnyCache<'_>,
        id: &SharedString,
    ) -> Result<Vec<SharedString>, Error>;

    // Provided method
    fn sub_directories(
        cache: AnyCache<'_>,
        id: &SharedString,
        f: impl FnMut(&str),
    ) -> Result<(), Error> { ... }
}
Expand description

Assets that are loadable from directories

Types that implement this trait can be used with [AssetCache::load_dir] to load all available assets in a directory (eventually recursively).

This trait is automatically implemented for all types that implement Asset, and you can implement it to extend your own Compounds.

§Exemple implementation

Imagine you have several playlists with a JSON manifest to specify the ids of the musics to include.

use assets_manager::{
    Asset, Compound, BoxedError, AnyCache, SharedString,
    asset::{DirLoadable, Json},
    source::{DirEntry, Source},
};

/// A music for our game.
#[derive(Clone)]
struct Music {
    /* ... */
}

impl Asset for Music {
    /* ... */
}

/// A simple playlist, an ordered list of musics.
struct Playlist {
    sounds: Vec<Music>,
}

// Specify how to load a playlist
impl Compound for Playlist {
    fn load(cache: AnyCache, id: &SharedString) -> Result<Self, BoxedError> {
        // Read the manifest (a list of ids)
        let manifest = cache.load::<Json<Vec<String>>>(id)?.read();

        // Load each sound
        let sounds = manifest.0.iter()
            .map(|id| Ok(cache.load::<Music>(id)?.cloned()))
            .collect::<Result<_, BoxedError>>()?;

        Ok(Playlist { sounds })
    }
}

// Specify how to get ids of playlists in a directory
impl DirLoadable for Playlist {
    fn select_ids(cache: AnyCache, id: &SharedString) -> std::io::Result<Vec<SharedString>> {
        let mut ids = Vec::new();

        // Select all files with "json" extension (manifest files)
        cache.raw_source().read_dir(id, &mut |entry| {
            if let DirEntry::File(id, ext) = entry {
                if ext == "json" {
                    ids.push(id.into());
                }
            }
        })?;

        Ok(ids)
    }
}

Required Methods§

fn select_ids( cache: AnyCache<'_>, id: &SharedString, ) -> Result<Vec<SharedString>, Error>

Returns the ids of the assets contained in the directory given by id.

Note that the order of the returned ids is not kept, and that redundant ids are removed.

Provided Methods§

fn sub_directories( cache: AnyCache<'_>, id: &SharedString, f: impl FnMut(&str), ) -> Result<(), Error>

Executes the given closure for each id of a child directory of the given directory. The default implementation reads the cache’s source.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

§

impl<T> DirLoadable for Arc<T>
where T: DirLoadable,

§

fn select_ids( cache: AnyCache<'_>, id: &SharedString, ) -> Result<Vec<SharedString>, Error>

§

fn sub_directories( cache: AnyCache<'_>, id: &SharedString, f: impl FnMut(&str), ) -> Result<(), Error>

Implementors§

§

impl<T> DirLoadable for T
where T: Asset,