Skip to main content

Rust core for parsing and building binary wire formats from Python

Project description

rustruct

A Rust core for parsing and building binary wire formats from Python.

A declarative schema built from Python primitives is compiled into a program executed with a single Python -> Rust call for the whole structure. The parse result is a plain dict.

Building and testing

# Rust core (no Python)
cargo test -p rustruct-core

# Python package (maturin backend) + pytest
uv sync
uv run pytest

# Sphinx documentation (warnings are errors)
make test-docs

Example

from rustruct import Field, Incomplete, compile

codec = compile(
    (
        Field(name="tag", kind="u8", opts={}),
        Field(name="size", kind="u8", opts={}),
        Field(
            name="value",
            kind="struct",
            opts={
                "fields": (Field(name="payload", kind="bytes", opts={"len": "*"}),),
                "size": ("ref", "size"),
            },
        ),
        Field(name="crc", kind="digest", opts={"algo": "crc32", "over": "*"}),
    )
)

data = codec.pack({"tag": 7, "value": {"payload": b"abc"}})
# size and crc are derived: computed and patched in automatically

values = codec.unpack(data)  # full buffer, a tail is an error
values, pos = codec.unpack_from(data, 0)  # a tail is allowed

r = codec.parse(data[:3])  # streaming parse
if not r:  # Incomplete: not enough data yet
    print("need at least", r.needed, "more bytes")

Documentation

The full documentation starts at docs/index.md and is organised by user need: a guided tutorial, task-oriented how-to guides, explanations of the schema and execution model, and concise API reference.

Declarative frontend

A Struct metaclass on top of compile()/Codec: class-body annotations and field descriptors (described, slice, array, switch, bits, convert, sized, an open registry()) compile lazily, per class, into a Codec, and convert to/from typed instances rather than plain dicts.

from rustruct import Struct, U8, U16, switch, slice, registry

Payload = Struct  # a registry base: class Foo(Payload, registry=True): ...

See src/rustruct/protocols/{inet,tcp,udp,dns}.py for a full worked example (IPv4/TCP/UDP header dispatch via an open registry, DNS with hand-written name-compression as the one piece that doesn't fit the declarative model) and tests/test_frontend_*.py, tests/test_protocols_*.py for the tests.

Repository layout

crates/rustruct/       core (rustruct-core): schema compiler, unpack/pack,
                       expressions, bits, flags, windows, digests; zero pyo3
crates/rustruct-py/    pyo3 cdylib -> the rustruct.core module
src/rustruct/          Python wrapper: low-level re-export, __abi__ check,
                       the Struct frontend (struct.py/fields.py/scalars.py/
                       expr.py), and protocols/ (IPv4/TCP/UDP/DNS example)
tests/                 pytest tests: public API, frontend, protocols
docs/                  tutorials, how-to guides, explanation, API reference
crates/rustruct/tests/ Rust integration tests, one file per feature, plus
                       tests/common/mod.rs for shared helpers
benchmark/             a separate uv project comparing rustruct against
                       struct/ctypes/dataclasses-struct/construct

Implementation status

Covered:

  • all fixed-width types, raw, bytes/str/cstr, bits (MSB-first), flags (keep/strict/ignore), struct (including size windows), array (count/until_eof), switch, digest (CRC presets with Rocksoft overrides, md5/sha1/sha256, over as a name tuple or "*" with self-zeroing);
  • expressions (the full operator set), lexical backward-only ref resolution, registers and span registers, limits (max, max_count, depth 64, an 8-deep Expr stack);
  • pack with backpatching: derived lengths (linear inversion a*x + b, including derived bits fields), digests computed innermost-first, consistency checks across multiple consumers of one register;
  • the streaming contract: parse -> Incomplete.needed with monotonic progress, distinguishing "hit a window boundary" (Invalid) from "ran out of buffer" (Incomplete);
  • errors carry kind/path/offset; the path is assembled only while unwinding;
  • coalescing of fixed fields (a static schema lowers to exactly one Fixed op, enforced by a test), min_size/static_size.

Known gaps and limitations:

  • to_bytes/from_bytes (Program serialization) raise NotImplementedError; the cache format hasn't been designed.
  • A switch discriminant is never derived from its own on= ref at pack time: the caller always supplies it explicitly, and a field cannot be both derived and a switch discriminant (SchemaError).
  • byteorder accepts "network" as a struct-module-style alias for "big". "native" is forbidden, since it would make the wire format depend on the machine running the code.
  • String encodings in the core: utf-8 / ascii / latin-1, errors="strict" only. Other CPython codecs are a frontend concern; the core currently raises SchemaError for anything else.
  • An extra digest.algo="ip" preset (RFC 1071, the IPv4 header checksum) is needed for the bundled IPv4 reference format.
  • No cargo-fuzz targets and no refcount tests yet; deterministic and randomized round-trip tests stand in for a proper proptest-based fuzz suite.
  • The hot path still builds an intermediate Value tree (the core stays Python-free); raw FFI and METH_FASTCALL benchmarking haven't been done.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rustruct-0.1.3.tar.gz (90.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

rustruct-0.1.3-cp311-abi3-win_arm64.whl (292.7 kB view details)

Uploaded CPython 3.11+Windows ARM64

rustruct-0.1.3-cp311-abi3-win_amd64.whl (304.1 kB view details)

Uploaded CPython 3.11+Windows x86-64

rustruct-0.1.3-cp311-abi3-musllinux_1_2_x86_64.whl (649.1 kB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ x86-64

rustruct-0.1.3-cp311-abi3-musllinux_1_2_aarch64.whl (608.4 kB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ ARM64

rustruct-0.1.3-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (435.0 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

rustruct-0.1.3-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (430.3 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARM64

rustruct-0.1.3-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (772.3 kB view details)

Uploaded CPython 3.11+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file rustruct-0.1.3.tar.gz.

File metadata

  • Download URL: rustruct-0.1.3.tar.gz
  • Upload date:
  • Size: 90.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for rustruct-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c5f94dc6e9cb13e46a8c58ff4e50ef389ccdf31ae79a3b7c38a029b9fa86c638
MD5 bec8f0888e4a24f3695b1e5c6c846f5a
BLAKE2b-256 e94957104ff029340deb5d0505050d051e576ab51ee63ef13e40a4c4a95338df

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustruct-0.1.3.tar.gz:

Publisher: publish.yml on mosquito/rustruct

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rustruct-0.1.3-cp311-abi3-win_arm64.whl.

File metadata

  • Download URL: rustruct-0.1.3-cp311-abi3-win_arm64.whl
  • Upload date:
  • Size: 292.7 kB
  • Tags: CPython 3.11+, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for rustruct-0.1.3-cp311-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 80ae49be25c3450a48a6a6d92766797b8d04cf0e792669fbf3e10fc91b686c1f
MD5 ecb8f49d5cdeefe8b4ae5d8459472f83
BLAKE2b-256 0e9baccdd81de103d97165c4075f515736901ab14e2c14fe202b32c52aa1f23d

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustruct-0.1.3-cp311-abi3-win_arm64.whl:

Publisher: publish.yml on mosquito/rustruct

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rustruct-0.1.3-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: rustruct-0.1.3-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 304.1 kB
  • Tags: CPython 3.11+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for rustruct-0.1.3-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 92e12bc0f99b1bc29b600a8315d0483f2789da13116de7478f3a9985c713660f
MD5 c7ef99b5405e490bf139fbfe4c9a0948
BLAKE2b-256 49795ba7e033872e0c0e19f2b7a6c83b74fe3420ff6eb0e2b0566249d286a8f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustruct-0.1.3-cp311-abi3-win_amd64.whl:

Publisher: publish.yml on mosquito/rustruct

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rustruct-0.1.3-cp311-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for rustruct-0.1.3-cp311-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 224d4ddaa011f5e6d46aa368c9b584e1885501b9b74b971551c8181765a53388
MD5 9631c137be483f9aade33e6ffb331c06
BLAKE2b-256 52d1bd8678b5281fae09d1b0fd115f32168ae149a86391ff6e36a15f3afc5684

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustruct-0.1.3-cp311-abi3-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on mosquito/rustruct

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rustruct-0.1.3-cp311-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for rustruct-0.1.3-cp311-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 bdb208745d252ce37f85c7a3924b8004411491432a9f7b7c416d6b1fe124991f
MD5 ab9eead4a3771f2c79f616503b7bb3cb
BLAKE2b-256 6fc5b43f6995a49b0b30ef60c9f1a4daa683bc49a9f44fe4df2f1c9ae58c8c33

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustruct-0.1.3-cp311-abi3-musllinux_1_2_aarch64.whl:

Publisher: publish.yml on mosquito/rustruct

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rustruct-0.1.3-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rustruct-0.1.3-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2c9151084c9e8e3a083a5bf118cf64733fb47ab500a6d3dec2f2ed06ad0e67c1
MD5 2bd0297fe12cda5147c828814a9686a6
BLAKE2b-256 d737686061d6544e00a57c6b780264dd7f3412920e7ef106371556f224b43d0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustruct-0.1.3-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on mosquito/rustruct

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rustruct-0.1.3-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for rustruct-0.1.3-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 098723970d90587b7649bc656b9c8440a988a4430a11ceae710fddbae7328640
MD5 decb1e324bf5d5002aa798ac36606011
BLAKE2b-256 606ffbec7c49fb3681e1d590112122970c2614f875340b9f73a34b9e51c08ca2

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustruct-0.1.3-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: publish.yml on mosquito/rustruct

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rustruct-0.1.3-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for rustruct-0.1.3-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 c2ab148b165a07e13568eb0e3d85f0a491a265efec0b9b083a4dc4f05e3475b5
MD5 b3a5a2a2e818bfc6b8e73faad7d09c0f
BLAKE2b-256 a325a83a367c65863ada7a7635826b8fab2c7f5c70b3856fd07391a42ef0e1e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustruct-0.1.3-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: publish.yml on mosquito/rustruct

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page