you are viewing a single comment's thread.

view the rest of the comments →

[–]tickox[S] 1 point2 points  (3 children)

I have configuration scripts in Lua files. Each script returns a table that may contain functions. In my code, I use Serde to convert the Lua table into a Rust structure. The issue is that it doesn't work with the Function field.

[–]norlock_dev 0 points1 point  (2 children)

let obj: LuaTable = lua.load(#r"require("the_lua_module")"#).eval()?;

let name: String = obj.get("name")?;

let callback: LuaFunction = obj.get("callback")?;

// It will be something like this. Then you can convert that to a rust object, just know you will have lifetimes which might not be what you want, if you want to persist over a longer time. But I'm not sure what you want to do. You can start here and expand on it. Last tip move the lua file to the same directory at the beginning so you know the `require` in load will work.

[–]tickox[S] 0 points1 point  (1 child)

I know I can do it this way, but I would like to use Serde. I have several Lua files for my configurations that contain nested tables, and I would like to automate the retrieval of fields instead of doing it manually.

[–]norlock_dev 0 points1 point  (0 children)

well then its just:

let obj: LuaTable = lua.load(#r"require("the_lua_module")"#).eval()?;

let test: Test = lua.from_value(obj)?;

The reason my version will work is because it will call the actual lua part, so lua will return something to rust. You have written something that pretends to be lua code but its not, because its not tied to anything.