aboutsummaryrefslogtreecommitdiff
path: root/src/lua.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua.rs')
-rw-r--r--src/lua.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/lua.rs b/src/lua.rs
index 2ab5624..5caf3f1 100644
--- a/src/lua.rs
+++ b/src/lua.rs
@@ -11,7 +11,10 @@ use rlua::Table;
use crate::Context;
+use self::template::ArgIndex;
+
mod serde;
+pub mod template;
pub struct Script<'a> {
pub lua_module_name: &'a str,
@@ -113,6 +116,47 @@ impl<'a> Script<'a> {
}
}
+fn module_path(module_name: &str) -> String {
+ format!("modules/{}.lua", module_name)
+}
+
+pub struct ModuleFunction<'a> {
+ pub module_name: &'a str,
+ pub function_name: &'a str,
+}
+
+pub fn call(
+ modfn: &ModuleFunction,
+ args: impl Iterator<Item = (ArgIndex, String)>,
+ ctx: &Context,
+) -> Result<String, ScriptError> {
+ let filename = module_path(modfn.module_name);
+
+ let lua_entr = ctx
+ .branch_head()
+ .unwrap()
+ .tree()
+ .and_then(|tree| tree.get_path(Path::new(&filename)))
+ .map_err(|_| ScriptError::ModuleNotFound)?;
+
+ let lua_blob = ctx.repo.find_blob(lua_entr.id()).unwrap();
+ let lua_code = from_utf8(lua_blob.content()).map_err(|_| ScriptError::ModuleNotUtf8)?;
+
+ lua_context(|ctx| {
+ let module: Table = ctx.load(lua_code).eval()?;
+ let view: Function = module.get(modfn.function_name)?;
+ let lua_args = ctx.create_table()?;
+ for (idx, val) in args {
+ match idx {
+ ArgIndex::Str(s) => lua_args.set(s, val)?,
+ ArgIndex::Num(n) => lua_args.set(n, val)?,
+ }
+ }
+ view.call(lua_args)
+ })
+ .map_err(ScriptError::LuaError)
+}
+
impl Display for ScriptError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {