aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2022-10-28 12:16:24 +0200
committerMartin Fischer <martin@push-f.com>2022-10-28 12:26:03 +0200
commitd050c761c0940397b5808934937a8d5e13e670d4 (patch)
tree02d66685e7aeb308751dd4e09eb230531e0a02a4
parent20555bba82f9420998ce0d4b42f29d9398d5036b (diff)
refactor: avoid unnecessary LuaTable::get call
-rw-r--r--src/lua.rs42
1 files changed, 18 insertions, 24 deletions
diff --git a/src/lua.rs b/src/lua.rs
index e9c4841..d8d82b2 100644
--- a/src/lua.rs
+++ b/src/lua.rs
@@ -44,30 +44,24 @@ pub fn lua_context<A>(run: impl FnOnce(rlua::Context) -> rlua::Result<A>) -> rlu
);
lua.context(|ctx| {
- ctx.globals()
- .raw_set(
- "gitpad",
- ctx.load(include_str!("static/api.lua"))
- .eval::<Table>()
- .expect("error in api.lua"),
- )
- .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)))
- })
- })?,
- )
- .unwrap();
+ let api: Table = ctx
+ .load(include_str!("static/api.lua"))
+ .eval::<Table>()
+ .expect("error in api.lua");
+
+ api.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)))
+ })
+ })?,
+ )?;
+
+ ctx.globals().raw_set("gitpad", api).unwrap();
run(ctx)
})