{"id":20043242,"url":"https://github.com/nilslice/zig-interface","last_synced_at":"2025-04-06T10:10:34.981Z","repository":{"id":262393003,"uuid":"887097441","full_name":"nilslice/zig-interface","owner":"nilslice","description":"A compile-time interface checker for Zig that enables interface-based design with comprehensive type checking and detailed error reporting.","archived":false,"fork":false,"pushed_at":"2025-03-20T08:55:58.000Z","size":17,"stargazers_count":112,"open_issues_count":5,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-30T09:08:58.031Z","etag":null,"topics":["zig-package"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nilslice.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-12T06:51:36.000Z","updated_at":"2025-03-25T06:13:17.000Z","dependencies_parsed_at":"2025-02-28T05:04:55.609Z","dependency_job_id":"be099a44-32a6-44ae-87e5-cb7dc7327f49","html_url":"https://github.com/nilslice/zig-interface","commit_stats":null,"previous_names":["nilslice/zig-interface"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilslice%2Fzig-interface","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilslice%2Fzig-interface/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilslice%2Fzig-interface/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilslice%2Fzig-interface/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nilslice","download_url":"https://codeload.github.com/nilslice/zig-interface/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247464220,"owners_count":20942970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["zig-package"],"created_at":"2024-11-13T10:55:36.423Z","updated_at":"2025-04-06T10:10:34.957Z","avatar_url":"https://github.com/nilslice.png","language":"Zig","funding_links":[],"categories":["Zig"],"sub_categories":[],"readme":"# Zig Interfaces \u0026 Validation\n\nA compile-time interface checker for Zig that enables interface-based design\nwith comprehensive type checking and detailed error reporting.\n\n## Features\n\nThis library provides a way to define and verify interfaces in Zig at compile\ntime. It supports:\n\n- Type-safe interface definitions with detailed error reporting\n- Interface embedding (composition)\n- Complex type validation including structs, enums, arrays, and slices\n- Comprehensive compile-time error messages with helpful hints\n- Flexible error union compatibility with `anyerror`\n\n## Install\n\nAdd or update this library as a dependency in your zig project run the following command:\n\n```sh\nzig fetch --save git+https://github.com/nilslice/zig-interface\n```\n\nAfterwards add the library as a dependency to any module in your _build.zig_:\n\n```zig\n// ...\nconst interface_dependency = b.dependency(\"interface\", .{\n    .target = target,\n    .optimize = optimize,\n});\n\nconst exe = b.addExecutable(.{\n    .name = \"main\",\n    .root_source_file = b.path(\"src/main.zig\"),\n    .target = target,\n    .optimize = optimize,\n});\n// import the exposed `interface` module from the dependency\nexe.root_module.addImport(\"interface\", interface_dependency.module(\"interface\"));\n// ...\n```\n\nIn the end you can import the `interface` module. For example:\n\n```zig\nconst Interface = @import(\"interface\").Interface;\n\nconst Repository = Interface(.{\n    .create = fn(anytype, User) anyerror!u32,\n    .findById = fn(anytype, u32) anyerror!?User,\n    .update = fn(anytype, User) anyerror!void,\n    .delete = fn(anytype, u32) anyerror!void,\n}, null);\n```\n\n## Usage\n\n1. Define an interface with required method signatures:\n\n```zig\nconst Repository = Interface(.{\n    .create = fn(anytype, User) anyerror!u32,\n    .findById = fn(anytype, u32) anyerror!?User,\n    .update = fn(anytype, User) anyerror!void,\n    .delete = fn(anytype, u32) anyerror!void,\n}, null);\n```\n\n2. Implement the interface methods in your type:\n\n```zig\nconst InMemoryRepository = struct {\n    allocator: std.mem.Allocator,\n    users: std.AutoHashMap(u32, User),\n    next_id: u32,\n\n    pub fn create(self: *InMemoryRepository, user: User) !u32 {\n        var new_user = user;\n        new_user.id = self.next_id;\n        try self.users.put(self.next_id, new_user);\n        self.next_id += 1;\n        return new_user.id;\n    }\n\n    // ... other Repository methods\n};\n```\n\n3. Verify the implementation at compile time:\n\n```zig\n// In functions that accept interface implementations:\nfn createUser(repo: anytype, name: []const u8, email: []const u8) !User {\n    comptime Repository.satisfiedBy(@TypeOf(repo));\n    // ... rest of implementation\n}\n\n// Or verify directly:\ncomptime Repository.satisfiedBy(InMemoryRepository);\n```\n\n## Interface Embedding\n\nInterfaces can embed other interfaces to combine their requirements:\n\n```zig\nconst Logger = Interface(.{\n    .log = fn(anytype, []const u8) void,\n    .getLogLevel = fn(anytype) u8,\n}, null);\n\nconst Metrics = Interface(.{\n    .increment = fn(anytype, []const u8) void,\n    .getValue = fn(anytype, []const u8) u64,\n}, .{ Logger });  // Embeds Logger interface\n\n// Now implements both Metrics and Logger methods\nconst MonitoredRepository = Interface(.{\n    .create = fn(anytype, User) anyerror!u32,\n    .findById = fn(anytype, u32) anyerror!?User,\n}, .{ Metrics });\n```\n\n\u003e Note: you can embed arbitrarily many interfaces!\n\n## Error Reporting\n\nThe library provides detailed compile-time errors when implementations don't\nmatch:\n\n```zig\n// Wrong parameter type ([]u8 vs []const u8)\nconst BadImpl = struct {\n    pub fn writeAll(self: @This(), data: []u8) !void {\n        _ = self;\n        _ = data;\n    }\n};\n\n// Results in compile error:\n// error: Method 'writeAll' parameter 1 has incorrect type:\n//    └─ Expected: []const u8\n//    └─ Got: []u8\n//       └─ Hint: Consider making the parameter type const\n```\n\n## Complex Types\n\nThe interface checker supports complex types including:\n\n```zig\nconst ComplexTypes = Interface(.{\n    .process = fn(\n        anytype,\n        struct { config: Config, points: []const DataPoint },\n        enum { ready, processing, error },\n        []const struct {\n            timestamp: i64,\n            data: ?[]const DataPoint,\n            status: Status,\n        }\n    ) anyerror!?ProcessingResult,\n}, null);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilslice%2Fzig-interface","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnilslice%2Fzig-interface","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilslice%2Fzig-interface/lists"}