diff options
author | Martin Fischer <martin@push-f.com> | 2022-10-28 12:16:24 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2022-10-28 12:26:03 +0200 |
commit | d050c761c0940397b5808934937a8d5e13e670d4 (patch) | |
tree | 02d66685e7aeb308751dd4e09eb230531e0a02a4 | |
parent | 20555bba82f9420998ce0d4b42f29d9398d5036b (diff) |
refactor: avoid unnecessary LuaTable::get call
-rw-r--r-- | src/lua.rs | 42 |
1 files changed, 18 insertions, 24 deletions
@@ -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) }) |