You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am quite new to mlua and lua. I have an idea for the architecture of my project when it comes to mlua. I arrived at something working, but I'd love it if someone more experienced could reassure me that I am on the right path.
I would like users to be able to write a config file in lua. However they should also be able to specify some run time behavior there. Essentially, there will be a "config evaluation run time" and then the "actual run time" of the program. I am taking inspiration from wezterm for the config structure.
Here is how a wezterm config file could look like:
localwezterm=require("wezterm")
localconfig=wezterm.config_builder()
config.somekey="exampleval"config.some.sub= {
someother=2,
}
-- here one can define some kind of "event handlers" in wezterm.wezterm.on("someevent", function () print("someevent happened") end)
returnconfig-- what gets returned is purely data that is (de)serializable
The returned config is then turned into a rust Config struct, I think the event handlers are stored in some kind of function registry.
However, I want to allow the user to define regular lua functions as values for some config keys because I think that is more ergonomic. Here is an example:
The config is then also deserialized into a Config struct that the program uses. Now, I really want to use the serde feature and keep rust types as a sort of source of truth. Mainly because I think this will make it easier later to generate LuaLS type definitions from them.
I know that trying to serialize functions is a bad idea. So, here is the workaround/poc I have arrived at:
But.. is this a good, even decent idea? I suppose I would have to walk the config "tree"/nested table multiple times for this which doesn't seem right. Does this perhaps have some implications I didn't consider? It seems weird because I'm almost fighting serde at that point. Really any advice is welcome.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am quite new to mlua and lua. I have an idea for the architecture of my project when it comes to mlua. I arrived at something working, but I'd love it if someone more experienced could reassure me that I am on the right path.
I would like users to be able to write a config file in lua. However they should also be able to specify some run time behavior there. Essentially, there will be a "config evaluation run time" and then the "actual run time" of the program. I am taking inspiration from wezterm for the config structure.
Here is how a wezterm config file could look like:
The returned config is then turned into a rust
Config
struct, I think the event handlers are stored in some kind of function registry.However, I want to allow the user to define regular lua functions as values for some config keys because I think that is more ergonomic. Here is an example:
The config is then also deserialized into a
Config
struct that the program uses. Now, I really want to use theserde
feature and keep rust types as a sort of source of truth. Mainly because I think this will make it easier later to generate LuaLS type definitions from them.I know that trying to serialize functions is a bad idea. So, here is the workaround/poc I have arrived at:
This seems to work and here is the output:
But.. is this a good, even decent idea? I suppose I would have to walk the config "tree"/nested table multiple times for this which doesn't seem right. Does this perhaps have some implications I didn't consider? It seems weird because I'm almost fighting serde at that point. Really any advice is welcome.
Beta Was this translation helpful? Give feedback.
All reactions