mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Add table builder util
This commit is contained in:
parent
1f72033a6a
commit
b4bbc0630a
3 changed files with 72 additions and 0 deletions
|
@ -2,6 +2,7 @@ use std::process::ExitCode;
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod scheduler;
|
mod scheduler;
|
||||||
|
mod util;
|
||||||
|
|
||||||
use self::scheduler::Scheduler;
|
use self::scheduler::Scheduler;
|
||||||
|
|
||||||
|
|
3
src/lune/util/mod.rs
Normal file
3
src/lune/util/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
mod table_builder;
|
||||||
|
|
||||||
|
pub use table_builder::TableBuilder;
|
68
src/lune/util/table_builder.rs
Normal file
68
src/lune/util/table_builder.rs
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
use mlua::prelude::*;
|
||||||
|
|
||||||
|
use crate::lune::scheduler::LuaSchedulerExt;
|
||||||
|
|
||||||
|
pub struct TableBuilder<'lua> {
|
||||||
|
lua: &'lua Lua,
|
||||||
|
tab: LuaTable<'lua>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'lua> TableBuilder<'lua> {
|
||||||
|
pub fn new(lua: &'lua Lua) -> LuaResult<Self> {
|
||||||
|
let tab = lua.create_table()?;
|
||||||
|
Ok(Self { lua, tab })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_value<K, V>(self, key: K, value: V) -> LuaResult<Self>
|
||||||
|
where
|
||||||
|
K: IntoLua<'lua>,
|
||||||
|
V: IntoLua<'lua>,
|
||||||
|
{
|
||||||
|
self.tab.raw_set(key, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_function<K, A, R, F>(self, key: K, func: F) -> LuaResult<Self>
|
||||||
|
where
|
||||||
|
K: IntoLua<'lua>,
|
||||||
|
A: FromLuaMulti<'lua>,
|
||||||
|
R: IntoLuaMulti<'lua>,
|
||||||
|
F: Fn(&'lua Lua, A) -> LuaResult<R> + 'static,
|
||||||
|
{
|
||||||
|
let f = self.lua.create_function(func)?;
|
||||||
|
self.with_value(key, LuaValue::Function(f))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_metatable(self, table: LuaTable) -> LuaResult<Self> {
|
||||||
|
self.tab.set_metatable(Some(table));
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_readonly(self) -> LuaResult<LuaTable<'lua>> {
|
||||||
|
self.tab.set_readonly(true);
|
||||||
|
Ok(self.tab)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(self) -> LuaResult<LuaTable<'lua>> {
|
||||||
|
Ok(self.tab)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: Remove static lifetimes here when possible and move into above impl
|
||||||
|
impl TableBuilder<'static> {
|
||||||
|
pub fn with_async_function<K, A, R, F, FR>(self, key: K, func: F) -> LuaResult<Self>
|
||||||
|
where
|
||||||
|
K: IntoLua<'static>,
|
||||||
|
A: FromLuaMulti<'static>,
|
||||||
|
R: IntoLuaMulti<'static>,
|
||||||
|
F: Fn(&'static Lua, A) -> FR + 'static,
|
||||||
|
FR: Future<Output = LuaResult<R>> + 'static,
|
||||||
|
{
|
||||||
|
let f = self.lua.create_async_function(func)?;
|
||||||
|
self.with_value(key, LuaValue::Function(f))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue