Metadata-Version: 2.4
Name: binal
Version: 0.3.0
Summary: Declarative, type-safe binary modeling for Python.
Author: Oceanus
Author-email: Oceanus <295322314+oceanus403@users.noreply.github.com>
License-Expression: MIT
Requires-Python: >=3.12
Project-URL: Repository, https://github.com/oceanus403/binal
Description-Content-Type: text/markdown

<div align="center">

<h1>binal</h1>

<p><strong>Declarative, type-safe binary modeling for Python</strong></p>

<p>
Define a binary format once, as a Python class, and get encoding,<br>
decoding, validation, and layout from the same declaration.
</p>

<p>
<a href="https://pypi.org/project/binal/"><img alt="PyPI version" src="https://img.shields.io/pypi/v/binal"></a>
<a href="pyproject.toml"><img alt="Python 3.12+" src="https://img.shields.io/badge/python-3.12%2B-blue"></a>
<a href="LICENSE"><img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-blue"></a>
</p>

<p>
<a href="https://oceanus403.github.io/binal/"><b>Documentation</b></a>
&nbsp;&middot;&nbsp;
<a href="#capabilities"><b>Capabilities</b></a>
&nbsp;&middot;&nbsp;
<a href="#installation"><b>Installation</b></a>
&nbsp;&middot;&nbsp;
<a href="#usage"><b>Usage</b></a>
&nbsp;&middot;&nbsp;
<a href="https://oceanus403.github.io/binal/docs/contributing"><b>Contributing</b></a>
</p>

</div>

<br>

## Capabilities

| Area | What it covers |
|:--|:--|
| [Binary types](docs/content/docs/binary-types/integers.mdx) | Integers (including custom widths), floats, booleans, bytes, strings, and bit fields; ULEB128, SLEB128, ZigZag32, ZigZag64 |
| [Modeling](docs/content/docs/modeling/collections.mdx) | Nested models, collections, enums, tagged unions, conditional and dependent fields, derived lengths, counts, and checksums (CRC32, CRC16, Adler32) |
| [I/O](docs/content/docs/io.mdx) | File, buffer, stream, and asynchronous I/O; incremental parsing and partial decoding |
| [Validation](docs/content/docs/validation.mdx) | Field constraints, cross-field validation, and structured errors with field paths and byte offsets |
| [Introspection](docs/content/docs/advanced/schema.mdx) | Static schema and runtime layout with field offsets and sizes |
| [Low-level I/O](docs/content/docs/api/io.mdx) | `BinaryReader` and `BinaryWriter` for ad-hoc formats and custom codecs |

## Installation

binal supports Python 3.12 and newer, with zero runtime dependencies.

Add it to a uv project:

```sh
uv add binal
```

To install it in an active Python environment:

```sh
pip install binal
```

To build from source:

```sh
git clone https://github.com/oceanus403/binal
cd binal
uv sync --locked
```

## Usage

```python
from io import BytesIO
from typing import Annotated

from binal import BinaryModel, Prefixed, UInt8, UInt16


class Packet(BinaryModel, byte_order="big"):
    version: UInt8
    payload: Annotated[bytes, Prefixed(UInt16)]


packet = Packet(version=1, payload=b"Hello")
data = packet.encode()  # b"\x01\x00\x05Hello"
assert Packet.decode(data) == packet

# Several records in one stream, read one at a time.
records = list(Packet.iter_read(BytesIO(data + data)))

# One record at an offset, with the bytes it consumed.
record, consumed = Packet.decode_partial(data + b"trailing")

# Where each field landed in the encoding.
print(packet.layout().total_bytes)  # 8
```

## Project status

binal 0.2.0 is released. The API may change before 1.0.

## License

MIT. See [LICENSE](LICENSE).
