Runtime API

For incremental parsing in context, see 4. Try partial input. For mapping-based operation examples, see How to compile mapping-based schemas. rustruct.compile() produces a rustruct.Codec; a rustruct.Struct subclass compiles and holds one internally.

Compilation and codec

rustruct.compile(fields, *, byteorder='big', max_default=67108864, max_count=16777216)
class rustruct.Codec
min_size

Lower bound of the size.

pack_into(buf, offset, values)

Writes into an existing writable buffer, returns the new position.

parse(buf, offset=0)

Streaming parse: a data shortage yields Incomplete, not an exception.

static_size

Exact size, if the schema is static.

unpack(buf)

Requires the buffer to be fully consumed; a tail raises InvalidDataError with kind=”trailing”.

unpack_from(buf, offset=0)

A trailing tail is allowed; returns (dict, new position).

Streaming result

class rustruct.Incomplete

The result of parse() when data is missing: falsy.

needed

Minimum bytes missing beyond the end of the buffer (a lower bound).

Incomplete is falsy and exposes needed.

Errors

PackError and InvalidDataError set kind/path/offset on the raised instance rather than exposing them as class attributes, so they’re documented by hand below rather than through autodoc introspection.

exception rustruct.RustructError

Base rustruct error.

exception rustruct.SchemaError

compile() error.

exception rustruct.PackError

Values don’t fit the schema (pack).

kind: str

Machine-readable failure category, e.g. "range" or "inconsistent".

path: str

Dotted field path where the failure occurred.

exception rustruct.InvalidDataError

Corrupt data (unpack).

kind: str

Machine-readable failure category, e.g. "checksum" or "tag".

path: str

Dotted field path where the failure occurred.

offset: int

Byte offset in the input where the failure was detected.