pub mod bounded;
pub mod function_bundle;
pub mod json;
pub mod validate;
use crate::{Error, FunctionBundle, Result, ScriptEngine};
use rhai::Engine;
pub use bounded::{
BoundedRhaiEngine, CompiledExpr, DEFAULT_MAX_ARRAY_SIZE, DEFAULT_MAX_CALL_LEVELS,
DEFAULT_MAX_MAP_SIZE, DEFAULT_MAX_OPERATIONS, DEFAULT_MAX_SOURCE_LEN, DEFAULT_MAX_STRING_SIZE,
};
pub use json::{from_dynamic_value, to_dynamic_value};
pub struct RhaiEngine {
engine: Engine,
scope: rhai::Scope<'static>,
}
impl Default for RhaiEngine {
fn default() -> Self {
Self::new()
}
}
impl RhaiEngine {
pub fn new() -> Self {
RhaiEngine {
engine: Engine::new(),
scope: rhai::Scope::new(),
}
}
pub fn with_bundle(bundle: &FunctionBundle) -> Self {
let mut engine = Self {
engine: Engine::new(),
scope: rhai::Scope::new(),
};
match bundle {
FunctionBundle::Rhai(rhai_bundle) => {
rhai_bundle.register(&mut engine.engine);
}
_ => {
}
}
engine
}
pub fn register_bundle(&mut self, bundle: &FunctionBundle) {
match bundle {
FunctionBundle::Rhai(rhai_bundle) => {
rhai_bundle.register(&mut self.engine);
}
_ => {
}
}
}
}
impl ScriptEngine for RhaiEngine {
fn eval(&mut self, code: &str) -> Result<String> {
match self
.engine
.eval_with_scope::<rhai::Dynamic>(&mut self.scope, code)
{
Ok(result) => Ok(result.to_string()),
Err(e) => Err(Error::Rhai(e.to_string())),
}
}
fn as_any(&mut self) -> &mut dyn std::any::Any {
self
}
}