gled-core 2.31.2

The engine of gled without a user interface: inputs, timing, rendering, Art-Net/DMX output and storage
Documentation
//! Saving and deleting assets: updates every thread's collections and queues
//! the file change for the storage thread, which writes it into the asset
//! repository.

use crate::storage::{
    action::StorageAction,
    asset::{Asset, AssetTrait},
    asset_id::AssetId,
    collections::Collections,
};

/// Persisting assets.
pub(crate) trait AssetStore {
    /// Stores the asset in the collections and on disk.
    fn save(self, collections: &mut Collections);
}

impl<T: AssetTrait> AssetStore for Asset<T> {
    fn save(self, collections: &mut Collections) {
        tracing::info!("Setting asset in cache: {:?}", self.id);
        collections.get_mut::<T>().set_asset(self.clone());
        collections.save();

        tracing::info!("Saving asset: {:?}", self.id);
        let uuid = self.id.id;
        match self.into_json() {
            Ok(json) => {
                StorageAction::SaveAsset {
                    dir_name: T::DIR_NAME,
                    uuid,
                    json,
                }
                .enqueue();
            }
            Err(err) => tracing::error!("Could not serialize asset {uuid}: {err}"),
        }
    }
}

/// Removes the asset with this id from the collections and from disk.
pub(crate) fn delete_asset<T: AssetTrait>(id: AssetId<T>, collections: &mut Collections) {
    tracing::info!("Deleting asset: {id}");

    collections.get_mut::<T>().delete_asset(id);
    collections.save();

    StorageAction::DeleteAsset {
        uuid: id.id,
        dir_name: T::DIR_NAME,
    }
    .enqueue();
}