diff --git a/src/lib/globals/console.rs b/src/lib/globals/console.rs index 95176dd..6a0bb7f 100644 --- a/src/lib/globals/console.rs +++ b/src/lib/globals/console.rs @@ -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 { - 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<()> { diff --git a/src/lib/globals/fs.rs b/src/lib/globals/fs.rs index 063771c..88580e0 100644 --- a/src/lib/globals/fs.rs +++ b/src/lib/globals/fs.rs @@ -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
{ - 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 { diff --git a/src/lib/globals/net.rs b/src/lib/globals/net.rs index 5cbdc86..40ea446 100644 --- a/src/lib/globals/net.rs +++ b/src/lib/globals/net.rs @@ -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
{ - 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)) -> Result { diff --git a/src/lib/globals/process.rs b/src/lib/globals/process.rs index 5c10ca5..268d13c 100644 --- a/src/lib/globals/process.rs +++ b/src/lib/globals/process.rs @@ -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) -> Result
{ // Create readonly args array let inner_args = lua.create_table()?; @@ -34,13 +36,12 @@ pub fn new(lua: &Lua, args_vec: Vec) -> Result
{ 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> { diff --git a/src/lib/globals/task.rs b/src/lib/globals/task.rs index 9139738..7c30a15 100644 --- a/src/lib/globals/task.rs +++ b/src/lib/globals/task.rs @@ -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
{ - let tab = lua.create_table()?; - tab.raw_set( - "defer", - lua.create_async_function( + ReadonlyTableBuilder::new(lua)? + .with_async_function( + "defer", |lua, (func, args): (Function, Variadic)| async move { let thread = lua.create_thread(func)?; thread.into_async(args).await?; Ok(()) }, - )?, - )?; - tab.raw_set( - "delay", - lua.create_async_function( + )? + .with_async_function( + "delay", |lua, (func, duration, args): (Function, Option, Variadic)| 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
{ thread.into_async(args).await?; Ok(()) }, - )?, - )?; - tab.raw_set( - "spawn", - lua.create_async_function( + )? + .with_async_function( + "spawn", |lua, (func, args): (Function, Variadic)| async move { let thread = lua.create_thread(func)?; thread.into_async(args).await?; Ok(()) }, - )?, - )?; - tab.raw_set( - "wait", - lua.create_async_function(|_, duration: Option| async move { + )? + .with_async_function("wait", |_, duration: Option| 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() } diff --git a/src/lib/utils/mod.rs b/src/lib/utils/mod.rs index 5168df1..7440516 100644 --- a/src/lib/utils/mod.rs +++ b/src/lib/utils/mod.rs @@ -1,2 +1,3 @@ pub mod formatting; pub mod net; +pub mod table_builder; diff --git a/src/lib/utils/table_builder.rs b/src/lib/utils/table_builder.rs new file mode 100644 index 0000000..2527b0f --- /dev/null +++ b/src/lib/utils/table_builder.rs @@ -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 { + let tab = lua.create_table()?; + Ok(Self { lua, tab }) + } + + pub fn with_table(self, key: &'static str, value: Table) -> Result { + self.tab.raw_set(key, value)?; + Ok(self) + } + + pub fn with_function(self, key: &'static str, func: F) -> Result + where + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + F: 'static + Fn(&'lua Lua, A) -> Result, + { + let value = self.lua.create_function(func)?; + self.tab.raw_set(key, value)?; + Ok(self) + } + + pub fn with_async_function(self, key: &'static str, func: F) -> Result + where + A: FromLuaMulti<'lua>, + R: ToLuaMulti<'lua>, + F: 'static + Fn(&'lua Lua, A) -> FR, + FR: 'lua + Future>, + { + let value = self.lua.create_async_function(func)?; + self.tab.raw_set(key, value)?; + Ok(self) + } + + pub fn build(self) -> Result> { + self.tab.set_readonly(true); + Ok(self.tab) + } +}