diff options
-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) }) |