Crate tealr[][src]

Expand description

tealr

A wrapper around rlua and/or mlua to help with embedding teal

tealr adds some traits that replace/extend those from rlua and mlua, allowing it to generate the .d.tl files needed for teal. It also contains some macro’s to make it easier to load/execute teal scripts.

Note:

Both rlua and mlua are behind feature flags and both feature flags can be enabled at the same time.

Expose a value to teal

Exposing types to teal as userdata is almost the same using tealr as it is using rlua and mlua

use tealr::TypeName;
#[derive(Clone, tealr::RluaUserData, TypeName)]
struct ExampleRlua {}

//now, implement rlu::TealData.
//This tells rlua what methods are available and tealr what the types are
impl tealr::rlu::TealData for ExampleRlua {
   //implement your methods/functions
   fn add_methods<'lua, T: tealr::rlu::TealDataMethods<'lua, Self>>(methods: &mut T) {
       methods.add_method("example_method", |_, _, x: i8| Ok(x));
       methods.add_method_mut("example_method_mut", |_, _, x: (i8, String)| Ok(x.1));
       methods.add_function("example_function", |_, x: Vec<String>| Ok((x, 8)));
       methods.add_function_mut("example_function_mut", |_, x: (bool, Option<ExampleRlua>)| {
           Ok(x)
       })
   }
}

Working with mlua is almost the same

use tealr::TypeName;
#[derive(Clone, tealr::MluaUserData, TypeName)]
struct ExampleMlua {}
impl tealr::mlu::TealData for ExampleMlua {
   //implement your methods/functions
   fn add_methods<'lua, T: tealr::mlu::TealDataMethods<'lua, Self>>(methods: &mut T) {
       methods.add_method("example_method", |_, _, x: i8| Ok(x));
       methods.add_method_mut("example_method_mut", |_, _, x: (i8, String)| Ok(x.1));
       methods.add_function("example_function", |_, x: Vec<String>| Ok((x, 8)));
       methods.add_function_mut("example_function_mut", |_, x: (bool, Option<ExampleMlua>)| {
           Ok(x)
       })
   }
}

Create a .d.tl file

Creating of the .d.tl files works the same for rlua or mlua

let file_contents = tealr::TypeWalker::new()
    .process_type::<Example>(tealr::Direction::ToLua)
    .generate_global("test")
    .expect("oh no :(");

println!("{}",file_contents)

Compile inline teal code into lua

As you get a string containing the lua code back this feature works the same for both rlua and mlua

let code = compile_inline_teal!("local x : number = 5 return x");

Embed the teal compiler, run teal files as if they where lua

Rlua:

let compiler = embed_compiler!("v0.10.0");
let res = rlua::Lua::new().context(|ctx| {
    let code = compiler("example/basic_teal_file");
    let res: u8 = ctx.load(&code).set_name("embedded_compiler")?.eval()?;
    Ok(res)
})?;

Mlua:

let compiler = embed_compiler!("v0.10.0");
let lua = mlua::Lua::new();
let code = compiler("example/basic_teal_file");
let res: u8 = lua.load(&code).set_name("embedded_compiler")?.eval()?;

You can find longer ones with comments on what each call does here

Future plans

Tealr can already help with 2 ways to run teal scripts. It can compile inline teal code at the same time as your rust code It can also embed the teal compiler for you, allowing you to execute external teal scripts like normal lua scripts. There is a third method I want tealr to help with. In this mode, it will compile a teal project, pack it into 1 file and embed it into the project.

Modules

mlu

traits and types specific to mlua

rlu

traits and types specific to rlua

Macros

compile_inline_teal

Compiles the given teal code at compile time to lua.

embed_compiler

Embeds the teal compiler, making it easy to load teal files directly.

Structs

ExportedFunction

Contains the data needed to write down the type of a function

TealType

Represents a type

TypeGenerator

This struct collects all the information needed to create the .d.tl file for your type.

TypeWalker

This generates the .d.tl files

Enums

Direction

The direction that the rust <-> lua conversion goes to. This is needed in case the FromLua and ToLua traits aren’t perfect opposites of each other.

Traits

TealMultiValue

A collection of TealValues.

TypeBody

Creates the body of the type, so the functions and fields it exposes.

TypeName

A trait to collect the required type information like the name of the type.

Derive Macros

MluaTealDerive

Implement both mlua::UserData and tealr::TypeName.

MluaUserData

Implements mlua::UserData and tealr::TypeBody

RluaTealDerive

Implement both rlua::UserData and tealr::TypeName.

RluaUserData

Implements rlua::UserData and tealr::TypeBody

TypeName

Implements tealr::TypeName.