use crate::core::errors::EngineError;
use mlua::LuaSerdeExt;
use mlua_flow_ir::Node;
use serde_json::Value;
pub fn parse_lua_blueprint(lua_src: &str) -> Result<Node, EngineError> {
let lua = mlua::Lua::new();
let bp_val: mlua::Value = lua
.load(lua_src)
.eval()
.map_err(|e| EngineError::Internal(format!("lua eval: {e}")))?;
let bp: Node = lua
.from_value(bp_val)
.map_err(|e| EngineError::Internal(format!("lua → Node parse: {e}")))?;
Ok(bp)
}
pub fn parse_lua_blueprint_with_ctx(lua_src: &str) -> Result<(Node, Value), EngineError> {
let lua = mlua::Lua::new();
let outer: mlua::Table = lua
.load(lua_src)
.eval()
.map_err(|e| EngineError::Internal(format!("lua eval: {e}")))?;
let bp_val: mlua::Value = outer
.get("bp")
.map_err(|e| EngineError::Internal(format!("table missing `bp`: {e}")))?;
let ctx_val: mlua::Value = outer
.get("ctx")
.map_err(|e| EngineError::Internal(format!("table missing `ctx`: {e}")))?;
let bp: Node = lua
.from_value(bp_val)
.map_err(|e| EngineError::Internal(format!("lua → Node parse: {e}")))?;
let ctx: Value = lua
.from_value(ctx_val)
.map_err(|e| EngineError::Internal(format!("lua → ctx parse: {e}")))?;
Ok((bp, ctx))
}