#![doc = include_str!("../README.md")]
macro_rules! log_miette_error {
($err: expr) => {
#[cfg(feature = "fancy_errors")]
let message = {
let mut s = String::new();
miette::GraphicalReportHandler::new()
.with_context_lines(2)
.with_width(90)
.with_footer("\n".into())
.render_report(&mut s, $err)
.unwrap();
s
};
#[cfg(not(feature = "fancy_errors"))]
let message = $err;
bevy::log::error!("{message:#}");
};
}
use bevy::asset::LoadContext;
use bevy::ecs::{prelude::*, system::SystemState};
use bevy::reflect::TypeRegistry;
use crate::interpret::Interpreter;
pub use anyhow;
#[cfg(feature = "macros")]
pub use cuicui_chirp_macros::parse_dsl_impl;
pub use interpret::{Handles, InterpError};
pub use loader::{Chirp, ChirpBundle, ChirpState, WorldHandles};
pub use parse_dsl::{MethodCtx, ParseDsl};
pub use reflect::ReflectDsl;
mod parser;
pub mod interpret;
pub mod loader;
pub mod parse_dsl;
#[cfg(feature = "macros")]
pub mod parse_dsl_impl;
pub mod reflect;
#[doc(hidden)]
#[cfg(feature = "test_and_doc")]
pub mod __doc_helpers {
pub use cuicui_dsl::macros::__doc_helpers::*;
impl<D: DslBundle> crate::ParseDsl for DocDsl<D> {
fn method(&mut self, _: crate::MethodCtx) -> anyhow::Result<()> {
Ok(())
}
}
#[derive(Default, Component)]
pub struct Camera2dBundle;
#[derive(Default, Component)]
pub struct LayoutRootCamera;
pub mod cuicui_layout_bevy_ui {
pub type UiDsl = super::DocDsl;
}
}
#[doc(hidden)]
pub mod bevy_types {
pub use bevy::prelude::Entity;
}
pub struct ChirpReader<'a> {
pub world: &'a mut World,
}
impl<'a> ChirpReader<'a> {
pub fn new(world: &'a mut World) -> Self {
Self { world }
}
pub fn interpret<D: ParseDsl>(
&mut self,
handles: &Handles,
load_context: Option<&mut LoadContext>,
registry: &TypeRegistry,
input: &[u8],
) -> Result<Entity, interpret::Errors> {
let mut state = SystemState::<Commands>::new(self.world);
let mut cmds = state.get_mut(self.world);
let mut cmds = cmds.spawn_empty();
let id = cmds.id();
let result = Interpreter::interpret::<D>(input, &mut cmds, load_context, registry, handles);
if result.is_ok() {
state.apply(self.world);
}
result.map(|()| id)
}
#[allow(clippy::missing_panics_doc)] #[must_use]
pub fn interpret_logging<D: ParseDsl>(
&mut self,
handles: &Handles,
load_context: Option<&mut LoadContext>,
registry: &TypeRegistry,
input: &[u8],
) -> bool {
let mut state = SystemState::<Commands>::new(self.world);
let mut cmds = state.get_mut(self.world);
let mut cmds = cmds.spawn_empty();
let result = Interpreter::interpret::<D>(input, &mut cmds, load_context, registry, handles);
if let Err(err) = &result {
log_miette_error!(err);
false
} else {
state.apply(self.world);
true
}
}
}