2 |
3 |
4 |
5 |
6 |
7 |
Zig-DbC
8 |
9 | [](https://github.com/habedi/zig-dbc/actions/workflows/tests.yml)
10 | [](https://www.codefactor.io/repository/github/habedi/zig-dbc)
11 | [](https://ziglang.org/download/)
12 | [](https://habedi.github.io/zig-dbc/)
13 | [](https://github.com/habedi/zig-dbc/releases/latest)
14 | [](https://github.com/habedi/zig-dbc/blob/main/LICENSE)
15 |
16 | A Design by Contract Library for Zig
17 |
18 |
19 |
20 | ---
21 |
22 | Zig-DbC is a small library that provides a collection of functions to use
23 | [design by contract](https://en.wikipedia.org/wiki/Design_by_contract) (DbC) principles in Zig programs.
24 | It provides a simple and idiomatic API for defining preconditions, postconditions, and invariants that can be
25 | checked at runtime.
26 |
27 | A common use case for DbC (and by extension Zig-DbC) is adding checks that guarantee the code behaves as intended.
28 | This can be especially useful during the implementation of complex data structures and algorithms (like balanced trees
29 | and graphs) where correctness depends on specific conditions being met.
30 |
31 | ### Features
32 |
33 | - A simple API to define preconditions, postconditions, and invariants
34 | - `require` and `ensure` functions check preconditions and postconditions
35 | - `requiref` and `ensuref` functions check preconditions and postconditions with formatted error messages
36 | - `requireCtx` and `ensureCtx` functions check preconditions and postconditions with a context string
37 | - `contract` and `contractWithErrorTolerance` functions check invariants
38 | - Checks are active in `Debug`, `ReleaseSafe`, and `ReleaseSmall` build modes to catch bugs
39 | - In `ReleaseFast` mode, all checks are removed at compile time to remove overhead
40 |
41 | > [!IMPORTANT]
42 | > Zig-DbC is in early development, so bugs and breaking API changes are expected.
43 | > Please use the [issues page](https://github.com/habedi/zig-dbc/issues) to report bugs or request features.
44 |
45 | ---
46 |
47 | ### Getting Started
48 |
49 | You can add Zig-DbC to your project and start using it by following the steps below.
50 |
51 | #### Installation
52 |
53 | Run the following command in the root directory of your project to download Zig-DbC:
54 |
55 | ```sh
56 | zig fetch --save=dbc "https://github.com/habedi/zig-dbc/archive/