add(zig-interface): dependency to check interface contracts at comptime
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 53s

The described interfaces for `Widget` and `Layout` are now defined and
correspondingly checked at comptime.
This commit is contained in:
2024-11-12 23:13:35 +01:00
parent 07e4819ecd
commit 28817d468a
8 changed files with 80 additions and 46 deletions

View File

@@ -22,9 +22,18 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
}
const Events = std.ArrayList(Event);
return struct {
const Type = struct {
const LayoutType = @This();
const Element = union(enum) {
layout: LayoutType,
widget: @import("widget.zig").Widget(Event, Renderer),
};
const Ptr = usize;
pub const Interface = @import("interface").Interface(.{
.handle = fn (anytype, Event) anyerror!*Events,
.render = fn (anytype, *Renderer) anyerror!void,
.deinit = fn (anytype) void,
}, .{});
const VTable = struct {
handle: *const fn (this: *LayoutType, event: Event) anyerror!*Events,
@@ -79,9 +88,17 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
}
// import and export of `Layout` implementations
pub const HStack = @import("layout/HStack.zig").Layout(Event, Renderer);
pub const VStack = @import("layout/VStack.zig").Layout(Event, Renderer);
pub const Padding = @import("layout/Padding.zig").Layout(Event, Renderer);
pub const Framing = @import("layout/Framing.zig").Layout(Event, Renderer);
pub const HStack = @import("layout/HStack.zig").Layout(Event, Element, Renderer);
pub const VStack = @import("layout/VStack.zig").Layout(Event, Element, Renderer);
pub const Padding = @import("layout/Padding.zig").Layout(Event, Element, Renderer);
pub const Framing = @import("layout/Framing.zig").Layout(Event, Element, Renderer);
};
// test widget implementation satisfies the interface
comptime Type.Interface.satisfiedBy(Type);
// TODO: there is a dependency loop (due to the necessary `Layout` type for the `Element`)
comptime Type.Interface.satisfiedBy(Type.HStack);
comptime Type.Interface.satisfiedBy(Type.VStack);
comptime Type.Interface.satisfiedBy(Type.Padding);
comptime Type.Interface.satisfiedBy(Type.Framing);
return Type;
}