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