From 3d1723dab4bd2a9fb401fcc3c02315449a945973 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 16 Mar 2024 13:43:35 +0100 Subject: [PATCH] Add more utility functions to lua api --- src/scripting/mod.rs | 1 + src/scripting/path_module.rs | 42 +++++++++++++++++++++++++++++++++++ src/scripting/require.rs | 2 ++ src/scripting/utils_module.rs | 17 ++++++++++++-- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/scripting/path_module.rs diff --git a/src/scripting/mod.rs b/src/scripting/mod.rs index 1312d36..509e9bd 100644 --- a/src/scripting/mod.rs +++ b/src/scripting/mod.rs @@ -1,4 +1,5 @@ pub mod log_module; +pub mod path_module; mod require; pub mod silo_module; pub mod utils_module; diff --git a/src/scripting/path_module.rs b/src/scripting/path_module.rs new file mode 100644 index 0000000..86f30c3 --- /dev/null +++ b/src/scripting/path_module.rs @@ -0,0 +1,42 @@ +use std::path::PathBuf; + +use mlua::{Lua, Result, Table}; + +struct PathModule; + +macro_rules! module { + ($lua:expr, $($fn_name:expr => $fn: expr),+,) => { + { + let table = $lua.create_table()?; + $( + table.set($fn_name, $lua.create_function($fn)?)?; + )+ + table + } + }; + ($lua:expr, $($fn_name:expr => $fn: expr),+) => { + module!($lua, $($fn_name => $fn),+,) + } +} + +/// Utility functions +pub fn path_module(lua: &Lua) -> Result { + let exports = module!(lua, + "join" => PathModule::join, + "exists" => PathModule::exists, + ); + + Ok(exports) +} + +impl PathModule { + fn join(_lua: &Lua, paths: Vec) -> Result { + Ok(PathBuf::from_iter(paths).to_string_lossy().to_string()) + } + + fn exists(_lua: &Lua, path: String) -> Result { + let path = PathBuf::from(path); + + Ok(path.exists()) + } +} diff --git a/src/scripting/require.rs b/src/scripting/require.rs index 6a605d9..f51c547 100644 --- a/src/scripting/require.rs +++ b/src/scripting/require.rs @@ -1,6 +1,7 @@ use mlua::{Lua, Result, Table}; use super::log_module::log_module; +use super::path_module::path_module; use super::silo_module::silo_module; use super::utils_module::utils_module; @@ -18,6 +19,7 @@ fn lua_require(lua: &Lua, module: String) -> Result> { "silo" => silo_module(lua), "log" => log_module(lua), "utils" => utils_module(lua), + "path" => path_module(lua), _ => { let old_require: mlua::Function = lua.globals().get("old_require")?; old_require.call(module) diff --git a/src/scripting/utils_module.rs b/src/scripting/utils_module.rs index e9b42ef..006614a 100644 --- a/src/scripting/utils_module.rs +++ b/src/scripting/utils_module.rs @@ -5,6 +5,7 @@ use std::{ use mlua::{Function, Lua, LuaSerdeExt, Result, Table}; use serde::Serialize; +use which::which; /// Utility functions pub fn utils_module(lua: &Lua) -> Result
{ @@ -17,6 +18,7 @@ pub fn utils_module(lua: &Lua) -> Result
{ exports.set("load_toml", lua.create_function(lua_load_toml)?)?; exports.set("ext", lua.create_function(lua_ext)?)?; exports.set("ext_piped", lua.create_function(lua_ext_piped)?)?; + exports.set("which", lua.create_function(lua_which)?)?; if let Ok(nu_path) = which::which("nu") { exports.set( @@ -76,6 +78,11 @@ fn lua_load_toml<'a>(lua: &'a Lua, path: String) -> Result> { lua_from_toml(lua, contents) } +/// Returns the path to the given command +fn lua_which<'a>(_: &'a Lua, path: String) -> Result> { + Ok(which(path).ok().map(|p| p.to_string_lossy().into_owned())) +} + /// Creates a new executable that can be called with a variable number of args fn lua_ext<'a>(lua: &'a Lua, program: String) -> Result> { lua.create_function(move |_lua, args| { @@ -113,8 +120,14 @@ fn lua_ext_piped<'a>(lua: &'a Lua, program: String) -> Result> { let output = cmd.wait_with_output()?; let output = CommandOutput { code: output.status.code().unwrap_or(0), - stdout: String::from_utf8(output.stdout).map_err(mlua::Error::external)?, - stderr: String::from_utf8(output.stderr).map_err(mlua::Error::external)?, + stdout: String::from_utf8(output.stdout) + .map_err(mlua::Error::external)? + .trim() + .into(), + stderr: String::from_utf8(output.stderr) + .map_err(mlua::Error::external)? + .trim() + .into(), }; lua.to_value(&output)