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