Skip to main content

minijinja_lua/
lib.rs

1// SPDX-License-Identifier: MIT
2
3pub mod contrib;
4mod convert;
5mod environment;
6mod state;
7
8use mlua::LuaSerdeExt;
9
10use crate::convert::{LuaSyntaxConfig, autoescape_lua, undefined_behavior_lua};
11pub use crate::{environment::LuaEnvironment, state::LuaState};
12
13/// Builds and returns the `minijinja` lua table.
14///
15/// The returned table has the following keys:
16///
17/// - [`Environment`](LuaEnvironment): `userdata` provides access to a [`minijinja::Environment`]
18/// - [`None`](mlua::Value::NULL): a nil-like `lightuserdata` that can be used without the downsides
19///   of `nil`. It maps to [`minijinja::value::ValueKind::None`]
20/// - `path_loader()`: creates a callback function which can be passed to `Environment:set_loader()`
21///   to load templates from the filesystem.
22/// - `type()`: a function to return types for minijinja-lua objects.
23#[cfg_attr(feature = "module", mlua::lua_module(name = "minijinja"))]
24pub fn minijinja_lua(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
25    let table = lua.create_table()?;
26
27    table.set("type", mlua::Function::wrap(contrib::minijinja_types))?;
28    table.set("path_loader", contrib::minijinja_path_loader(lua)?)?;
29
30    table.set("None", lua.null())?;
31    table.set("AutoEscape", autoescape_lua(lua)?)?;
32    table.set("UndefinedBehavior", undefined_behavior_lua(lua)?)?;
33    table.set("Environment", lua.create_proxy::<LuaEnvironment>()?)?;
34    table.set("SyntaxConfig", lua.create_proxy::<LuaSyntaxConfig>()?)?;
35
36    Ok(table)
37}