oxeon 0.0.6

Rust-like scripting language for modding Bevy games, compiled to sandboxed WebAssembly.
Documentation
use bevy_asset::io::Reader;
use bevy_asset::{Asset, AssetLoader, LoadContext};
use bevy_reflect::TypePath;

/// A `.ox` source file. It's compiled once loaded, together with the
/// game's bindings (see `OxeonBindings`).
#[derive(Asset, TypePath, Debug)]
pub struct OxeonSource {
    pub source: String,
    /// The mod's name, for a source that isn't a file (see
    /// [`crate::LoadModSource`]); a file's name is its asset path.
    pub name: Option<String>,
}

#[derive(Debug, thiserror::Error)]
pub enum OxeonLoadError {
    #[error("could not read mod source: {0}")]
    Io(#[from] std::io::Error),
    #[error("mod source is not valid UTF-8")]
    Utf8(#[from] std::string::FromUtf8Error),
}

/// Loads `.ox` files. Bevy's file watcher (the `hot-reload` feature)
/// reloads them on change, and the plugin recompiles them.
#[derive(Default, TypePath)]
pub struct OxeonSourceLoader;

impl AssetLoader for OxeonSourceLoader {
    type Asset = OxeonSource;
    type Settings = ();
    type Error = OxeonLoadError;

    async fn load(
        &self,
        reader: &mut dyn Reader,
        _settings: &(),
        _load_context: &mut LoadContext<'_>,
    ) -> Result<OxeonSource, OxeonLoadError> {
        let mut bytes = Vec::new();
        reader.read_to_end(&mut bytes).await?;
        Ok(OxeonSource {
            source: String::from_utf8(bytes)?,
            name: None,
        })
    }

    fn extensions(&self) -> &[&str] {
        &[oxeon_compiler::SOURCE_EXTENSION]
    }
}