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

# Binal

Declarative, type-safe binary modeling for Python.

Binal is a pure-Python library for defining, encoding, decoding, validating, and composing binary data structures using native Python classes and type annotations.

It provides a unified binary modeling system that eliminates repetitive serialization logic, manual buffer management, offset calculations, bitwise operations, and structural validation.

The data model serves as the single source of truth for both Python objects and their binary representations.

## Installation

Requires Python 3.11+.

```bash
uv add binal
```

```bash
pip install binal
```

## Quick Start

```python
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'
packet = Packet.decode(data)
# Packet(version=1, payload=b'Hello')
```

## Features

- Declarative models with native type annotations
- Integers (including custom widths), floats, booleans, bytes, strings, and bit fields
- Nested models, collections, enums, and tagged unions
- Length-prefixed, fixed-length, null-terminated, and length-dependent fields
- Derived lengths, counts, and checksums (CRC32, CRC16, Adler32)
- Conditional and dependent fields
- Variable-length integers: ULEB128, SLEB128, ZigZag
- Schema introspection with field offsets and sizes
- File, buffer, stream, and asynchronous I/O
- Structured errors with field paths and byte offsets
- Zero runtime dependencies, pure Python

See the [documentation](docs/) and `examples/` for more.

## Development

```bash
uv sync --locked
uv run ruff format
uv run ruff check
uv run pyright
uv run pytest
uv run pytest --cov=binal
uv build
```

## License

MIT. See [LICENSE](LICENSE).
