From 56c593fcc70497b4ae31a52a520fc7bb441e5cbf Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Sun, 16 Jun 2024 00:50:54 +0300 Subject: [PATCH] feat(api): Mockup how to use and test a Rust function from Lua --- core/sile.lua | 4 ++++ rusile/src/lib.rs | 6 ++++++ spec/rusile_spec.lua | 16 ++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 spec/rusile_spec.lua diff --git a/core/sile.lua b/core/sile.lua index 037e96c46e..9fa10566ff 100644 --- a/core/sile.lua +++ b/core/sile.lua @@ -16,6 +16,10 @@ SILE = {} --- Fields -- @section fields +--- Functions implemented in Rust, to be moved to approprate places in the API +-- @table SILE.rusile +SILE._rusile = require("rusile") + --- Machine friendly short-form version. -- Semver, prefixed with "v", possible postfixed with ".r" followed by VCS version information. -- @string version diff --git a/rusile/src/lib.rs b/rusile/src/lib.rs index c5bb5e7231..f237201743 100644 --- a/rusile/src/lib.rs +++ b/rusile/src/lib.rs @@ -7,5 +7,11 @@ use mlua::prelude::*; #[mlua::lua_module] fn rusile(lua: &Lua) -> LuaResult { let exports = lua.create_table().unwrap(); + let foo: LuaFunction = lua.create_function(foo).unwrap(); + exports.set("foo", foo)?; Ok(exports) } + +fn foo(lua: &Lua, (): ()) -> LuaResult { + lua.create_string("Hello from rusile") +} diff --git a/spec/rusile_spec.lua b/spec/rusile_spec.lua new file mode 100644 index 0000000000..3eed95c27e --- /dev/null +++ b/spec/rusile_spec.lua @@ -0,0 +1,16 @@ +SILE = require("core.sile") + +describe("rusile", function () + + it("should exist", function () + assert.is.truthy(SILE._rusile) + end) + + describe("foo ", function () + it("should return a test string", function () + local str = "Hello from rusile" + assert.is.equal(str, SILE._rusile.foo()) + end) + end) + +end)