Add declarative syntax#161
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #161 +/- ##
======================================
Coverage 0.00% 0.00%
======================================
Files 22 23 +1
Lines 2700 2926 +226
======================================
- Misses 2700 2926 +226
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds an optional “declarative” API for defining cstruct Structure/Union types using Python class syntax + type annotations, while keeping parity with existing cstruct behavior and allowing either auto-created or user-supplied cstruct instances.
Changes:
- Introduces
dissect.cstruct.declarativewithStruct/Unionbases, unbound type references (e.g.uint32,uint8[4]), pointer/array helpers, andfield(...)specs (includingtyping.Annotatedsupport). - Extends array subscripting (
Type[...]) to accept string expressions (converted toExpression) for declarative string parsing. - Adds comprehensive tests for the declarative feature and updates Ruff per-file ignores for the new test module.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
dissect/cstruct/declarative.py |
New declarative structure/union definition layer built on annotations + metaclasses. |
dissect/cstruct/types/base.py |
Allows __getitem__ array sizes to be provided as str expressions (parsed as Expression). |
dissect/cstruct/__init__.py |
Exposes declarative Struct and field in the public package API. |
tests/test_declarative.py |
Adds test coverage for declarative structs/unions, forward refs, pointers, arrays, Annotated, alignment, etc. |
pyproject.toml |
Adds Ruff per-file ignores needed for declarative syntax test coverage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This adds an optional declarative syntax for structure definitions, for when you want to write structures in a more Pythonic syntax.
Or to bring-your-own-cstruct:
The resulting classes are proper subclasses of
Structureand have all the same behavior as regular cstruct structures.