serpent-serializer 0.2.0

Serialize Lua values to round-trippable Lua source with cycle and shared-reference handling
Documentation
  • Coverage
  • 100%
    114 out of 114 items documented2 out of 44 items with examples
  • Size
  • Source code size: 156.1 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.05 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • hey-jj/serpent-serializer
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • hey-jj

serpent-serializer

Serialize Lua values to round-trippable Lua source, and load that source back.

The output is Lua source code that Lua's load reads into an equivalent value graph. It preserves shared references, reconstructs cyclic references, emits arrays positionally, uses short notation for identifier keys, and writes the special numbers as 1/0, -1/0, and 0/0.

Installation

[dependencies]
serpent-serializer = "0.1"

Entry points

  • dump produces full serialization wrapped in a do ... return _ end block with a self-reference section. Compact and sparse by default.
  • line produces single-line output. It is an expression, so prepend return to evaluate it. load does this for you.
  • block produces multi-line indented output. Also an expression.
  • serialize is the raw entry point that takes options as given.
  • load parses serialized output back into a Value.

Example

use serpent_serializer::{block, load, LoadOptions};
use serpent_serializer::value::{Key, Table, Value};

let t = Table::new();
t.push(Value::Str(b"a".to_vec()));
t.push(Value::Str(b"b".to_vec()));
t.set(Key::Str(b"x".to_vec()), Value::Number(1.0));

let src = block(&Value::Table(t)).unwrap();
let back = load(&src, &LoadOptions::default()).unwrap();
assert!(matches!(back, Value::Table(_)));

Options

Build options over Options::dump, Options::line, or Options::block, then override the fields you need. Supported options include name, indent, compact, sparse, fatal, maxnum, maxlevel, maxlength, nocode, nohuge, comment, sortkeys, a custom sort, metatostring, numformat, the value and key ignore filters, and a custom table formatter.

Safety

load runs in a sandbox by default. Any function call in the input is rejected with "cannot call functions", and any global read or assignment resolves against the sandbox, so it cannot reach real state. Set safe to Some(false) to disable the call check.

Functions do not round-trip. The default output reloads a function through a call, which the sandbox rejects, and load cannot run function bodies in any case. Set nocode to emit an empty stub instead, which loads back as nil.

License

Licensed under the MIT license.