rombob 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rombob-1.0.0/LICENSE +207 -0
- rombob-1.0.0/PKG-INFO +224 -0
- rombob-1.0.0/README.md +206 -0
- rombob-1.0.0/pyproject.toml +53 -0
- rombob-1.0.0/src/rombob/__init__.py +153 -0
- rombob-1.0.0/src/rombob/aliases.py +42 -0
- rombob-1.0.0/src/rombob/buffers.py +109 -0
- rombob-1.0.0/src/rombob/codecs.py +585 -0
- rombob-1.0.0/src/rombob/compiler.py +926 -0
- rombob-1.0.0/src/rombob/errors.py +2 -0
- rombob-1.0.0/src/rombob/factory.py +213 -0
- rombob-1.0.0/src/rombob/metadata.py +173 -0
- rombob-1.0.0/src/rombob/parser.py +408 -0
- rombob-1.0.0/src/rombob/tracing.py +127 -0
- rombob-1.0.0/src/rombob/typing.py +18 -0
rombob-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the
|
|
13
|
+
copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other
|
|
16
|
+
entities that control, are controlled by, or are under common control with
|
|
17
|
+
that entity. For the purposes of this definition, "control" means (i) the
|
|
18
|
+
power, direct or indirect, to cause the direction or management of such
|
|
19
|
+
entity, whether by contract or otherwise, or (ii) ownership of
|
|
20
|
+
fifty percent (50%) or more of the outstanding shares, or (iii) beneficial
|
|
21
|
+
ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
24
|
+
permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation source,
|
|
28
|
+
and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical transformation
|
|
31
|
+
or translation of a Source form, including but not limited to compiled
|
|
32
|
+
object code, generated documentation, and conversions to
|
|
33
|
+
other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or Object
|
|
36
|
+
form, made available under the License, as indicated by a copyright notice
|
|
37
|
+
that is included in or attached to the work (an example is provided in the
|
|
38
|
+
Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object form,
|
|
41
|
+
that is based on (or derived from) the Work and for which the editorial
|
|
42
|
+
revisions, annotations, elaborations, or other modifications represent,
|
|
43
|
+
as a whole, an original work of authorship. For the purposes of this
|
|
44
|
+
License, Derivative Works shall not include works that remain separable
|
|
45
|
+
from, or merely link (or bind by name) to the interfaces of, the Work and
|
|
46
|
+
Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including the original
|
|
49
|
+
version of the Work and any modifications or additions to that Work or
|
|
50
|
+
Derivative Works thereof, that is intentionally submitted to Licensor for
|
|
51
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
52
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
53
|
+
For the purposes of this definition, "submitted" means any form of
|
|
54
|
+
electronic, verbal, or written communication sent to the Licensor or its
|
|
55
|
+
representatives, including but not limited to communication on electronic
|
|
56
|
+
mailing lists, source code control systems, and issue tracking systems
|
|
57
|
+
that are managed by, or on behalf of, the Licensor for the purpose of
|
|
58
|
+
discussing and improving the Work, but excluding communication that is
|
|
59
|
+
conspicuously marked or otherwise designated in writing by the copyright
|
|
60
|
+
owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on
|
|
63
|
+
behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License.
|
|
67
|
+
|
|
68
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
69
|
+
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
|
70
|
+
royalty-free, irrevocable copyright license to reproduce, prepare
|
|
71
|
+
Derivative Works of, publicly display, publicly perform, sublicense,
|
|
72
|
+
and distribute the Work and such Derivative Works in
|
|
73
|
+
Source or Object form.
|
|
74
|
+
|
|
75
|
+
3. Grant of Patent License.
|
|
76
|
+
|
|
77
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
78
|
+
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
|
79
|
+
royalty-free, irrevocable (except as stated in this section) patent
|
|
80
|
+
license to make, have made, use, offer to sell, sell, import, and
|
|
81
|
+
otherwise transfer the Work, where such license applies only to those
|
|
82
|
+
patent claims licensable by such Contributor that are necessarily
|
|
83
|
+
infringed by their Contribution(s) alone or by combination of their
|
|
84
|
+
Contribution(s) with the Work to which such Contribution(s) was submitted.
|
|
85
|
+
If You institute patent litigation against any entity (including a
|
|
86
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
|
87
|
+
Contribution incorporated within the Work constitutes direct or
|
|
88
|
+
contributory patent infringement, then any patent licenses granted to
|
|
89
|
+
You under this License for that Work shall terminate as of the date such
|
|
90
|
+
litigation is filed.
|
|
91
|
+
|
|
92
|
+
4. Redistribution.
|
|
93
|
+
|
|
94
|
+
You may reproduce and distribute copies of the Work or Derivative Works
|
|
95
|
+
thereof in any medium, with or without modifications, and in Source or
|
|
96
|
+
Object form, provided that You meet the following conditions:
|
|
97
|
+
|
|
98
|
+
1. You must give any other recipients of the Work or Derivative Works a
|
|
99
|
+
copy of this License; and
|
|
100
|
+
|
|
101
|
+
2. You must cause any modified files to carry prominent notices stating
|
|
102
|
+
that You changed the files; and
|
|
103
|
+
|
|
104
|
+
3. You must retain, in the Source form of any Derivative Works that You
|
|
105
|
+
distribute, all copyright, patent, trademark, and attribution notices from
|
|
106
|
+
the Source form of the Work, excluding those notices that do not pertain
|
|
107
|
+
to any part of the Derivative Works; and
|
|
108
|
+
|
|
109
|
+
4. If the Work includes a "NOTICE" text file as part of its distribution,
|
|
110
|
+
then any Derivative Works that You distribute must include a readable copy
|
|
111
|
+
of the attribution notices contained within such NOTICE file, excluding
|
|
112
|
+
those notices that do not pertain to any part of the Derivative Works,
|
|
113
|
+
in at least one of the following places: within a NOTICE text file
|
|
114
|
+
distributed as part of the Derivative Works; within the Source form or
|
|
115
|
+
documentation, if provided along with the Derivative Works; or, within a
|
|
116
|
+
display generated by the Derivative Works, if and wherever such
|
|
117
|
+
third-party notices normally appear. The contents of the NOTICE file are
|
|
118
|
+
for informational purposes only and do not modify the License.
|
|
119
|
+
You may add Your own attribution notices within Derivative Works that You
|
|
120
|
+
distribute, alongside or as an addendum to the NOTICE text from the Work,
|
|
121
|
+
provided that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and may
|
|
125
|
+
provide additional or different license terms and conditions for use,
|
|
126
|
+
reproduction, or distribution of Your modifications, or for any such
|
|
127
|
+
Derivative Works as a whole, provided Your use, reproduction, and
|
|
128
|
+
distribution of the Work otherwise complies with the conditions
|
|
129
|
+
stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions.
|
|
132
|
+
|
|
133
|
+
Unless You explicitly state otherwise, any Contribution intentionally
|
|
134
|
+
submitted for inclusion in the Work by You to the Licensor shall be under
|
|
135
|
+
the terms and conditions of this License, without any additional
|
|
136
|
+
terms or conditions. Notwithstanding the above, nothing herein shall
|
|
137
|
+
supersede or modify the terms of any separate license agreement you may
|
|
138
|
+
have executed with Licensor regarding such Contributions.
|
|
139
|
+
|
|
140
|
+
6. Trademarks.
|
|
141
|
+
|
|
142
|
+
This License does not grant permission to use the trade names, trademarks,
|
|
143
|
+
service marks, or product names of the Licensor, except as required for
|
|
144
|
+
reasonable and customary use in describing the origin of the Work and
|
|
145
|
+
reproducing the content of the NOTICE file.
|
|
146
|
+
|
|
147
|
+
7. Disclaimer of Warranty.
|
|
148
|
+
|
|
149
|
+
Unless required by applicable law or agreed to in writing, Licensor
|
|
150
|
+
provides the Work (and each Contributor provides its Contributions)
|
|
151
|
+
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
152
|
+
either express or implied, including, without limitation, any warranties
|
|
153
|
+
or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS
|
|
154
|
+
FOR A PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
155
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
156
|
+
associated with Your exercise of permissions under this License.
|
|
157
|
+
|
|
158
|
+
8. Limitation of Liability.
|
|
159
|
+
|
|
160
|
+
In no event and under no legal theory, whether in tort
|
|
161
|
+
(including negligence), contract, or otherwise, unless required by
|
|
162
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed
|
|
163
|
+
to in writing, shall any Contributor be liable to You for damages,
|
|
164
|
+
including any direct, indirect, special, incidental, or consequential
|
|
165
|
+
damages of any character arising as a result of this License or out of
|
|
166
|
+
the use or inability to use the Work (including but not limited to damages
|
|
167
|
+
for loss of goodwill, work stoppage, computer failure or malfunction,
|
|
168
|
+
or any and all other commercial damages or losses), even if such
|
|
169
|
+
Contributor has been advised of the possibility of such damages.
|
|
170
|
+
|
|
171
|
+
9. Accepting Warranty or Additional Liability.
|
|
172
|
+
|
|
173
|
+
While redistributing the Work or Derivative Works thereof, You may choose
|
|
174
|
+
to offer, and charge a fee for, acceptance of support, warranty,
|
|
175
|
+
indemnity, or other liability obligations and/or rights consistent with
|
|
176
|
+
this License. However, in accepting such obligations, You may act only
|
|
177
|
+
on Your own behalf and on Your sole responsibility, not on behalf of any
|
|
178
|
+
other Contributor, and only if You agree to indemnify, defend, and hold
|
|
179
|
+
each Contributor harmless for any liability incurred by, or claims
|
|
180
|
+
asserted against, such Contributor by reason of your accepting any such
|
|
181
|
+
warranty or additional liability.
|
|
182
|
+
|
|
183
|
+
END OF TERMS AND CONDITIONS
|
|
184
|
+
|
|
185
|
+
APPENDIX: How to apply the Apache License to your work
|
|
186
|
+
|
|
187
|
+
To apply the Apache License to your work, attach the following boilerplate
|
|
188
|
+
notice, with the fields enclosed by brackets "[]" replaced with your own
|
|
189
|
+
identifying information. (Don't include the brackets!) The text should be
|
|
190
|
+
enclosed in the appropriate comment syntax for the file format. We also
|
|
191
|
+
recommend that a file or class name and description of purpose be included
|
|
192
|
+
on the same "printed page" as the copyright notice for easier
|
|
193
|
+
identification within third-party archives.
|
|
194
|
+
|
|
195
|
+
Copyright 2026 loudblow
|
|
196
|
+
|
|
197
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
198
|
+
you may not use this file except in compliance with the License.
|
|
199
|
+
You may obtain a copy of the License at
|
|
200
|
+
|
|
201
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
202
|
+
|
|
203
|
+
Unless required by applicable law or agreed to in writing, software
|
|
204
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
205
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
206
|
+
or implied. See the License for the specific language governing
|
|
207
|
+
permissions and limitations under the License.
|
rombob-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rombob
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Fast binary serialization, driven by plain type hints.
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Author: loudblow
|
|
8
|
+
Author-email: 145815114+loudblow@users.noreply.github.com
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
<div align="center">
|
|
19
|
+
|
|
20
|
+
# 🧩 Rombob
|
|
21
|
+
|
|
22
|
+
**Binary serialization for Python, driven by plain type hints.**
|
|
23
|
+
|
|
24
|
+
Describe your wire format as a [dataclass] or a [NamedTuple] — get a fast, validated encoder and decoder for free. Zero dependencies, pure python.
|
|
25
|
+
|
|
26
|
+
[Installation](#-installation) ·
|
|
27
|
+
[Quick Start](#-quick-start) ·
|
|
28
|
+
[Advanced Example](#-advanced-example) ·
|
|
29
|
+
[Annotations](#-annotations-cheat-sheet) ·
|
|
30
|
+
[Types](#-supported-types) ·
|
|
31
|
+
[Contributing](#-contributing)
|
|
32
|
+
|
|
33
|
+
</div>
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## ✨ Why Rombob?
|
|
38
|
+
|
|
39
|
+
- 🏷️ **Just type hints** — no schema files, no code generation step, no base classes.
|
|
40
|
+
- ⚡ **Fast** — consecutive fixed-size fields are compiled into a single `struct` call.
|
|
41
|
+
- 🔗 **Dependent fields** — lengths and optional fields can depend on values read earlier.
|
|
42
|
+
- 🛡️ **Validated** — constraints (`Range`, `Gt`, `Predicate`, ...) are checked on read *and* write.
|
|
43
|
+
- 🔍 **Readable errors** — `trace_errors=True` points at the exact failing field, e.g. `Packet.points[0].x`.
|
|
44
|
+
- 🧠 **Stateless codecs** — build once, share freely.
|
|
45
|
+
|
|
46
|
+
## 📦 Installation
|
|
47
|
+
|
|
48
|
+
Install the latest unreleased code straight from Git:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install git+https://github.com/loudblow/rombob.git@main
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 🚀 Quick Start
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import dataclasses
|
|
58
|
+
import rombob
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dataclasses.dataclass
|
|
62
|
+
class Player:
|
|
63
|
+
id: rombob.U8_B # 1 byte, big-endian
|
|
64
|
+
username: str # UTF-8 with a 2-byte length prefix
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# 📤 Serialize
|
|
68
|
+
encoded = rombob.encode(Player(id=1, username="Archer"))
|
|
69
|
+
print(encoded) # b'\x01\x00\x06Archer'
|
|
70
|
+
|
|
71
|
+
# 📥 Deserialize
|
|
72
|
+
player = rombob.decode(encoded, cls=Player)
|
|
73
|
+
print(player) # Player(id=1, username='Archer')
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## 🔬 Advanced Example
|
|
77
|
+
|
|
78
|
+
A tiny network packet that uses most of the features at once: magic bytes, an enum,
|
|
79
|
+
a length taken from an earlier field, a conditional field, validation, nested
|
|
80
|
+
structures, and a raw tail.
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import dataclasses
|
|
84
|
+
import enum
|
|
85
|
+
from typing import Annotated, Literal
|
|
86
|
+
|
|
87
|
+
import rombob
|
|
88
|
+
from rombob import (
|
|
89
|
+
I16_L, U8_B, Len, Named, Prefix, Range, Raw, When,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class Kind(enum.IntEnum):
|
|
94
|
+
PING = 1
|
|
95
|
+
CHAT = 2
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@dataclasses.dataclass
|
|
99
|
+
class Point:
|
|
100
|
+
x: I16_L # little-endian int16
|
|
101
|
+
y: I16_L
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclasses.dataclass
|
|
105
|
+
class Packet:
|
|
106
|
+
magic: Literal[b"RB"] # constant, checked on read
|
|
107
|
+
version: Annotated[Literal[1, 2], U8_B] # only 1 or 2 is accepted
|
|
108
|
+
kind: Annotated[Kind, U8_B] # enum stored as one byte
|
|
109
|
+
count: Named[U8_B] # remembered as "count"
|
|
110
|
+
points: Annotated[list[Point], Len(ref="count")] # length = count
|
|
111
|
+
has_token: Named[bool]
|
|
112
|
+
token: Annotated[str | None, When(ref="has_token")] # only if has_token
|
|
113
|
+
hp: Annotated[U8_B, Range(0, 100)] # validated 0..100
|
|
114
|
+
tags: Annotated[list[Annotated[str, Prefix(U8_B)]], Prefix(U8_B)]
|
|
115
|
+
payload: Raw # everything that is left
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
packet = Packet(
|
|
119
|
+
magic=b"RB",
|
|
120
|
+
version=1,
|
|
121
|
+
kind=Kind.CHAT,
|
|
122
|
+
count=2,
|
|
123
|
+
points=[Point(1, 2), Point(-3, 4)],
|
|
124
|
+
has_token=True,
|
|
125
|
+
token="secret",
|
|
126
|
+
hp=80,
|
|
127
|
+
tags=["a", "bc"],
|
|
128
|
+
payload=b"\xde\xad",
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
data = rombob.encode(packet)
|
|
132
|
+
assert rombob.decode(data, cls=Packet) == packet
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Something went wrong? Ask for a trace and get the exact location:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
import dataclasses
|
|
139
|
+
|
|
140
|
+
rombob.encode(dataclasses.replace(packet, hp=200), trace_errors=True)
|
|
141
|
+
# BytesOperationError: Encoding failed at length 22 (Packet.hp):
|
|
142
|
+
# 200 violates Range(lo=0, hi=100)
|
|
143
|
+
|
|
144
|
+
rombob.decode(data[:5], cls=Packet, trace_errors=True)
|
|
145
|
+
# BytesOperationError: Decoding failed at offset 5 (Packet.points[0].x): ...
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## 🧰 Annotations Cheat Sheet
|
|
149
|
+
|
|
150
|
+
Everything lives in `rombob.metadata` and is importable from `rombob`.
|
|
151
|
+
|
|
152
|
+
| Annotation | What it does | Example |
|
|
153
|
+
| ---------- | ------------ | ------- |
|
|
154
|
+
| `LE` / `BE` | Byte order for `IntCodec` (or use the `*_L` / `*_B` aliases) | `U16_L` |
|
|
155
|
+
| `Len(n)` | Exact length, no length prefix | `Annotated[str, Len(10)]` |
|
|
156
|
+
| `Len(ref=...)` | Length from an earlier `Named` field (or `lambda ctx: ...`); checked on write | `Len(ref="count")` |
|
|
157
|
+
| `Prefix(codec)` | Integer type of the length prefix (U16 big-endian by default) | `Prefix(U8_B)` |
|
|
158
|
+
| `Named[T]` | Remember the value so later fields can refer to it | `Named[U8_B]` |
|
|
159
|
+
| `When(...)` | Make a field conditional on earlier fields | `When(ref="has_token")` |
|
|
160
|
+
| `Optional[T]` / `T \| None` | Present while there is data left | `int \| None` |
|
|
161
|
+
| `Skip[T]` | Neither read nor written (needs a default) | `Skip[int]` |
|
|
162
|
+
| `Gt` `Ge` `Lt` `Le` `Range` | Numeric constraints, checked on read and write | `Range(1, 10)` |
|
|
163
|
+
| `Predicate(func)` | Any custom check | `Predicate(lambda v: v % 2 == 0)` |
|
|
164
|
+
| `Literal[...]` | Constants; `bytes`/`str` literals need no codec, `int` literals do | `Annotated[Literal[1, 2], U8_B]` |
|
|
165
|
+
|
|
166
|
+
## 🧱 Supported Types
|
|
167
|
+
|
|
168
|
+
Fields can be bools, integers, floats, strings, bytes, lists, tuples, sets,
|
|
169
|
+
frozensets, enums, named tuples, or other dataclasses — plus `Optional`, `Literal`,
|
|
170
|
+
`Generic`/`TypeVar`, fixed- and variable-length collections, and heterogeneous
|
|
171
|
+
tuples such as `tuple[int, str]`.
|
|
172
|
+
|
|
173
|
+
### Plain Python types (defaults)
|
|
174
|
+
|
|
175
|
+
| Python type | Encoding | Notes |
|
|
176
|
+
| ----------- | -------- | ----- |
|
|
177
|
+
| `bool` | `?` | Writes 1 or 0 |
|
|
178
|
+
| `str` | `U16(BE)` length + UTF-8 bytes | |
|
|
179
|
+
| `bytes` | `U16(BE)` length + bytes | |
|
|
180
|
+
| `int` | `I64(BE)` | Signed 64-bit |
|
|
181
|
+
| `float` | `F64(BE)` | IEEE 754 double |
|
|
182
|
+
|
|
183
|
+
### Aliases
|
|
184
|
+
|
|
185
|
+
Every alias comes in two flavors: `_B` (big-endian) and `_L` (little-endian).
|
|
186
|
+
|
|
187
|
+
| Family | Aliases |
|
|
188
|
+
| ------ | ------- |
|
|
189
|
+
| Unsigned ints | `U8` `U16` `U24` `U32` `U48` `U64` |
|
|
190
|
+
| Signed ints | `I8` `I16` `I24` `I32` `I48` `I64` |
|
|
191
|
+
| Floats | `F16` `F32` `F64` |
|
|
192
|
+
| Variable-length | `UVInt` (unsigned), `IVInt` (signed) |
|
|
193
|
+
| Rest of the buffer | `Raw` |
|
|
194
|
+
|
|
195
|
+
## ⚙️ Going Further
|
|
196
|
+
|
|
197
|
+
- **Fast paths** — `Codec.encode/decode` with an explicit codec, or `Factory` for
|
|
198
|
+
dispatch by primary field. See `rombob.get_codec(cls)` to build and inspect a codec.
|
|
199
|
+
- **Streaming** — pass `reader=` / `writer=` to `decode` / `encode` to continue an
|
|
200
|
+
existing buffer.
|
|
201
|
+
- **Benchmarks** — see [`benchmarks/`](benchmarks) (`bench.py`, `bench_int24.py`).
|
|
202
|
+
|
|
203
|
+
## 🤝 Contributing
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
make install # poetry install + pre-commit hooks
|
|
207
|
+
make test # pytest + coverage
|
|
208
|
+
make format # ruff format
|
|
209
|
+
make lint # pre-commit run
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Pull requests and issues are welcome!
|
|
213
|
+
|
|
214
|
+
## 📚 References & Inspiration
|
|
215
|
+
|
|
216
|
+
- [bytechomp](https://github.com/AndrewSpittlemeister/bytechomp/tree/main)
|
|
217
|
+
- [bstruct](https://github.com/flxbe/bstruct)
|
|
218
|
+
- [pyserde](https://github.com/yukinarit/pyserde/tree/main)
|
|
219
|
+
- [pdc_struct](https://github.com/boxcake/pdc_struct)
|
|
220
|
+
- [dataclasses-struct](https://github.com/harrymander/dataclasses-struct)
|
|
221
|
+
|
|
222
|
+
[dataclass]: https://docs.python.org/3/library/dataclasses.html
|
|
223
|
+
[NamedTuple]: https://docs.python.org/3/library/typing.html#typing.NamedTuple
|
|
224
|
+
|
rombob-1.0.0/README.md
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# 🧩 Rombob
|
|
4
|
+
|
|
5
|
+
**Binary serialization for Python, driven by plain type hints.**
|
|
6
|
+
|
|
7
|
+
Describe your wire format as a [dataclass] or a [NamedTuple] — get a fast, validated encoder and decoder for free. Zero dependencies, pure python.
|
|
8
|
+
|
|
9
|
+
[Installation](#-installation) ·
|
|
10
|
+
[Quick Start](#-quick-start) ·
|
|
11
|
+
[Advanced Example](#-advanced-example) ·
|
|
12
|
+
[Annotations](#-annotations-cheat-sheet) ·
|
|
13
|
+
[Types](#-supported-types) ·
|
|
14
|
+
[Contributing](#-contributing)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## ✨ Why Rombob?
|
|
21
|
+
|
|
22
|
+
- 🏷️ **Just type hints** — no schema files, no code generation step, no base classes.
|
|
23
|
+
- ⚡ **Fast** — consecutive fixed-size fields are compiled into a single `struct` call.
|
|
24
|
+
- 🔗 **Dependent fields** — lengths and optional fields can depend on values read earlier.
|
|
25
|
+
- 🛡️ **Validated** — constraints (`Range`, `Gt`, `Predicate`, ...) are checked on read *and* write.
|
|
26
|
+
- 🔍 **Readable errors** — `trace_errors=True` points at the exact failing field, e.g. `Packet.points[0].x`.
|
|
27
|
+
- 🧠 **Stateless codecs** — build once, share freely.
|
|
28
|
+
|
|
29
|
+
## 📦 Installation
|
|
30
|
+
|
|
31
|
+
Install the latest unreleased code straight from Git:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install git+https://github.com/loudblow/rombob.git@main
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 🚀 Quick Start
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import dataclasses
|
|
41
|
+
import rombob
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclasses.dataclass
|
|
45
|
+
class Player:
|
|
46
|
+
id: rombob.U8_B # 1 byte, big-endian
|
|
47
|
+
username: str # UTF-8 with a 2-byte length prefix
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# 📤 Serialize
|
|
51
|
+
encoded = rombob.encode(Player(id=1, username="Archer"))
|
|
52
|
+
print(encoded) # b'\x01\x00\x06Archer'
|
|
53
|
+
|
|
54
|
+
# 📥 Deserialize
|
|
55
|
+
player = rombob.decode(encoded, cls=Player)
|
|
56
|
+
print(player) # Player(id=1, username='Archer')
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## 🔬 Advanced Example
|
|
60
|
+
|
|
61
|
+
A tiny network packet that uses most of the features at once: magic bytes, an enum,
|
|
62
|
+
a length taken from an earlier field, a conditional field, validation, nested
|
|
63
|
+
structures, and a raw tail.
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import dataclasses
|
|
67
|
+
import enum
|
|
68
|
+
from typing import Annotated, Literal
|
|
69
|
+
|
|
70
|
+
import rombob
|
|
71
|
+
from rombob import (
|
|
72
|
+
I16_L, U8_B, Len, Named, Prefix, Range, Raw, When,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class Kind(enum.IntEnum):
|
|
77
|
+
PING = 1
|
|
78
|
+
CHAT = 2
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@dataclasses.dataclass
|
|
82
|
+
class Point:
|
|
83
|
+
x: I16_L # little-endian int16
|
|
84
|
+
y: I16_L
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclasses.dataclass
|
|
88
|
+
class Packet:
|
|
89
|
+
magic: Literal[b"RB"] # constant, checked on read
|
|
90
|
+
version: Annotated[Literal[1, 2], U8_B] # only 1 or 2 is accepted
|
|
91
|
+
kind: Annotated[Kind, U8_B] # enum stored as one byte
|
|
92
|
+
count: Named[U8_B] # remembered as "count"
|
|
93
|
+
points: Annotated[list[Point], Len(ref="count")] # length = count
|
|
94
|
+
has_token: Named[bool]
|
|
95
|
+
token: Annotated[str | None, When(ref="has_token")] # only if has_token
|
|
96
|
+
hp: Annotated[U8_B, Range(0, 100)] # validated 0..100
|
|
97
|
+
tags: Annotated[list[Annotated[str, Prefix(U8_B)]], Prefix(U8_B)]
|
|
98
|
+
payload: Raw # everything that is left
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
packet = Packet(
|
|
102
|
+
magic=b"RB",
|
|
103
|
+
version=1,
|
|
104
|
+
kind=Kind.CHAT,
|
|
105
|
+
count=2,
|
|
106
|
+
points=[Point(1, 2), Point(-3, 4)],
|
|
107
|
+
has_token=True,
|
|
108
|
+
token="secret",
|
|
109
|
+
hp=80,
|
|
110
|
+
tags=["a", "bc"],
|
|
111
|
+
payload=b"\xde\xad",
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
data = rombob.encode(packet)
|
|
115
|
+
assert rombob.decode(data, cls=Packet) == packet
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Something went wrong? Ask for a trace and get the exact location:
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
import dataclasses
|
|
122
|
+
|
|
123
|
+
rombob.encode(dataclasses.replace(packet, hp=200), trace_errors=True)
|
|
124
|
+
# BytesOperationError: Encoding failed at length 22 (Packet.hp):
|
|
125
|
+
# 200 violates Range(lo=0, hi=100)
|
|
126
|
+
|
|
127
|
+
rombob.decode(data[:5], cls=Packet, trace_errors=True)
|
|
128
|
+
# BytesOperationError: Decoding failed at offset 5 (Packet.points[0].x): ...
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## 🧰 Annotations Cheat Sheet
|
|
132
|
+
|
|
133
|
+
Everything lives in `rombob.metadata` and is importable from `rombob`.
|
|
134
|
+
|
|
135
|
+
| Annotation | What it does | Example |
|
|
136
|
+
| ---------- | ------------ | ------- |
|
|
137
|
+
| `LE` / `BE` | Byte order for `IntCodec` (or use the `*_L` / `*_B` aliases) | `U16_L` |
|
|
138
|
+
| `Len(n)` | Exact length, no length prefix | `Annotated[str, Len(10)]` |
|
|
139
|
+
| `Len(ref=...)` | Length from an earlier `Named` field (or `lambda ctx: ...`); checked on write | `Len(ref="count")` |
|
|
140
|
+
| `Prefix(codec)` | Integer type of the length prefix (U16 big-endian by default) | `Prefix(U8_B)` |
|
|
141
|
+
| `Named[T]` | Remember the value so later fields can refer to it | `Named[U8_B]` |
|
|
142
|
+
| `When(...)` | Make a field conditional on earlier fields | `When(ref="has_token")` |
|
|
143
|
+
| `Optional[T]` / `T \| None` | Present while there is data left | `int \| None` |
|
|
144
|
+
| `Skip[T]` | Neither read nor written (needs a default) | `Skip[int]` |
|
|
145
|
+
| `Gt` `Ge` `Lt` `Le` `Range` | Numeric constraints, checked on read and write | `Range(1, 10)` |
|
|
146
|
+
| `Predicate(func)` | Any custom check | `Predicate(lambda v: v % 2 == 0)` |
|
|
147
|
+
| `Literal[...]` | Constants; `bytes`/`str` literals need no codec, `int` literals do | `Annotated[Literal[1, 2], U8_B]` |
|
|
148
|
+
|
|
149
|
+
## 🧱 Supported Types
|
|
150
|
+
|
|
151
|
+
Fields can be bools, integers, floats, strings, bytes, lists, tuples, sets,
|
|
152
|
+
frozensets, enums, named tuples, or other dataclasses — plus `Optional`, `Literal`,
|
|
153
|
+
`Generic`/`TypeVar`, fixed- and variable-length collections, and heterogeneous
|
|
154
|
+
tuples such as `tuple[int, str]`.
|
|
155
|
+
|
|
156
|
+
### Plain Python types (defaults)
|
|
157
|
+
|
|
158
|
+
| Python type | Encoding | Notes |
|
|
159
|
+
| ----------- | -------- | ----- |
|
|
160
|
+
| `bool` | `?` | Writes 1 or 0 |
|
|
161
|
+
| `str` | `U16(BE)` length + UTF-8 bytes | |
|
|
162
|
+
| `bytes` | `U16(BE)` length + bytes | |
|
|
163
|
+
| `int` | `I64(BE)` | Signed 64-bit |
|
|
164
|
+
| `float` | `F64(BE)` | IEEE 754 double |
|
|
165
|
+
|
|
166
|
+
### Aliases
|
|
167
|
+
|
|
168
|
+
Every alias comes in two flavors: `_B` (big-endian) and `_L` (little-endian).
|
|
169
|
+
|
|
170
|
+
| Family | Aliases |
|
|
171
|
+
| ------ | ------- |
|
|
172
|
+
| Unsigned ints | `U8` `U16` `U24` `U32` `U48` `U64` |
|
|
173
|
+
| Signed ints | `I8` `I16` `I24` `I32` `I48` `I64` |
|
|
174
|
+
| Floats | `F16` `F32` `F64` |
|
|
175
|
+
| Variable-length | `UVInt` (unsigned), `IVInt` (signed) |
|
|
176
|
+
| Rest of the buffer | `Raw` |
|
|
177
|
+
|
|
178
|
+
## ⚙️ Going Further
|
|
179
|
+
|
|
180
|
+
- **Fast paths** — `Codec.encode/decode` with an explicit codec, or `Factory` for
|
|
181
|
+
dispatch by primary field. See `rombob.get_codec(cls)` to build and inspect a codec.
|
|
182
|
+
- **Streaming** — pass `reader=` / `writer=` to `decode` / `encode` to continue an
|
|
183
|
+
existing buffer.
|
|
184
|
+
- **Benchmarks** — see [`benchmarks/`](benchmarks) (`bench.py`, `bench_int24.py`).
|
|
185
|
+
|
|
186
|
+
## 🤝 Contributing
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
make install # poetry install + pre-commit hooks
|
|
190
|
+
make test # pytest + coverage
|
|
191
|
+
make format # ruff format
|
|
192
|
+
make lint # pre-commit run
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Pull requests and issues are welcome!
|
|
196
|
+
|
|
197
|
+
## 📚 References & Inspiration
|
|
198
|
+
|
|
199
|
+
- [bytechomp](https://github.com/AndrewSpittlemeister/bytechomp/tree/main)
|
|
200
|
+
- [bstruct](https://github.com/flxbe/bstruct)
|
|
201
|
+
- [pyserde](https://github.com/yukinarit/pyserde/tree/main)
|
|
202
|
+
- [pdc_struct](https://github.com/boxcake/pdc_struct)
|
|
203
|
+
- [dataclasses-struct](https://github.com/harrymander/dataclasses-struct)
|
|
204
|
+
|
|
205
|
+
[dataclass]: https://docs.python.org/3/library/dataclasses.html
|
|
206
|
+
[NamedTuple]: https://docs.python.org/3/library/typing.html#typing.NamedTuple
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "rombob"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Fast binary serialization, driven by plain type hints."
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "loudblow", email = "145815114+loudblow@users.noreply.github.com"}
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
license = "Apache-2.0"
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
dependencies = []
|
|
13
|
+
|
|
14
|
+
[dependency-groups]
|
|
15
|
+
dev = [
|
|
16
|
+
"pytest (>=9.0.2,<10.0.0)",
|
|
17
|
+
"pytest-coverage (>=0.0,<0.1)",
|
|
18
|
+
"ruff (>=0.15.7,<0.16.0)",
|
|
19
|
+
"pre-commit (>=4.5.1,<5.0.0)"
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[tool.poetry]
|
|
23
|
+
packages = [{include = "*", from="src"}]
|
|
24
|
+
package-mode = true
|
|
25
|
+
|
|
26
|
+
[tool.pytest.ini_options]
|
|
27
|
+
pythonpath = ["src"]
|
|
28
|
+
testpaths = ["tests"]
|
|
29
|
+
|
|
30
|
+
[tool.ruff]
|
|
31
|
+
src = ["src"]
|
|
32
|
+
fix = true
|
|
33
|
+
show-fixes = true
|
|
34
|
+
output-format = "full"
|
|
35
|
+
line-length = 80
|
|
36
|
+
|
|
37
|
+
[tool.ruff.lint]
|
|
38
|
+
select = [
|
|
39
|
+
"B", # flake8-bugbear
|
|
40
|
+
"E", # pycodestyle error
|
|
41
|
+
"F", # pyflakes
|
|
42
|
+
"I", # isort
|
|
43
|
+
"UP", # pyupgrade
|
|
44
|
+
"W", # pycodestyle warning
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[tool.ruff.lint.isort]
|
|
48
|
+
force-single-line = false
|
|
49
|
+
order-by-type = true
|
|
50
|
+
|
|
51
|
+
[build-system]
|
|
52
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
53
|
+
build-backend = "poetry.core.masonry.api"
|