diff options
author | Martin Fischer <martin@push-f.com> | 2022-08-13 03:56:55 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2022-08-14 00:33:23 +0200 |
commit | 36e98c1b135f07ef9a5eec046b8f0fd6d93534d4 (patch) | |
tree | da9c93ceb0e147695e1992c4d1678964af3be4bf /src/lua.rs | |
parent | dc20e1df60c1e4e81d1e16e8f177a1c6956966b7 (diff) |
add gitpad.decode_toml lua method
We are vendoring the rlua_serde crate because it currently depends on
rlua 0.17, which is outdated and my attempts to contact the crate author
were bounced by Yandex for somehow looking like spam.
Diffstat (limited to 'src/lua.rs')
-rw-r--r-- | src/lua.rs | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -1,15 +1,20 @@ use std::fmt::Display; +use std::ops::Index; use std::path::Path; use std::str::from_utf8; +use std::sync::Arc; use rlua::Function; use rlua::HookTriggers; use rlua::Lua; +use rlua::MetaMethod; use rlua::StdLib; use rlua::Table; use crate::Context; +mod serde; + pub struct Script<'a> { pub lua_module_name: &'a str, input: &'a str, @@ -80,6 +85,23 @@ impl<'a> Script<'a> { ) .unwrap(); + ctx.globals() + .get::<_, Table>("gitpad") + .unwrap() + .set( + "decode_toml", + ctx.create_function(|_ctx, text: String| { + toml::from_str::<toml::Value>(&text) + .map_err(|e| rlua::Error::ExternalError(Arc::new(e))) + .and_then(|v| { + serde::to_value(_ctx, v) + .map_err(|e| rlua::Error::ExternalError(Arc::new(e))) + }) + }) + .map_err(ScriptError::LuaError)?, + ) + .unwrap(); + let module: Table = ctx.load(lua_code).eval().map_err(ScriptError::LuaError)?; let view: Function = module.get("view").map_err(ScriptError::LuaError)?; |