schema-bridge-lshape 0.2.0

lshape exporter for schema-bridge — generates lshape (Lua Schema-as-Data) type definitions from schema-bridge's Schema IR
Documentation

schema-bridge-lshape

lshape exporter for schema-bridge — generates lshape (Lua Schema-as-Data) type definitions from schema-bridge's Schema IR.

This is a bridge crate: it depends on schema-bridge-core for the IR and emits plain Lua source text, so it needs no Lua runtime at generation time. The generated module is validated against a real Lua VM in the test suite via mlua-lshape.

Quick start

use schema_bridge::SchemaBridge;
use schema_bridge_lshape::export_lshape_types;

#[derive(SchemaBridge)]
struct User {
    name: String,
    age: u8,
    email: Option<String>,
}

export_lshape_types!("types.lua", User)?;

types.lua:

-- This file is auto-generated by schema-bridge-lshape. Do not edit.
local T = require("lshape").t

local M = {}

M.User = T.shape({
    name = T.string,
    age = T.integer,
    email = T.string:is_optional(),
})

return M

Use it from Lua (e.g. under mlua-lshape's install):

local check = require("lshape").check
local types = require("types")
local ok, why = check.check({ name = "a", age = 30 }, types.User)

Lower-level entry points: schema_to_lshape(&Schema) (single expression), generate_lshape_file(&[(&str, Schema)]) (module text), and export_to_lua_file (write to disk).

Type mapping

Schema IR lshape
String / Number / Boolean / Any T.string / T.number / T.boolean / T.any
Integer T.integer (lshape 0.3+; whole-number check, 2.0 passes)
Array(T) T.array_of(...)
Record { key, value } T.map_of(k, v)
Object(fields) T.shape({...}), optional fields get :is_optional()
Enum(values) T.one_of({...})
Union(variants) T.any_of({...}); a Null variant is stripped and folded into :is_optional()
Tuple(items) T.tuple({...}) (lshape 0.3+; fixed length, per-position schemas)
Ref(name) T.ref("name")
Constraints.one_of T.one_of({...})
Constraints min/max/min_len/max_len :min(..) / :max(..) / :min_len(..) / :max_len(..) (lshape 0.3+; enforced at check time)

A standalone Schema::Null (outside a union) is an error — lshape has no nil type; nullability is expressed with :is_optional(). An empty Tuple is likewise an error (T.tuple requires at least one item; not reachable via the derive macro).

lshape version requirement

Generated modules use T.integer / T.tuple / the bounds combinators, which require lshape 0.3.0+ at load time (vendored by mlua-lshape 0.3+).

Semantic note: T.integer is lshape's portable whole-number check (v % 1 == 0), so 2.0 passes; Rust-side integer range (e.g. u8) is not carried into the schema.