use std::future::Future; use mlua::prelude::*; pub struct TableBuilder<'lua> { lua: &'lua Lua, tab: LuaTable<'lua>, } impl<'lua> TableBuilder<'lua> { pub fn new(lua: &'lua Lua) -> LuaResult { let tab = lua.create_table()?; Ok(Self { lua, tab }) } pub fn with_value(self, key: K, value: V) -> LuaResult where K: ToLua<'lua>, V: ToLua<'lua>, { self.tab.raw_set(key, value)?; Ok(self) } pub fn with_values(self, values: Vec<(K, V)>) -> LuaResult where K: ToLua<'lua>, V: ToLua<'lua>, { for (key, value) in values { self.tab.raw_set(key, value)?; } Ok(self) } pub fn with_sequential_value(self, value: V) -> LuaResult where V: ToLua<'lua>, { self.tab.raw_push(value)?; Ok(self) } pub fn with_sequential_values(self, values: Vec) -> LuaResult where V: ToLua<'lua>, { for value in values { self.tab.raw_push(value)?; } Ok(self) } pub fn with_metatable(self, table: LuaTable) -> LuaResult { self.tab.set_metatable(Some(table)); Ok(self) } pub fn with_function(self, key: K, func: F) -> LuaResult where K: ToLua<'lua>, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + Fn(&'lua Lua, A) -> LuaResult, { let f = self.lua.create_function(func)?; self.with_value(key, LuaValue::Function(f)) } pub fn with_async_function(self, key: K, func: F) -> LuaResult where K: ToLua<'lua>, A: FromLuaMulti<'lua>, R: ToLuaMulti<'lua>, F: 'static + Fn(&'lua Lua, A) -> FR, FR: 'lua + Future>, { let f = self.lua.create_async_function(func)?; self.with_value(key, LuaValue::Function(f)) } pub fn build_readonly(self) -> LuaResult> { self.tab.set_readonly(true); Ok(self.tab) } pub fn build(self) -> LuaResult> { Ok(self.tab) } }