mlua-batteries 0.1.0

Batteries-included standard library modules for mlua
Documentation

Batteries-included standard library modules for mlua.

Each module exposes a single module(lua) -> LuaResult<LuaTable> entry point. Register individually or use [register_all] for convenience.

Platform support

This crate targets Unix server platforms (Linux, macOS). Windows is not a supported target.

Encoding — UTF-8 only (by design)

All path arguments are received as Rust [String] (UTF-8). Non-UTF-8 Lua strings are rejected at the FromLua boundary. Returned paths use to_string_lossy, replacing any non-UTF-8 bytes with U+FFFD.

Why not raw bytes / OsStr?

mlua's FromLua for String performs UTF-8 validation — non-UTF-8 values produce FromLuaConversionError before reaching handler code. Bypassing this would require accepting mlua::String + as_bytes() in every function, converting through OsStr::from_bytes(), and returning OsStr::as_bytes() back to Lua. This adds complexity across all path-accepting functions for a scenario (non-UTF-8 filenames) that is rare on modern systems.

References:

Quick start

use mlua::prelude::*;

let lua = Lua::new();
mlua_batteries::register_all(&lua, "std").unwrap();
// Lua: std.json.encode({a = 1})
// Lua: std.env.get("HOME")

Custom configuration

// Requires the `sandbox` feature.
use mlua::prelude::*;
use mlua_batteries::config::Config;
use mlua_batteries::policy::Sandboxed;

let lua = Lua::new();
let config = Config::builder()
    .path_policy(Sandboxed::new(["/app/data"]).unwrap().read_only())
    .max_walk_depth(50)
    .build()
    .expect("invalid config");
mlua_batteries::register_all_with(&lua, "std", config).unwrap();