use anyhow::Result;
use bevy::asset::LoadContext;
use bevy::reflect::TypeRegistry;
use cuicui_dsl::{BaseDsl, DslBundle};
use thiserror::Error;
pub use args::Arguments;
pub use escape::escape_literal;
mod escape;
pub mod args;
#[derive(Debug, Error)]
#[error("No '{method}' method")]
pub struct DslParseError {
method: Box<str>,
}
impl DslParseError {
pub fn new(method: impl Into<Box<str>>) -> Self {
Self { method: method.into() }
}
}
pub struct MethodCtx<'i, 'c, 'cc> {
pub name: &'i str,
pub arguments: Arguments<'i, 'c>,
pub ctx: Option<&'c mut LoadContext<'cc>>,
pub registry: &'c TypeRegistry,
}
pub trait ParseDsl: DslBundle {
fn method(&mut self, ctx: MethodCtx) -> Result<()>;
}
impl ParseDsl for BaseDsl {
fn method(&mut self, data: MethodCtx) -> Result<()> {
let MethodCtx { name, arguments: args, .. } = data;
if name == "named" {
let name = args.get(0).unwrap();
let str = String::from(String::from_utf8_lossy(name.as_ref()));
self.named(str);
Ok(())
} else {
Err(DslParseError::new(name).into())
}
}
}