|
| 1 | +# Zig Interfaces & Validation |
| 2 | + |
| 3 | +A compile-time interface checker for Zig that enables interface-based design |
| 4 | +with comprehensive type checking and detailed error reporting. |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +This library provides a way to define and verify interfaces in Zig at compile |
| 9 | +time. It supports: |
| 10 | + |
| 11 | +- Type-safe interface definitions with detailed error reporting |
| 12 | +- Interface embedding (composition) |
| 13 | +- Complex type validation including structs, enums, arrays, and slices |
| 14 | +- Comprehensive compile-time error messages with helpful hints |
| 15 | +- Flexible error union compatibility with `anyerror` |
| 16 | + |
| 17 | +## Install |
| 18 | + |
| 19 | +``` |
| 20 | +TODO |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +1. Define an interface with required method signatures: |
| 26 | + |
| 27 | +```zig |
| 28 | +const Repository = Interface(.{ |
| 29 | + .create = fn(anytype, User) anyerror!u32, |
| 30 | + .findById = fn(anytype, u32) anyerror!?User, |
| 31 | + .update = fn(anytype, User) anyerror!void, |
| 32 | + .delete = fn(anytype, u32) anyerror!void, |
| 33 | +}, null); |
| 34 | +``` |
| 35 | + |
| 36 | +2. Implement the interface methods in your type: |
| 37 | + |
| 38 | +```zig |
| 39 | +const InMemoryRepository = struct { |
| 40 | + allocator: std.mem.Allocator, |
| 41 | + users: std.AutoHashMap(u32, User), |
| 42 | + next_id: u32, |
| 43 | +
|
| 44 | + pub fn create(self: *InMemoryRepository, user: User) !u32 { |
| 45 | + var new_user = user; |
| 46 | + new_user.id = self.next_id; |
| 47 | + try self.users.put(self.next_id, new_user); |
| 48 | + self.next_id += 1; |
| 49 | + return new_user.id; |
| 50 | + } |
| 51 | +
|
| 52 | + // ... other Repository methods |
| 53 | +}; |
| 54 | +``` |
| 55 | + |
| 56 | +3. Verify the implementation at compile time: |
| 57 | + |
| 58 | +```zig |
| 59 | +// In functions that accept interface implementations: |
| 60 | +fn createUser(repo: anytype, name: []const u8, email: []const u8) !User { |
| 61 | + comptime Repository.satisfiedBy(@TypeOf(repo)); |
| 62 | + // ... rest of implementation |
| 63 | +} |
| 64 | +
|
| 65 | +// Or verify directly: |
| 66 | +comptime Repository.satisfiedBy(InMemoryRepository); |
| 67 | +``` |
| 68 | + |
| 69 | +## Interface Embedding |
| 70 | + |
| 71 | +Interfaces can embed other interfaces to combine their requirements: |
| 72 | + |
| 73 | +```zig |
| 74 | +const Logger = Interface(.{ |
| 75 | + .log = fn(anytype, []const u8) void, |
| 76 | + .getLogLevel = fn(anytype) u8, |
| 77 | +}, null); |
| 78 | +
|
| 79 | +const Metrics = Interface(.{ |
| 80 | + .increment = fn(anytype, []const u8) void, |
| 81 | + .getValue = fn(anytype, []const u8) u64, |
| 82 | +}, .{ Logger }); // Embeds Logger interface |
| 83 | +
|
| 84 | +// Now implements both Metrics and Logger methods |
| 85 | +const MonitoredRepository = Interface(.{ |
| 86 | + .create = fn(anytype, User) anyerror!u32, |
| 87 | + .findById = fn(anytype, u32) anyerror!?User, |
| 88 | +}, .{ Metrics }); |
| 89 | +``` |
| 90 | + |
| 91 | +> Note: you can embed arbitrarily many interfaces! |
| 92 | +
|
| 93 | +## Error Reporting |
| 94 | + |
| 95 | +The library provides detailed compile-time errors when implementations don't |
| 96 | +match: |
| 97 | + |
| 98 | +```zig |
| 99 | +// Wrong parameter type ([]u8 vs []const u8) |
| 100 | +const BadImpl = struct { |
| 101 | + pub fn writeAll(self: @This(), data: []u8) !void { |
| 102 | + _ = self; |
| 103 | + _ = data; |
| 104 | + } |
| 105 | +}; |
| 106 | +
|
| 107 | +// Results in compile error: |
| 108 | +// error: Method 'writeAll' parameter 1 has incorrect type: |
| 109 | +// └─ Expected: []const u8 |
| 110 | +// └─ Got: []u8 |
| 111 | +// └─ Hint: Consider making the parameter type const |
| 112 | +``` |
| 113 | + |
| 114 | +## Complex Types |
| 115 | + |
| 116 | +The interface checker supports complex types including: |
| 117 | + |
| 118 | +```zig |
| 119 | +const ComplexTypes = Interface(.{ |
| 120 | + .process = fn( |
| 121 | + anytype, |
| 122 | + struct { config: Config, points: []const DataPoint }, |
| 123 | + enum { ready, processing, error }, |
| 124 | + []const struct { |
| 125 | + timestamp: i64, |
| 126 | + data: ?[]const DataPoint, |
| 127 | + status: Status, |
| 128 | + } |
| 129 | + ) anyerror!?ProcessingResult, |
| 130 | +}, null); |
| 131 | +``` |
0 commit comments