Metadata-Version: 2.1
Name: pystructs3
Version: 0.0.8
Summary: Dataclass-Like Serialization Helpers for More Complex Data-Types
Author-email: Andrew C Scott <imgurbot12@gmail.com>
License: The MIT License (MIT)
        
        Copyright (c) 2025 Andrew C Scott
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
        
Project-URL: Repository, https://github.com/imgurbot12/pystructs
Keywords: struct,dataclass,parser,serialization
Classifier: Typing :: Typed
Classifier: License :: OSI Approved :: MIT License
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pyderive3>=0.0.7
Requires-Dist: typing-extensions>=4.7.1

PyStructs
---------

[![PyPI version](https://img.shields.io/pypi/v/pystructs3?style=for-the-badge)](https://pypi.org/project/pystructs3/)
[![Python versions](https://img.shields.io/pypi/pyversions/pystructs3?style=for-the-badge)](https://pypi.org/project/pystructs3/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=for-the-badge)](https://github.com/imgurbot12/pystructs/blob/master/LICENSE)
[![Made with Love](https://img.shields.io/badge/built%20with-%E2%99%A5-orange?style=for-the-badge)](https://github.com/imgurbot12/pystructs)

A convenient
[`dataclass`](https://docs.python.org/3/library/dataclasses.html)
version of python's
[`struct`](https://docs.python.org/3/library/struct.html) library
for packing and unpacking objects to their equivalent byte representations.

### Installation

```
pip install pystructs3
```

### Examples

###### Simple Example

```python
from pystructs import *

class Bar(Struct):
    z: U32

class Foo(Struct):
    x:   U8
    y:   U16
    bar: Bar

# pack and unpack structs with their builtin methods
foo1   = Foo(230, 65000, Bar(2147483648))
packed = foo1.pack()
foo2   = Foo.unpack(packed)
print('original', foo1)
print('packed', packed)
print('unpacked', foo2)

# equivalent functional version for additional utility
packed   = pack((U8, U16, U32), 230, 65000, 2147483648)
unpacked = unpack((U8, U16, U32), packed)
print('packed', packed)
print('unpacked', unpacked)
```

###### More Complex Example

```python
from typing import List
from ipaddress import IPv4Address, IPv6Address
from typing_extensions import Annotated
from pystructs import *

class Bar(Struct):
    mac:    MACAddr
    ip4:    IPv4
    ip6:    IPv6
    domain: Domain

class Foo(Struct):
    signed:   I8  # I8/I16/I32/I48/I64/I128
    unsigned: U32 # U8/U16/U32/U48/U64/U128
    custom:   Annotated[int, IntField(1, 'little', False)] # custom integer
    b_hinted: Annotated[bytes, HintedBytes(U16)] # dynamic with size hint
    b_static: bytes = field(field=StaticBytes(16)) # static sized bytes
    l_hinted: Annotated[List[int], HintedList(U8, U8)] # dynamic with size hint
    l_static: List[int] = field(field=StaticList(2, U8)) # static sized list

    # b_greedy: bytes = field(field=GreedyBytes()) # consumes rest of message
    # l_list: List[int] = field(field=GreedyList(U8)) # consumes rest of message

bar = Bar('01:02:03:04:05:06', IPv4Address('1.2.3.4'), IPv6Address('::1'), b'example.com')
foo = Foo(1, 2, 3, b'hinted', b'static', [4, 5, 6], [7, 8])

# use a context object when packing/unpacking multiple objects in sequence
ctx    = Context()
packed = bar.pack(ctx) + foo.pack(ctx)

ctx.reset() # context uses index to track place in buffer (must be reset)
bar2 = bar.unpack(packed, ctx)
foo2 = foo.unpack(packed, ctx)

print('foo1', foo)
print('bar1', bar)
print('packed', packed)
print('foo2', foo2)
print('bar2', bar2)
```
