use bevy_asset::io::Reader;
use bevy_asset::{Asset, AssetLoader, LoadContext};
use bevy_reflect::TypePath;
#[derive(Asset, TypePath, Debug)]
pub struct OxeonSource {
pub source: String,
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),
}
#[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]
}
}