sltcodec 1.2.1__tar.gz → 2.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.
@@ -0,0 +1,314 @@
1
+ Metadata-Version: 2.5
2
+ Name: sltcodec
3
+ Version: 2.0.0
4
+ Summary: Decode and encode bytearrays according to struct layout definitions using sltcore.
5
+ Project-URL: Homepage, https://github.com/fangface-hub/StructLayoutToolkitCodec
6
+ Project-URL: Documentation, https://readthedocs.org
7
+ Project-URL: Repository, https://github.com/fangface-hub/StructLayoutToolkitCodec
8
+ Project-URL: Issues, https://github.com/fangface-hub/StructLayoutToolkitCodec/issues
9
+ Author: fangface
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: byte,deserialization,layout,serialization,struct
13
+ Requires-Python: >=3.12
14
+ Requires-Dist: sltcalc>=0.1.0
15
+ Requires-Dist: sltcore>=1.5.0
16
+ Description-Content-Type: text/markdown
17
+
18
+ # StructLayoutToolkitCodec
19
+
20
+ `sltcodec` encodes and decodes bytearrays according to structured layout definitions. It uses `sltcore` for bit-level access.
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ pip install sltcodec
26
+ ```
27
+
28
+ ## Quick Example
29
+
30
+ `StructLayout` bundles the root structure name with its `TypeDict`. Pass the same layout to both `encode` and `decode`.
31
+
32
+ ```python
33
+ from sltcore import InfoSize
34
+ from sltcodec import (
35
+ FieldDef,
36
+ FieldInstance,
37
+ StructDef,
38
+ StructInstance,
39
+ StructLayout,
40
+ TypeDict,
41
+ decode,
42
+ encode,
43
+ )
44
+
45
+ fields = [
46
+ FieldDef(name="flag", offset=InfoSize(0, 0), size=InfoSize(0, 1), type="bool"),
47
+ FieldDef(name="value", offset=InfoSize(0, 1), size=InfoSize(1, 0), type="unsigned int"),
48
+ ]
49
+ struct_def = StructDef(name="Packet", fields=fields)
50
+ layout = StructLayout(
51
+ struct_def_name="Packet",
52
+ type_dict=TypeDict(struct_dict={"Packet": struct_def}),
53
+ )
54
+ instance = StructInstance(
55
+ struct_def=struct_def,
56
+ field_instances=[
57
+ FieldInstance(fields[0], True),
58
+ FieldInstance(fields[1], 0xA5),
59
+ ],
60
+ )
61
+
62
+ encoded = encode(layout, instance, bytearray())
63
+ decoded = decode(layout, encoded)
64
+ ```
65
+
66
+ ## Field Types
67
+
68
+ The public `PRIMITIVE_TYPES` set contains:
69
+
70
+ ```python
71
+ {"bool", "signed int", "int", "unsigned int", "float", "bytearray", "bytes"}
72
+ ```
73
+
74
+ `FieldDef.type` may also be a nested `StructDef` or an expression string. Expressions can use values from previously processed fields, allowing dynamic types and sizes. Repeated fields, byte swapping, padding, ranges, and enum metadata are supported.
75
+
76
+ ## Enums And Named Structures
77
+
78
+ Store named structures in `TypeDict.struct_dict` and enums in `TypeDict.enum_dict`. A field refers to an enum by its `enum_def_name`; the codec resolves it through `StructLayout.type_dict`.
79
+
80
+ ```python
81
+ | `__lt__(other)` | Compare enum definitions using their serialized ordering. |
82
+ from sltcore import InfoSize
83
+ from sltcodec import EnumDef, FieldDef, StructDef, StructLayout, TypeDict, decode
84
+
85
+ enum_def = EnumDef(name="Status", values={"OK": 0, "NG": 1})
86
+ packet = StructDef(
87
+ name="Packet",
88
+ fields=[FieldDef(
89
+ name="status",
90
+ offset=InfoSize(0, 0),
91
+ size=InfoSize(1, 0),
92
+ type="unsigned int",
93
+ enum_def_name="Status",
94
+ )],
95
+ )
96
+ layout = StructLayout(
97
+ struct_def_name="Packet",
98
+ type_dict=TypeDict(
99
+ struct_dict={"Packet": packet},
100
+ enum_dict={"Status": enum_def},
101
+ ),
102
+ )
103
+
104
+ decoded = decode(layout, bytearray(b"\x01"))
105
+ | `__lt__(other)` | Compare field definitions using their serialized ordering. |
106
+ assert decoded.field_instances[0].enum_item == ("NG", 1)
107
+ ```
108
+
109
+ ## Saving And Loading
110
+
111
+ Persist a complete `StructLayout`, including its root structure name, structure definitions, and enum definitions, with `save_struct_layout` and `load_struct_layout`.
112
+
113
+ ```python
114
+ from pathlib import Path
115
+ from sltcodec import load_struct_layout, save_struct_layout
116
+
117
+ path = Path("struct_layout.json")
118
+ save_struct_layout(layout, path)
119
+ loaded = load_struct_layout(path)
120
+ | `__lt__(other)` | Compare field instances using field-definition order. |
121
+ assert loaded == layout
122
+ ```
123
+
124
+ `InfoSize` values are stored as typed JSON dictionaries. Expression-based offsets and sizes remain strings and are evaluated when the layout is used.
125
+
126
+ ## Public API Reference
127
+
128
+ The following symbols are exported by `sltcodec.__all__` in this order:
129
+
130
+ | Symbol | Description |
131
+ | --- | --- |
132
+ | `PRIMITIVE_TYPES` | Set of built-in field type names supported by the codec. |
133
+ | `EnumDef` | Immutable definition of an enumeration. |
134
+ | `EnumDict` | Dictionary-like container for `EnumDef` objects. |
135
+ | `FieldDef` | Immutable definition of one structured field. |
136
+ | `FieldInstance` | Immutable field definition and decoded/encodable value pair. |
137
+ | `StructDef` | Immutable definition that groups fields into a structure. |
138
+ | `StructDict` | Dictionary-like container for `StructDef` objects. |
139
+ | `StructInstance` | Decoded/encodable structure instance containing field instances. |
140
+ | `StructLayout` | Bundle of the root structure name and its `TypeDict`. |
141
+ | `TypeDict` | Bundle of named structure and enum dictionaries. |
142
+ | `decode` | Decode bytes according to a `StructLayout`. |
143
+ | `encode` | Encode a `StructInstance` according to a `StructLayout`. |
144
+ | `load_struct_layout` | Load a `StructLayout` from JSON. |
145
+ | `save_struct_layout` | Save a `StructLayout` as JSON. |
146
+
147
+ ### Types (`types.py`)
148
+
149
+ Classes are listed in their definition order. The member tables show the
150
+ dataclass fields or constructor attributes. Methods beginning with `_` are
151
+ internal implementation details and are omitted from the public method tables.
152
+
153
+ #### `EnumDef`
154
+
155
+ | Member | Type | Description |
156
+ | --- | --- | --- |
157
+ | `name` | `str` | Enumeration name. |
158
+ | `description` | `str \| None` | Optional description. |
159
+ | `values` | `dict[str, int]` | Mapping from enumeration names to integer values. |
160
+
161
+ | Method | Description |
162
+ | --- | --- |
163
+ | `to_dict()` | Convert the definition to a JSON-compatible dictionary. |
164
+ | `to_json()` | Convert the definition to a JSON string. |
165
+ | `from_dict(data)` | Create an `EnumDef` from a dictionary. |
166
+ | `serialize()` | Convert the definition to a typed dictionary. |
167
+ | `deserialize(data)` | Create an `EnumDef` from a typed dictionary. |
168
+ | `from_json(data)` | Create an `EnumDef` from a JSON string. |
169
+ | `__lt__(other)` | Compare enum definitions using their serialized ordering. |
170
+
171
+ #### `FieldDef`
172
+
173
+ | Member | Type | Description |
174
+ | --- | --- | --- |
175
+ | `name` | `str` | Field name. |
176
+ | `offset` | `InfoSize \| str` | Static offset or expression. |
177
+ | `size` | `InfoSize \| str` | Static size or expression. |
178
+ | `type` | `str \| StructDef` | Primitive, named, or nested field type. |
179
+ | `scale` | `float` | Numeric scale applied to the field. |
180
+ | `repeat` | `int \| None` | Number of repeated field values. |
181
+ | `description` | `str \| None` | Optional field description. |
182
+ | `range_expression` | `str \| None` | Optional value-range expression. |
183
+ | `enum_def_name` | `str \| None` | Name of the associated enum definition. |
184
+ | `byte_swap` | `bool \| str` | Whether to reverse bytes, or an expression controlling it. |
185
+
186
+ | Method | Description |
187
+ | --- | --- |
188
+ | `split_repeat(index, offset=None, size=None)` | Create one field definition from a repeated field. |
189
+ | `to_dict()` | Convert the definition to a JSON-compatible dictionary. |
190
+ | `to_json()` | Convert the definition to a JSON string. |
191
+ | `from_dict(data)` | Create a `FieldDef` from a dictionary. |
192
+ | `from_json(data)` | Create a `FieldDef` from a JSON string. |
193
+ | `__lt__(other)` | Compare field definitions using their serialized ordering. |
194
+
195
+ #### `FieldInstance`
196
+
197
+ | Member | Type | Description |
198
+ | --- | --- | --- |
199
+ | `field_def` | `FieldDef` | Definition of the field. |
200
+ | `value` | `Any` | Decoded or encodable field value. |
201
+ | `enum_item` | `tuple[str, int] \| None` | Matching enum name and value, when available. |
202
+ | `is_padding` | `bool` | Whether this instance represents padding. |
203
+
204
+ | Method | Description |
205
+ | --- | --- |
206
+ | `range_check(env=None)` | Evaluate the field's range expression. |
207
+ | `from_value(field_def, value, type_dict=None, is_padding=False)` | Create an instance and resolve matching enum metadata. |
208
+ | `__lt__(other)` | Compare field instances using field-definition order. |
209
+
210
+ #### `StructDef`
211
+
212
+ | Member | Type | Description |
213
+ | --- | --- | --- |
214
+ | `name` | `str` | Structure name. |
215
+ | `description` | `str` | Structure description. |
216
+ | `fields` | `list[FieldDef]` | Ordered field definitions. |
217
+
218
+ | Method | Description |
219
+ | --- | --- |
220
+ | `to_dict()` | Convert the definition to a JSON-compatible dictionary. |
221
+ | `to_json()` | Convert the definition to a JSON string. |
222
+ | `from_dict(data)` | Create a `StructDef` from a dictionary or legacy field list. |
223
+ | `from_json(data)` | Create a `StructDef` from a JSON string. |
224
+ | `__lt__(other)` | Compare structure definitions using their serialized ordering. |
225
+
226
+ #### `StructInstance`
227
+
228
+ | Member | Type | Description |
229
+ | --- | --- | --- |
230
+ | `struct_def` | `StructDef` | Structure definition for the instance. |
231
+ | `field_instances` | `list[FieldInstance]` | Stored field instances, kept in field order. |
232
+ | `size` | `InfoSize` | Total structure size. |
233
+
234
+ | Method | Description |
235
+ | --- | --- |
236
+ | `append_field_instance(field_instance)` | Append one field instance. |
237
+ | `extend_field_instances(field_instances)` | Append multiple field instances. |
238
+ | `__iter__()` | Iterate over field instances. |
239
+ | `__len__()` | Return the number of field instances. |
240
+ | `__getitem__(index)` | Get a field instance by index. |
241
+ | `__post_init__()` | Normalize field order, size, and generated padding after construction. |
242
+ | `__lt__(other)` | Compare structure instances using their serialized ordering. |
243
+
244
+ #### `EnumDict`
245
+
246
+ | Member | Type | Description |
247
+ | --- | --- | --- |
248
+ | `_items` | `dict[str, EnumDef]` | Stored enum definitions. Use mapping operations or `items_dict()`. |
249
+
250
+ | Method | Description |
251
+ | --- | --- |
252
+ | `__getitem__(key)` | Get an enum definition by name. |
253
+ | `__setitem__(key, value)` | Store an enum definition. |
254
+ | `__delitem__(key)` | Delete an enum definition. |
255
+ | `__iter__()` | Iterate over enum names. |
256
+ | `__len__()` | Return the number of enum definitions. |
257
+ | `items_dict()` | Return the underlying dictionary. |
258
+
259
+ #### `StructDict`
260
+
261
+ | Member | Type | Description |
262
+ | --- | --- | --- |
263
+ | `_items` | `dict[str, StructDef]` | Stored structure definitions. Use mapping operations or `items_dict()`. |
264
+
265
+ | Method | Description |
266
+ | --- | --- |
267
+ | `__getitem__(key)` | Get a structure definition by name. |
268
+ | `__setitem__(key, value)` | Store a structure definition. |
269
+ | `__delitem__(key)` | Delete a structure definition. |
270
+ | `__iter__()` | Iterate over structure names. |
271
+ | `__len__()` | Return the number of structure definitions. |
272
+ | `items_dict()` | Return the underlying dictionary. |
273
+
274
+ #### `TypeDict`
275
+
276
+ | Member | Type | Description |
277
+ | --- | --- | --- |
278
+ | `enum_dict` | `EnumDict` | Named enum definitions. |
279
+ | `struct_dict` | `StructDict` | Named structure definitions. |
280
+
281
+ `TypeDict(enum_dict=None, struct_dict=None)` constructs both containers from
282
+ optional dictionaries.
283
+
284
+ #### `StructLayout`
285
+
286
+ | Member | Type | Description |
287
+ | --- | --- | --- |
288
+ | `struct_def_name` | `str` | Key of the root structure in `type_dict.struct_dict`. |
289
+ | `type_dict` | `TypeDict` | Structure and enum definitions used for resolution. |
290
+
291
+ `StructLayout(struct_def_name, type_dict)` constructs a layout bundle.
292
+
293
+ ### Codec (`codec.py`)
294
+
295
+ The following public definitions are listed in their definition order. Names
296
+ beginning with `_` are internal helpers and are not part of the public API.
297
+
298
+ | Definition | Signature | Description |
299
+ | --- | --- | --- |
300
+ | `PRIMITIVE_TYPES` | `set[str]` | Built-in field type names. |
301
+ | `save_struct_layout` | `(struct_layout, path) -> None` | Save a layout to a JSON file. |
302
+ | `load_struct_layout` | `(path) -> StructLayout` | Load a layout from a JSON file. |
303
+ | `save_struct_def_dict` | `(path, struct_def_dict) -> None` | Save a structure-definition dictionary through the layout format. |
304
+ | `load_struct_def_dict` | `(path) -> dict[str, StructDef]` | Load a structure-definition dictionary. |
305
+ | `save_enum_def_dict` | `(path, enum_def_dict) -> None` | Save an enum-definition dictionary through the layout format. |
306
+ | `load_enum_def_dict` | `(path) -> dict[str, EnumDef]` | Load an enum-definition dictionary. |
307
+ | `encode` | `(struct_layout, struct_instance, buf, padding_alignment_bits=32) -> bytearray` | Encode a complete structure. |
308
+ | `decode_field` | `(field_def, data, env=None, type_dict=None, padding_alignment_bits=32) -> FieldInstance \| None` | Decode one field. |
309
+ | `decode` | `(struct_layout, data, padding_alignment_bits=32) -> StructInstance` | Decode a complete structure. |
310
+
311
+ `save_struct_def_dict`, `load_struct_def_dict`, `save_enum_def_dict`, and
312
+ `load_enum_def_dict` are available from `sltcodec.codec` for dictionary-level
313
+ compatibility. The package root exports the complete `StructLayout` API:
314
+ `save_struct_layout` and `load_struct_layout`.
@@ -0,0 +1,85 @@
1
+ # StructLayoutToolkitCodec Development
2
+
3
+ This repository contains `sltcodec`, a Python package for encoding and
4
+ decoding bytearrays according to structured layout definitions.
5
+
6
+ The package description and user-facing API examples used for PyPI are in
7
+ [README_pypi.md](README_pypi.md).
8
+
9
+ ## Development Setup
10
+
11
+ This project requires Python 3.12 or newer and uses `uv` for dependency and
12
+ environment management.
13
+
14
+ ```bash
15
+ uv sync --all-extras
16
+ ```
17
+
18
+ The project also keeps a local virtual environment at `.venv` when using the
19
+ standard `uv` workflow.
20
+
21
+ ## Run Tests
22
+
23
+ Run the complete test suite:
24
+
25
+ ```bash
26
+ uv run pytest
27
+ ```
28
+
29
+ Run a focused test module:
30
+
31
+ ```bash
32
+ uv run pytest tests/test_codec.py -q
33
+ uv run pytest tests/test_types.py -q
34
+ uv run pytest tests/test_persistence.py -q
35
+ ```
36
+
37
+ Tests are organized by responsibility:
38
+
39
+ - `tests/test_codec.py`: encoding, decoding, padding, repetition, and byte swapping
40
+ - `tests/test_types.py`: field, enum, structure, and instance behavior
41
+ - `tests/test_persistence.py`: `StructLayout` JSON persistence
42
+
43
+ ## Source Layout
44
+
45
+ ```text
46
+ src/sltcodec/
47
+ __init__.py Public package exports
48
+ codec.py Encoding, decoding, and layout persistence
49
+ types.py Field, enum, structure, and layout types
50
+ tests/ Pytest test modules
51
+ ```
52
+
53
+ ## Public API Changes
54
+
55
+ `encode` and `decode` receive one `StructLayout` object. The structure
56
+ definition is resolved by `struct_layout.struct_def_name` from
57
+ `struct_layout.type_dict.struct_dict`; enum definitions are resolved from the
58
+ same `TypeDict`.
59
+
60
+ Layout persistence uses the explicit names `save_struct_layout` and
61
+ `load_struct_layout`. The former `save_type_dict` and `load_type_dict` API is
62
+ not part of the current public interface.
63
+
64
+ ## Versioning And Release
65
+
66
+ Version bump scripts are provided for patch, minor, and major releases:
67
+
68
+ ```powershell
69
+ .\bump_patch.ps1
70
+ .\bump_minor.ps1
71
+ .\bump_major.ps1
72
+ ```
73
+
74
+ Review the generated version change and run the full test suite before
75
+ building or publishing a package.
76
+
77
+ ## Build
78
+
79
+ The package uses Hatchling as its build backend. The package metadata points
80
+ to `README_pypi.md` so the PyPI project page contains the consumer-facing
81
+ documentation rather than repository development notes.
82
+
83
+ ```bash
84
+ uv build
85
+ ```
@@ -0,0 +1,297 @@
1
+ # StructLayoutToolkitCodec
2
+
3
+ `sltcodec` encodes and decodes bytearrays according to structured layout definitions. It uses `sltcore` for bit-level access.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install sltcodec
9
+ ```
10
+
11
+ ## Quick Example
12
+
13
+ `StructLayout` bundles the root structure name with its `TypeDict`. Pass the same layout to both `encode` and `decode`.
14
+
15
+ ```python
16
+ from sltcore import InfoSize
17
+ from sltcodec import (
18
+ FieldDef,
19
+ FieldInstance,
20
+ StructDef,
21
+ StructInstance,
22
+ StructLayout,
23
+ TypeDict,
24
+ decode,
25
+ encode,
26
+ )
27
+
28
+ fields = [
29
+ FieldDef(name="flag", offset=InfoSize(0, 0), size=InfoSize(0, 1), type="bool"),
30
+ FieldDef(name="value", offset=InfoSize(0, 1), size=InfoSize(1, 0), type="unsigned int"),
31
+ ]
32
+ struct_def = StructDef(name="Packet", fields=fields)
33
+ layout = StructLayout(
34
+ struct_def_name="Packet",
35
+ type_dict=TypeDict(struct_dict={"Packet": struct_def}),
36
+ )
37
+ instance = StructInstance(
38
+ struct_def=struct_def,
39
+ field_instances=[
40
+ FieldInstance(fields[0], True),
41
+ FieldInstance(fields[1], 0xA5),
42
+ ],
43
+ )
44
+
45
+ encoded = encode(layout, instance, bytearray())
46
+ decoded = decode(layout, encoded)
47
+ ```
48
+
49
+ ## Field Types
50
+
51
+ The public `PRIMITIVE_TYPES` set contains:
52
+
53
+ ```python
54
+ {"bool", "signed int", "int", "unsigned int", "float", "bytearray", "bytes"}
55
+ ```
56
+
57
+ `FieldDef.type` may also be a nested `StructDef` or an expression string. Expressions can use values from previously processed fields, allowing dynamic types and sizes. Repeated fields, byte swapping, padding, ranges, and enum metadata are supported.
58
+
59
+ ## Enums And Named Structures
60
+
61
+ Store named structures in `TypeDict.struct_dict` and enums in `TypeDict.enum_dict`. A field refers to an enum by its `enum_def_name`; the codec resolves it through `StructLayout.type_dict`.
62
+
63
+ ```python
64
+ | `__lt__(other)` | Compare enum definitions using their serialized ordering. |
65
+ from sltcore import InfoSize
66
+ from sltcodec import EnumDef, FieldDef, StructDef, StructLayout, TypeDict, decode
67
+
68
+ enum_def = EnumDef(name="Status", values={"OK": 0, "NG": 1})
69
+ packet = StructDef(
70
+ name="Packet",
71
+ fields=[FieldDef(
72
+ name="status",
73
+ offset=InfoSize(0, 0),
74
+ size=InfoSize(1, 0),
75
+ type="unsigned int",
76
+ enum_def_name="Status",
77
+ )],
78
+ )
79
+ layout = StructLayout(
80
+ struct_def_name="Packet",
81
+ type_dict=TypeDict(
82
+ struct_dict={"Packet": packet},
83
+ enum_dict={"Status": enum_def},
84
+ ),
85
+ )
86
+
87
+ decoded = decode(layout, bytearray(b"\x01"))
88
+ | `__lt__(other)` | Compare field definitions using their serialized ordering. |
89
+ assert decoded.field_instances[0].enum_item == ("NG", 1)
90
+ ```
91
+
92
+ ## Saving And Loading
93
+
94
+ Persist a complete `StructLayout`, including its root structure name, structure definitions, and enum definitions, with `save_struct_layout` and `load_struct_layout`.
95
+
96
+ ```python
97
+ from pathlib import Path
98
+ from sltcodec import load_struct_layout, save_struct_layout
99
+
100
+ path = Path("struct_layout.json")
101
+ save_struct_layout(layout, path)
102
+ loaded = load_struct_layout(path)
103
+ | `__lt__(other)` | Compare field instances using field-definition order. |
104
+ assert loaded == layout
105
+ ```
106
+
107
+ `InfoSize` values are stored as typed JSON dictionaries. Expression-based offsets and sizes remain strings and are evaluated when the layout is used.
108
+
109
+ ## Public API Reference
110
+
111
+ The following symbols are exported by `sltcodec.__all__` in this order:
112
+
113
+ | Symbol | Description |
114
+ | --- | --- |
115
+ | `PRIMITIVE_TYPES` | Set of built-in field type names supported by the codec. |
116
+ | `EnumDef` | Immutable definition of an enumeration. |
117
+ | `EnumDict` | Dictionary-like container for `EnumDef` objects. |
118
+ | `FieldDef` | Immutable definition of one structured field. |
119
+ | `FieldInstance` | Immutable field definition and decoded/encodable value pair. |
120
+ | `StructDef` | Immutable definition that groups fields into a structure. |
121
+ | `StructDict` | Dictionary-like container for `StructDef` objects. |
122
+ | `StructInstance` | Decoded/encodable structure instance containing field instances. |
123
+ | `StructLayout` | Bundle of the root structure name and its `TypeDict`. |
124
+ | `TypeDict` | Bundle of named structure and enum dictionaries. |
125
+ | `decode` | Decode bytes according to a `StructLayout`. |
126
+ | `encode` | Encode a `StructInstance` according to a `StructLayout`. |
127
+ | `load_struct_layout` | Load a `StructLayout` from JSON. |
128
+ | `save_struct_layout` | Save a `StructLayout` as JSON. |
129
+
130
+ ### Types (`types.py`)
131
+
132
+ Classes are listed in their definition order. The member tables show the
133
+ dataclass fields or constructor attributes. Methods beginning with `_` are
134
+ internal implementation details and are omitted from the public method tables.
135
+
136
+ #### `EnumDef`
137
+
138
+ | Member | Type | Description |
139
+ | --- | --- | --- |
140
+ | `name` | `str` | Enumeration name. |
141
+ | `description` | `str \| None` | Optional description. |
142
+ | `values` | `dict[str, int]` | Mapping from enumeration names to integer values. |
143
+
144
+ | Method | Description |
145
+ | --- | --- |
146
+ | `to_dict()` | Convert the definition to a JSON-compatible dictionary. |
147
+ | `to_json()` | Convert the definition to a JSON string. |
148
+ | `from_dict(data)` | Create an `EnumDef` from a dictionary. |
149
+ | `serialize()` | Convert the definition to a typed dictionary. |
150
+ | `deserialize(data)` | Create an `EnumDef` from a typed dictionary. |
151
+ | `from_json(data)` | Create an `EnumDef` from a JSON string. |
152
+ | `__lt__(other)` | Compare enum definitions using their serialized ordering. |
153
+
154
+ #### `FieldDef`
155
+
156
+ | Member | Type | Description |
157
+ | --- | --- | --- |
158
+ | `name` | `str` | Field name. |
159
+ | `offset` | `InfoSize \| str` | Static offset or expression. |
160
+ | `size` | `InfoSize \| str` | Static size or expression. |
161
+ | `type` | `str \| StructDef` | Primitive, named, or nested field type. |
162
+ | `scale` | `float` | Numeric scale applied to the field. |
163
+ | `repeat` | `int \| None` | Number of repeated field values. |
164
+ | `description` | `str \| None` | Optional field description. |
165
+ | `range_expression` | `str \| None` | Optional value-range expression. |
166
+ | `enum_def_name` | `str \| None` | Name of the associated enum definition. |
167
+ | `byte_swap` | `bool \| str` | Whether to reverse bytes, or an expression controlling it. |
168
+
169
+ | Method | Description |
170
+ | --- | --- |
171
+ | `split_repeat(index, offset=None, size=None)` | Create one field definition from a repeated field. |
172
+ | `to_dict()` | Convert the definition to a JSON-compatible dictionary. |
173
+ | `to_json()` | Convert the definition to a JSON string. |
174
+ | `from_dict(data)` | Create a `FieldDef` from a dictionary. |
175
+ | `from_json(data)` | Create a `FieldDef` from a JSON string. |
176
+ | `__lt__(other)` | Compare field definitions using their serialized ordering. |
177
+
178
+ #### `FieldInstance`
179
+
180
+ | Member | Type | Description |
181
+ | --- | --- | --- |
182
+ | `field_def` | `FieldDef` | Definition of the field. |
183
+ | `value` | `Any` | Decoded or encodable field value. |
184
+ | `enum_item` | `tuple[str, int] \| None` | Matching enum name and value, when available. |
185
+ | `is_padding` | `bool` | Whether this instance represents padding. |
186
+
187
+ | Method | Description |
188
+ | --- | --- |
189
+ | `range_check(env=None)` | Evaluate the field's range expression. |
190
+ | `from_value(field_def, value, type_dict=None, is_padding=False)` | Create an instance and resolve matching enum metadata. |
191
+ | `__lt__(other)` | Compare field instances using field-definition order. |
192
+
193
+ #### `StructDef`
194
+
195
+ | Member | Type | Description |
196
+ | --- | --- | --- |
197
+ | `name` | `str` | Structure name. |
198
+ | `description` | `str` | Structure description. |
199
+ | `fields` | `list[FieldDef]` | Ordered field definitions. |
200
+
201
+ | Method | Description |
202
+ | --- | --- |
203
+ | `to_dict()` | Convert the definition to a JSON-compatible dictionary. |
204
+ | `to_json()` | Convert the definition to a JSON string. |
205
+ | `from_dict(data)` | Create a `StructDef` from a dictionary or legacy field list. |
206
+ | `from_json(data)` | Create a `StructDef` from a JSON string. |
207
+ | `__lt__(other)` | Compare structure definitions using their serialized ordering. |
208
+
209
+ #### `StructInstance`
210
+
211
+ | Member | Type | Description |
212
+ | --- | --- | --- |
213
+ | `struct_def` | `StructDef` | Structure definition for the instance. |
214
+ | `field_instances` | `list[FieldInstance]` | Stored field instances, kept in field order. |
215
+ | `size` | `InfoSize` | Total structure size. |
216
+
217
+ | Method | Description |
218
+ | --- | --- |
219
+ | `append_field_instance(field_instance)` | Append one field instance. |
220
+ | `extend_field_instances(field_instances)` | Append multiple field instances. |
221
+ | `__iter__()` | Iterate over field instances. |
222
+ | `__len__()` | Return the number of field instances. |
223
+ | `__getitem__(index)` | Get a field instance by index. |
224
+ | `__post_init__()` | Normalize field order, size, and generated padding after construction. |
225
+ | `__lt__(other)` | Compare structure instances using their serialized ordering. |
226
+
227
+ #### `EnumDict`
228
+
229
+ | Member | Type | Description |
230
+ | --- | --- | --- |
231
+ | `_items` | `dict[str, EnumDef]` | Stored enum definitions. Use mapping operations or `items_dict()`. |
232
+
233
+ | Method | Description |
234
+ | --- | --- |
235
+ | `__getitem__(key)` | Get an enum definition by name. |
236
+ | `__setitem__(key, value)` | Store an enum definition. |
237
+ | `__delitem__(key)` | Delete an enum definition. |
238
+ | `__iter__()` | Iterate over enum names. |
239
+ | `__len__()` | Return the number of enum definitions. |
240
+ | `items_dict()` | Return the underlying dictionary. |
241
+
242
+ #### `StructDict`
243
+
244
+ | Member | Type | Description |
245
+ | --- | --- | --- |
246
+ | `_items` | `dict[str, StructDef]` | Stored structure definitions. Use mapping operations or `items_dict()`. |
247
+
248
+ | Method | Description |
249
+ | --- | --- |
250
+ | `__getitem__(key)` | Get a structure definition by name. |
251
+ | `__setitem__(key, value)` | Store a structure definition. |
252
+ | `__delitem__(key)` | Delete a structure definition. |
253
+ | `__iter__()` | Iterate over structure names. |
254
+ | `__len__()` | Return the number of structure definitions. |
255
+ | `items_dict()` | Return the underlying dictionary. |
256
+
257
+ #### `TypeDict`
258
+
259
+ | Member | Type | Description |
260
+ | --- | --- | --- |
261
+ | `enum_dict` | `EnumDict` | Named enum definitions. |
262
+ | `struct_dict` | `StructDict` | Named structure definitions. |
263
+
264
+ `TypeDict(enum_dict=None, struct_dict=None)` constructs both containers from
265
+ optional dictionaries.
266
+
267
+ #### `StructLayout`
268
+
269
+ | Member | Type | Description |
270
+ | --- | --- | --- |
271
+ | `struct_def_name` | `str` | Key of the root structure in `type_dict.struct_dict`. |
272
+ | `type_dict` | `TypeDict` | Structure and enum definitions used for resolution. |
273
+
274
+ `StructLayout(struct_def_name, type_dict)` constructs a layout bundle.
275
+
276
+ ### Codec (`codec.py`)
277
+
278
+ The following public definitions are listed in their definition order. Names
279
+ beginning with `_` are internal helpers and are not part of the public API.
280
+
281
+ | Definition | Signature | Description |
282
+ | --- | --- | --- |
283
+ | `PRIMITIVE_TYPES` | `set[str]` | Built-in field type names. |
284
+ | `save_struct_layout` | `(struct_layout, path) -> None` | Save a layout to a JSON file. |
285
+ | `load_struct_layout` | `(path) -> StructLayout` | Load a layout from a JSON file. |
286
+ | `save_struct_def_dict` | `(path, struct_def_dict) -> None` | Save a structure-definition dictionary through the layout format. |
287
+ | `load_struct_def_dict` | `(path) -> dict[str, StructDef]` | Load a structure-definition dictionary. |
288
+ | `save_enum_def_dict` | `(path, enum_def_dict) -> None` | Save an enum-definition dictionary through the layout format. |
289
+ | `load_enum_def_dict` | `(path) -> dict[str, EnumDef]` | Load an enum-definition dictionary. |
290
+ | `encode` | `(struct_layout, struct_instance, buf, padding_alignment_bits=32) -> bytearray` | Encode a complete structure. |
291
+ | `decode_field` | `(field_def, data, env=None, type_dict=None, padding_alignment_bits=32) -> FieldInstance \| None` | Decode one field. |
292
+ | `decode` | `(struct_layout, data, padding_alignment_bits=32) -> StructInstance` | Decode a complete structure. |
293
+
294
+ `save_struct_def_dict`, `load_struct_def_dict`, `save_enum_def_dict`, and
295
+ `load_enum_def_dict` are available from `sltcodec.codec` for dictionary-level
296
+ compatibility. The package root exports the complete `StructLayout` API:
297
+ `save_struct_layout` and `load_struct_layout`.