Skip to main content

Crate serpent_serializer

Crate serpent_serializer 

Source
Expand description

Serialize Lua values to round-trippable Lua source.

This crate turns a Value graph into Lua source code that Lua’s load reads back into an equivalent value graph. It is both a serializer and a pretty printer. It preserves shared references, reconstructs cyclic references, and emits arrays positionally, string keys in short notation, and the special numbers as 1/0, -1/0, 0/0.

§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.
  • block produces multi-line indented output. Also an expression.
  • [serialize] is the raw entry point with no default options.
  • [load] parses serialized output back into a Value.

§Example

use serpent_serializer::{line, load, LoadOptions};
use serpent_serializer::value::{Table, Key, 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 = line(&Value::Table(t)).unwrap();
let back = load(&src, &LoadOptions::default()).unwrap();
assert!(matches!(back, Value::Table(_)));

Re-exports§

pub use load::load;
pub use load::LoadError;
pub use load::LoadOptions;
pub use options::Options;
pub use serialize::serialize;
pub use serialize::SerError;
pub use value::Func;
pub use value::Global;
pub use value::Ident;
pub use value::Key;
pub use value::MetaError;
pub use value::MetaFn;
pub use value::Table;
pub use value::TableData;
pub use value::Value;

Modules§

load
The deserializer.
numfmt
C printf number formatting for %g and %e conversions.
options
Serialization options.
quote
Lua string-literal escaping.
serialize
The serializer core.
value
The Lua value model.

Constants§

COPYRIGHT
Copyright holder, matching serpent’s _COPYRIGHT.
DESCRIPTION
Description, matching serpent’s _DESCRIPTION.
NAME
Module name, matching serpent’s _NAME.
VERSION
Version string, matching serpent’s _VERSION.

Functions§

block
Multi-line indented pretty printing. Two-space indent, sortkeys, comment on.
block_with
Multi-line pretty printing with caller options. Build the options over Options::block, then override the fields you need.
dump
Full serialization: name = "_", compact, sparse.
dump_with
Full serialization with extra options merged over the dump defaults.
line
Single-line pretty printing. sortkeys and comment on.
line_with
Single-line pretty printing with caller options. Build the options over Options::line, then override the fields you need.