Compare commits

...

3 Commits

@ -59,9 +59,7 @@ pub fn read_config(repo: &Path) -> Result<SiloConfig> {
lines.push(format!(
"config = utils.merge(config, utils.load_toml {old_config:?})"
));
lines.push(format!(
"config = utils.merge(config, config.template_context)"
))
lines.push("config = utils.merge(config, config.template_context)".to_string())
}
lines.push("-- Changes can be added to the `config` object".to_owned());
lines.push("".to_owned());

@ -233,7 +233,7 @@ impl RootDirData {
}
fn read_lua(path: &Path, cfg: &SiloConfig) -> Result<Self> {
let lua = create_lua(&cfg)?;
let lua = create_lua(cfg)?;
let cfg: Self = lua
.from_value(lua.load(path).eval().describe("evaluating script")?)
.describe("deserialize lua value")?;

@ -108,7 +108,7 @@ impl Hooks {
impl HookScript {
pub fn load(config: &SiloConfig, path: &Path) -> Result<Self> {
let lua = create_lua(&config)?;
let lua = create_lua(config)?;
let module: OwnedTable = lua
.load(path)
.eval()

@ -1,4 +1,5 @@
pub mod log_module;
pub mod path_module;
mod require;
pub mod silo_module;
pub mod utils_module;

@ -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<Table> {
let exports = module!(lua,
"join" => PathModule::join,
"exists" => PathModule::exists,
);
Ok(exports)
}
impl PathModule {
fn join(_lua: &Lua, paths: Vec<String>) -> Result<String> {
Ok(PathBuf::from_iter(paths).to_string_lossy().to_string())
}
fn exists(_lua: &Lua, path: String) -> Result<bool> {
let path = PathBuf::from(path);
Ok(path.exists())
}
}

@ -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<Table<'_>> {
"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)

@ -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<Table> {
@ -17,6 +18,7 @@ pub fn utils_module(lua: &Lua) -> Result<Table> {
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(
@ -50,7 +52,7 @@ fn lua_merge<'a>(lua: &'a Lua, (a, b): (mlua::Value, mlua::Value)) -> Result<mlu
}
/// Parse a json string into a lua value
fn lua_from_json<'a>(lua: &'a Lua, json_string: String) -> Result<mlua::Value<'a>> {
fn lua_from_json(lua: &Lua, json_string: String) -> Result<mlua::Value<'_>> {
let toml_value: serde_json::Value =
serde_json::from_str(&json_string).map_err(mlua::Error::external)?;
@ -58,26 +60,31 @@ fn lua_from_json<'a>(lua: &'a Lua, json_string: String) -> Result<mlua::Value<'a
}
/// Reads a json file and parses it as a lua value
fn lua_load_json<'a>(lua: &'a Lua, path: String) -> Result<mlua::Value<'a>> {
fn lua_load_json(lua: &Lua, path: String) -> Result<mlua::Value<'_>> {
let contents = fs::read_to_string(path)?;
lua_from_json(lua, contents)
}
/// Parse a toml string into a lua value
fn lua_from_toml<'a>(lua: &'a Lua, toml_string: String) -> Result<mlua::Value<'a>> {
fn lua_from_toml(lua: &Lua, toml_string: String) -> Result<mlua::Value<'_>> {
let toml_value: toml::Value = toml::from_str(&toml_string).map_err(mlua::Error::external)?;
lua.to_value(&toml_value)
}
/// Reads a toml file and parses it as a lua value
fn lua_load_toml<'a>(lua: &'a Lua, path: String) -> Result<mlua::Value<'a>> {
fn lua_load_toml(lua: &Lua, path: String) -> Result<mlua::Value<'_>> {
let contents = fs::read_to_string(path)?;
lua_from_toml(lua, contents)
}
/// Returns the path to the given command
fn lua_which(_: &Lua, path: String) -> Result<Option<String>> {
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<Function<'a>> {
fn lua_ext(lua: &Lua, program: String) -> Result<Function<'_>> {
lua.create_function(move |_lua, args| {
let exit_status = Command::new(&program)
.args::<Vec<String>, _>(args)
@ -103,7 +110,7 @@ struct CommandOutput {
}
/// Creates a new executable that can be called with a variable number of args
fn lua_ext_piped<'a>(lua: &'a Lua, program: String) -> Result<Function<'a>> {
fn lua_ext_piped(lua: &Lua, program: String) -> Result<Function<'_>> {
lua.create_function(move |lua, args| {
let cmd = Command::new(&program)
.args::<Vec<String>, _>(args)
@ -113,8 +120,14 @@ fn lua_ext_piped<'a>(lua: &'a Lua, program: String) -> Result<Function<'a>> {
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)

Loading…
Cancel
Save