Add lua dir definition support
parent
ae13b467e6
commit
b034617e73
@ -0,0 +1,5 @@
|
||||
[build]
|
||||
rustflags = [
|
||||
"-C", "link-arg=-undefined",
|
||||
"-C", "link-arg=dynamic_lookup",
|
||||
]
|
@ -0,0 +1,36 @@
|
||||
use log::Level;
|
||||
use mlua::{Error, Lua, Result, Table};
|
||||
|
||||
pub fn log_module(lua: &Lua) -> Result<Table> {
|
||||
let exports = lua.create_table()?;
|
||||
|
||||
for level in [
|
||||
Level::Trace,
|
||||
Level::Debug,
|
||||
Level::Info,
|
||||
Level::Warn,
|
||||
Level::Error,
|
||||
] {
|
||||
exports.set(
|
||||
level.as_str().to_lowercase(),
|
||||
lua.create_function(move |_, v| lua_log(v, level))?,
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(exports)
|
||||
}
|
||||
|
||||
fn lua_log(value: mlua::Value, level: log::Level) -> Result<()> {
|
||||
match level {
|
||||
Level::Error | Level::Warn | Level::Info => {
|
||||
log::log!(target: "lua", level, "{}", value.to_string()?)
|
||||
}
|
||||
Level::Debug | Level::Trace => log::log!(
|
||||
target: "lua",
|
||||
level,
|
||||
"{}",
|
||||
serde_json::to_string(&value).map_err(Error::external)?
|
||||
),
|
||||
};
|
||||
Ok(())
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
pub mod log_module;
|
||||
mod require;
|
||||
pub mod silo_module;
|
||||
|
||||
use miette::Result;
|
||||
use mlua::{Lua, LuaSerdeExt};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::utils::Describe;
|
||||
|
||||
pub fn create_lua<T: Serialize>(ctx: &T) -> Result<Lua> {
|
||||
let lua = Lua::new();
|
||||
{
|
||||
let globals = lua.globals();
|
||||
require::register_require(&lua).describe("registering custom require")?;
|
||||
globals
|
||||
.set(
|
||||
"silo_ctx",
|
||||
lua.to_value(ctx).describe("serializing context to lua")?,
|
||||
)
|
||||
.describe("registering silo context")?;
|
||||
}
|
||||
|
||||
Ok(lua)
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
use mlua::{Lua, Result, Table};
|
||||
|
||||
use super::log_module::log_module;
|
||||
use super::silo_module::silo_module;
|
||||
|
||||
pub fn register_require(lua: &Lua) -> Result<()> {
|
||||
let globals = lua.globals();
|
||||
let old_require: mlua::Function = globals.get("require")?;
|
||||
globals.set("old_require", old_require)?;
|
||||
globals.set("require", lua.create_function(lua_require)?)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn lua_require<'a>(lua: &'a Lua, module: String) -> Result<Table<'a>> {
|
||||
match module.as_str() {
|
||||
"silo" => silo_module(&lua),
|
||||
"log" => log_module(&lua),
|
||||
_ => {
|
||||
let old_require: mlua::Function = lua.globals().get("old_require")?;
|
||||
old_require.call(module)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
use mlua::{Lua, LuaSerdeExt, Result, Table};
|
||||
|
||||
use crate::templating::ContextData;
|
||||
|
||||
pub fn silo_module(lua: &Lua) -> Result<Table> {
|
||||
let silo_ctx = ContextData::default();
|
||||
let exports = lua.create_table()?;
|
||||
|
||||
exports.set("dirs", lua.to_value(&silo_ctx.dirs)?)?;
|
||||
exports.set("flags", lua.to_value(&silo_ctx.flags)?)?;
|
||||
exports.set("system", lua.to_value(&silo_ctx.system)?)?;
|
||||
exports.set("usercfg", lua.globals().get::<_, mlua::Value>("silo_ctx")?)?;
|
||||
|
||||
Ok(exports)
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
use std::fmt;
|
||||
|
||||
use miette::{Context, IntoDiagnostic};
|
||||
|
||||
pub trait Describe<T, E>: miette::IntoDiagnostic<T, E> {
|
||||
fn describe<S: fmt::Display + Send + Sync + 'static>(self, s: S) -> miette::Result<T>;
|
||||
fn with_describe<F: FnOnce() -> S, S: fmt::Display + Send + Sync + 'static>(
|
||||
self,
|
||||
f: F,
|
||||
) -> miette::Result<T>;
|
||||
}
|
||||
|
||||
impl<T, E: std::error::Error + Send + Sync + 'static> Describe<T, E> for Result<T, E> {
|
||||
fn describe<S: fmt::Display + Send + Sync + 'static>(self, s: S) -> miette::Result<T> {
|
||||
self.into_diagnostic().context(s)
|
||||
}
|
||||
|
||||
fn with_describe<F: FnOnce() -> S, S: fmt::Display + Send + Sync + 'static>(
|
||||
self,
|
||||
f: F,
|
||||
) -> miette::Result<T> {
|
||||
match &self {
|
||||
Ok(_) => self.into_diagnostic(),
|
||||
Err(_) => self.into_diagnostic().context(f()),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue