Use table builder helper for creating globals

This commit is contained in:
Filip Tibell 2023-01-21 15:48:56 -05:00
parent e6faa3f6be
commit f9627fc2ae
No known key found for this signature in database
7 changed files with 104 additions and 62 deletions

View file

@ -1,22 +1,22 @@
use mlua::{Lua, MultiValue, Result, Table};
use crate::utils::formatting::{
flush_stdout, pretty_format_multi_value, print_color, print_label, print_style,
use crate::utils::{
formatting::{flush_stdout, pretty_format_multi_value, print_color, print_label, print_style},
table_builder::ReadonlyTableBuilder,
};
pub fn new(lua: &Lua) -> Result<Table> {
let tab = lua.create_table()?;
tab.raw_set("resetColor", lua.create_function(console_reset_color)?)?;
tab.raw_set("setColor", lua.create_function(console_set_color)?)?;
tab.raw_set("resetStyle", lua.create_function(console_reset_style)?)?;
tab.raw_set("setStyle", lua.create_function(console_set_style)?)?;
tab.raw_set("format", lua.create_function(console_format)?)?;
tab.raw_set("log", lua.create_function(console_log)?)?;
tab.raw_set("info", lua.create_function(console_info)?)?;
tab.raw_set("warn", lua.create_function(console_warn)?)?;
tab.raw_set("error", lua.create_function(console_error)?)?;
tab.set_readonly(true);
Ok(tab)
ReadonlyTableBuilder::new(lua)?
.with_function("resetColor", console_reset_color)?
.with_function("setColor", console_set_color)?
.with_function("resetStyle", console_reset_style)?
.with_function("setStyle", console_set_style)?
.with_function("format", console_format)?
.with_function("log", console_log)?
.with_function("info", console_info)?
.with_function("warn", console_warn)?
.with_function("error", console_error)?
.build()
}
fn console_reset_color(_: &Lua, _: ()) -> Result<()> {

View file

@ -3,18 +3,19 @@ use std::path::{PathBuf, MAIN_SEPARATOR};
use mlua::{Lua, Result, Table};
use tokio::fs;
use crate::utils::table_builder::ReadonlyTableBuilder;
pub fn new(lua: &Lua) -> Result<Table> {
let tab = lua.create_table()?;
tab.raw_set("readFile", lua.create_async_function(fs_read_file)?)?;
tab.raw_set("readDir", lua.create_async_function(fs_read_dir)?)?;
tab.raw_set("writeFile", lua.create_async_function(fs_write_file)?)?;
tab.raw_set("writeDir", lua.create_async_function(fs_write_dir)?)?;
tab.raw_set("removeFile", lua.create_async_function(fs_remove_file)?)?;
tab.raw_set("removeDir", lua.create_async_function(fs_remove_dir)?)?;
tab.raw_set("isFile", lua.create_async_function(fs_is_file)?)?;
tab.raw_set("isDir", lua.create_async_function(fs_is_dir)?)?;
tab.set_readonly(true);
Ok(tab)
ReadonlyTableBuilder::new(lua)?
.with_async_function("readFile", fs_read_file)?
.with_async_function("readDir", fs_read_dir)?
.with_async_function("writeFile", fs_write_file)?
.with_async_function("writeDir", fs_write_dir)?
.with_async_function("removeFile", fs_remove_file)?
.with_async_function("removeDir", fs_remove_dir)?
.with_async_function("isFile", fs_is_file)?
.with_async_function("isDir", fs_is_dir)?
.build()
}
async fn fs_read_file(_: &Lua, path: String) -> Result<String> {

View file

@ -6,15 +6,14 @@ use reqwest::{
Method,
};
use crate::utils::net::get_request_user_agent_header;
use crate::utils::{net::get_request_user_agent_header, table_builder::ReadonlyTableBuilder};
pub fn new(lua: &Lua) -> Result<Table> {
let tab = lua.create_table()?;
tab.raw_set("jsonEncode", lua.create_function(net_json_encode)?)?;
tab.raw_set("jsonDecode", lua.create_function(net_json_decode)?)?;
tab.raw_set("request", lua.create_async_function(net_request)?)?;
tab.set_readonly(true);
Ok(tab)
ReadonlyTableBuilder::new(lua)?
.with_function("jsonEncode", net_json_encode)?
.with_function("jsonDecode", net_json_decode)?
.with_async_function("request", net_request)?
.build()
}
fn net_json_encode(_: &Lua, (val, pretty): (Value, Option<bool>)) -> Result<String> {

View file

@ -7,6 +7,8 @@ use mlua::{Error, Function, Lua, MetaMethod, Result, Table, Value};
use os_str_bytes::RawOsString;
use tokio::process::Command;
use crate::utils::table_builder::ReadonlyTableBuilder;
pub fn new(lua: &Lua, args_vec: Vec<String>) -> Result<Table> {
// Create readonly args array
let inner_args = lua.create_table()?;
@ -34,13 +36,12 @@ pub fn new(lua: &Lua, args_vec: Vec<String>) -> Result<Table> {
inner_env.set_metatable(Some(inner_env_meta));
inner_env.set_readonly(true);
// Create the full process table
let tab = lua.create_table()?;
tab.raw_set("args", inner_args)?;
tab.raw_set("env", inner_env)?;
tab.raw_set("exit", lua.create_function(process_exit)?)?;
tab.raw_set("spawn", lua.create_async_function(process_spawn)?)?;
tab.set_readonly(true);
Ok(tab)
ReadonlyTableBuilder::new(lua)?
.with_table("args", inner_args)?
.with_table("env", inner_env)?
.with_function("exit", process_exit)?
.with_async_function("spawn", process_spawn)?
.build()
}
fn process_env_get<'lua>(lua: &'lua Lua, (_, key): (Value<'lua>, String)) -> Result<Value<'lua>> {

View file

@ -3,23 +3,22 @@ use std::time::Duration;
use mlua::{Function, Lua, Result, Table, Value, Variadic};
use tokio::time;
use crate::utils::table_builder::ReadonlyTableBuilder;
const DEFAULT_SLEEP_DURATION: f32 = 1.0 / 60.0;
pub fn new(lua: &Lua) -> Result<Table> {
let tab = lua.create_table()?;
tab.raw_set(
ReadonlyTableBuilder::new(lua)?
.with_async_function(
"defer",
lua.create_async_function(
|lua, (func, args): (Function, Variadic<Value>)| async move {
let thread = lua.create_thread(func)?;
thread.into_async(args).await?;
Ok(())
},
)?,
)?;
tab.raw_set(
)?
.with_async_function(
"delay",
lua.create_async_function(
|lua, (func, duration, args): (Function, Option<f32>, Variadic<Value>)| async move {
let secs = duration.unwrap_or(DEFAULT_SLEEP_DURATION);
time::sleep(Duration::from_secs_f32(secs)).await;
@ -27,26 +26,19 @@ pub fn new(lua: &Lua) -> Result<Table> {
thread.into_async(args).await?;
Ok(())
},
)?,
)?;
tab.raw_set(
)?
.with_async_function(
"spawn",
lua.create_async_function(
|lua, (func, args): (Function, Variadic<Value>)| async move {
let thread = lua.create_thread(func)?;
thread.into_async(args).await?;
Ok(())
},
)?,
)?;
tab.raw_set(
"wait",
lua.create_async_function(|_, duration: Option<f32>| async move {
)?
.with_async_function("wait", |_, duration: Option<f32>| async move {
let secs = duration.unwrap_or(DEFAULT_SLEEP_DURATION);
time::sleep(Duration::from_secs_f32(secs)).await;
Ok(secs)
})?,
)?;
tab.set_readonly(true);
Ok(tab)
})?
.build()
}

View file

@ -1,2 +1,3 @@
pub mod formatting;
pub mod net;
pub mod table_builder;

View file

@ -0,0 +1,48 @@
use std::future::Future;
use mlua::{FromLuaMulti, Lua, Result, Table, ToLuaMulti};
pub struct ReadonlyTableBuilder<'lua> {
lua: &'lua Lua,
tab: Table<'lua>,
}
impl<'lua> ReadonlyTableBuilder<'lua> {
pub fn new(lua: &'lua Lua) -> Result<Self> {
let tab = lua.create_table()?;
Ok(Self { lua, tab })
}
pub fn with_table(self, key: &'static str, value: Table) -> Result<Self> {
self.tab.raw_set(key, value)?;
Ok(self)
}
pub fn with_function<A, R, F>(self, key: &'static str, func: F) -> Result<Self>
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
F: 'static + Fn(&'lua Lua, A) -> Result<R>,
{
let value = self.lua.create_function(func)?;
self.tab.raw_set(key, value)?;
Ok(self)
}
pub fn with_async_function<A, R, F, FR>(self, key: &'static str, func: F) -> Result<Self>
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
F: 'static + Fn(&'lua Lua, A) -> FR,
FR: 'lua + Future<Output = Result<R>>,
{
let value = self.lua.create_async_function(func)?;
self.tab.raw_set(key, value)?;
Ok(self)
}
pub fn build(self) -> Result<Table<'lua>> {
self.tab.set_readonly(true);
Ok(self.tab)
}
}