Skip to content

Commit 19760b0

Browse files
authored
Add documentation for the cstruct-stubgen tool (#87)
1 parent a2a27be commit 19760b0

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed
16.1 KB
Loading
30.1 KB
Loading
24.5 KB
Loading
26.8 KB
Loading

docs/source/projects/dissect.cstruct/index.rst

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,90 @@ Custom definition parsers
201201

202202
Don't like the C-like definition syntax? Write your own syntax parser!
203203

204+
cstruct-stubgen
205+
---------------
206+
207+
``cstruct-stubgen`` is a tool that generates Python stub files (``.pyi``) from ``dissect.cstruct`` definitions.
208+
These stubs provide an enhanced developer experience in your IDE, enabling type checking and code completion.
209+
210+
The most basic usage of ``cstruct-stubgen`` is as simple as:
211+
212+
.. code-block:: console
213+
214+
$ cstruct-stubgen /path/to/project/source/files
215+
216+
So in the case of the following cstruct definition:
217+
218+
.. code-block:: python
219+
220+
from dissect.cstruct import cstruct
221+
222+
c_systemtime_def = """
223+
struct SYSTEMTIME {
224+
WORD wYear;
225+
WORD wMonth;
226+
WORD wDayOfWeek;
227+
WORD wDay;
228+
WORD wHour;
229+
WORD wMinute;
230+
WORD wSecond;
231+
WORD wMilliseconds;
232+
};
233+
"""
234+
c_systemtime = cstruct().load(c_systemtime_def)
235+
236+
The generated ``.pyi`` file will look like:
237+
238+
.. code-block:: python
239+
240+
# Generated by cstruct-stubgen
241+
from typing import BinaryIO, Literal, overload
242+
243+
import dissect.cstruct as __cs__
244+
from typing_extensions import TypeAlias
245+
246+
class _c_systemtime(__cs__.cstruct):
247+
class SYSTEMTIME(__cs__.Structure):
248+
wYear: _c_systemtime.uint16
249+
wMonth: _c_systemtime.uint16
250+
wDayOfWeek: _c_systemtime.uint16
251+
wDay: _c_systemtime.uint16
252+
wHour: _c_systemtime.uint16
253+
wMinute: _c_systemtime.uint16
254+
wSecond: _c_systemtime.uint16
255+
wMilliseconds: _c_systemtime.uint16
256+
@overload
257+
def __init__(self, wYear: _c_systemtime.uint16 | None = ..., wMonth: _c_systemtime.uint16 | None = ..., wDayOfWeek: _c_systemtime.uint16 | None = ..., wDay: _c_systemtime.uint16 | None = ..., wHour: _c_systemtime.uint16 | None = ..., wMinute: _c_systemtime.uint16 | None = ..., wSecond: _c_systemtime.uint16 | None = ..., wMilliseconds: _c_systemtime.uint16 | None = ...): ...
258+
@overload
259+
def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
260+
261+
# Technically `c_systemtime` is an instance of `_c_systemtime`, but then we can't use it in type hints
262+
c_systemtime: TypeAlias = _c_systemtime
263+
264+
And the autocompletion, with an IDE such as VS Code, will look like:
265+
266+
.. image:: /images/autocompletion.png
267+
268+
.. image:: /images/autocompletion_attribute.png
269+
270+
Structure fields also have correct typing, enabling further type checking and code completion on compatible Python types:
271+
272+
.. image:: /images/field_typing_struct.png
273+
274+
.. image:: /images/field_typing_char.png
275+
276+
.. note::
277+
278+
``cstruct-stubgen`` generates type hints for cstruct definitions only.
279+
Other definitions or functions in the ``.py`` file are not included.
280+
To make them visible in your IDE, manually add the missing functions and variables to the ``.pyi`` file.
281+
282+
.. sphinx_argparse_cli::
283+
:module: dissect.cstruct.tools.stubgen
284+
:func: main
285+
:prog: cstruct-stubgen
286+
:hook:
287+
204288
Reference
205289
---------
206290

0 commit comments

Comments
 (0)