use mlua::prelude::*;

pub struct LuaEnv {
    pub lua: Lua,
}

impl LuaEnv {
    pub fn new() -> Self {
        LuaEnv { lua: Lua::new() }
    }

    pub fn func<F, A, R>(&self, name: &str, func: F) -> crate::Result<()>
    where
        F: Fn(&Lua, A) -> mlua::Result<R> + mlua::MaybeSend + 'static,
        A: FromLuaMulti,
        R: IntoLuaMulti,
    {
        self.lua
            .globals()
            .set(name, self.lua.create_function(func)?)?;

        Ok(())
    }

    pub fn exec(&self, script: String) -> crate::Result<()> {
        self.lua.load(script).exec()?;
        Ok(())
    }
}
