MLua Extras
[!WARNING] This crate is under heavy active development. All features are currently experimental and are subject to change partially or fully at any time without notice.
[!NOTE] Feel free to use this crate and start workshopping ideas and features that could be useful.
PR's and Changes are welcome; Some topics may inclue API reworks/restruture, additional type information, more type generators, etc...
If you want to discuss this project, you can do that here
Add helpers for common coding styles with mlua
.
The biggest part of this library is adding lua type information and doc comments. The typing is a light wrapper around UserData
and its related traits UserDataFields
and UserDataMethods
. There is a Definition generator that allows you to group the type definitions (entries), commonly thought of as definition files. From this API a generator for definition files, documentation, or anything else could be built on top. This library will also provide a basic definition file generator.
Inspiration
I really enjoy having types information when writing my code. However, it can be a pain to write and maintain the types for lua while also writing the rust code and rust types. This is where this libarary comes in. Using ideas from Tealr
, this library adds a wrapper around [mlua
]'s traits and types to automatically get type information and doc comments for rust types.
Not all the pain of maintenance is gone as you have to explicitly say which types are going to be used and where. However, it greatly reduces the pain point and keeps the types and documentation close to where the code is written.
The hope is that this library will provide an API to generate definition files, lsp addons, and lua api documentation.
While we are already creating an API this library also adds some usefull helper traits and macros to make writing rust code for the lua engine much easier.
Pain Point
There is still one huge pain point with this library. Since it implements traits for the mlua
crate, it requires that it is a dependency. The mlua
crate will not compile unless a version of lua is provided. This is bit of problem since it will fail to compile if your project depends on it with one lua version and this crate depends on it with a different version. With this in mind, this crate re-exposes mlua
and exposes most of it's features. With this the crate can model it's traits and implementations based on how you want to use lua.
Just make sure you use the exposed mlua
crate through this crates API.
Features
-
Helper Traits
Require
- call
require
which allows for a lua style call to get data from the lua engine. - ex:
table.require::<String>("nested.tables.name")?
==local name = require('nested.tables').name
- call
LuaExtras
- Manipulate the lua
path
andcpath
variables withappend
,prepend
, andset
methods for each variant. It also includes the ability to add multiple paths with each variant. - Set global variables and functions with
set_global("value", "value")
andset_global_function("func", |lua, ()| Ok(()))
which wold replacelua.globals().set("value", "value)
andlua.globals().set("func", lua.create_function(|lua, ()| Ok(()))?)
respectively
- Manipulate the lua
-
Typed Lua Traits
Typed
- Generate a
Type
andParam
for a rust type so it can be used both as a type and as a parameter for a function
- Generate a
TypedUserData
- Typed variant of
mlua::UserData
with an additionaladd_documentation
method to add doc comments to theUserData
type - An extra
document
method is added to theTypedDataFields
andTypedDataMethods
foradd_fields
andadd_methods
. This will queue doc comments to be added to the next field or method that is added. - All types from function parameters and and return types are stored for fields, functions, and methods.
- This trait is mainly used when generating type definitions. If it is called through the
UserData
derive macro it will ignore all types and documentation
- Typed variant of
TypedDataFields
: Implemented on a generator forTypedUserData
(add_fields
)TypedDataMethods
: Implemented on a generator forTypedUserData
(add_methods
)TypedDataDocumentation
: Implemented on a generator forTypedUserData
(add_documentation
)
-
Derive Macros
Typed
: Auto implement theTyped
trait to get type information for bothstruct
andenum
UserData
: Auto implement themlua::UserData
trait for rust types that also implementTypedUserData
. This will pass through theUserData
add_methods
andadd_fields
to theTypedUserData
's version. This will ignore all documentation and types.
-
Macros
function
: Write lua functions more like Rust's syntax
Instead of this:
lua.create_function
You can now write this:
function!
The difference isn't huge, but it could make the syntax more readable.
[!NOTE] This also helps with assigning functions to nested tables.
[!WARNING] This requires the
LuaExtras
trait when adding functions to nested tables withlua
as the starting point. This requires theRequire
trait when starting from any other table.lua..set; // vs function!
Example
Helpers
use ;
use ;
Types
use Deserialize;
use ;
Produces the following definition file
--- init.d.lua
--- @meta
--- @alias SystemColor "Black"
--- | "Red"
--- | "Green"
--- | "Yellow"
--- | "Blue"
--- | "Cyan"
--- | "Magenta"
--- | "White"
--- @alias Color SystemColor
--- | integer
--- | { [1]: integer, [2]: integer, [3]: integer }
--- This is a doc comment section for the overall type
--- @class Example
--- Example complex type
--- @field color Color
--- Example module
--- @type Example
example = nil
--- Greet the name that was passed in
--- @param param0 string
--- Print a color and it's value
--- @param param0 Color