From a13df3ca580dc31544ef092d5d37e18089af3517 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 30 Apr 2026 17:56:55 +0200 Subject: refactor(element): generalize element creation --- src/eval/html/Content.zig | 4 ++-- src/eval/html/Element.zig | 9 ++++++--- src/eval/html/Literal.zig | 4 ++-- src/eval/html/Root.zig | 4 ++-- src/eval/html/Void.zig | 4 ++-- 5 files changed, 14 insertions(+), 11 deletions(-) (limited to 'src/eval/html') diff --git a/src/eval/html/Content.zig b/src/eval/html/Content.zig index 96961da..7b9da3e 100644 --- a/src/eval/html/Content.zig +++ b/src/eval/html/Content.zig @@ -28,7 +28,7 @@ pub fn init(alloc: Allocator, tag: []const u8) Error!*Self { } pub fn element(self: *Self) Element { - return (Element.Wrapper(Self){ .ptr = self }).element(); + return Element.Wrapper(Self, render).init(self); } fn fromNode(context: *anyopaque) Element { @@ -36,7 +36,7 @@ fn fromNode(context: *anyopaque) Element { return self.element(); } -pub fn render(self: *Self, alloc: Allocator) Error![]const u8 { +fn render(self: *Self, alloc: Allocator) Error![]const u8 { var base = self.base; const b = try base.element().render(alloc); defer alloc.free(b); diff --git a/src/eval/html/Element.zig b/src/eval/html/Element.zig index 52ee58d..dd24bea 100644 --- a/src/eval/html/Element.zig +++ b/src/eval/html/Element.zig @@ -11,13 +11,12 @@ pub const Root = @import("Root.zig"); pub const Error = html.Error || Allocator.Error; -pub fn Wrapper(comptime V: type) type { +pub fn Wrapper(comptime V: type, comptime r: *const fn (*V, Allocator) Error![]const u8) type { comptime { if (!@hasField(V, "node")) @compileError("missing field 'node' for " ++ @typeName(V)); const nd = @FieldType(V, "node"); if (nd != Node) @compileError("invalid node's type: " ++ @typeName(nd) ++ ", want " ++ @typeName(Node)); if (!std.meta.hasMethod(V, "element")) @compileError("missing declaration 'element' for " ++ @typeName(V)); - if (!std.meta.hasMethod(V, "render")) @compileError("missing declaration 'render' for " ++ @typeName(V)); } return struct { ptr: *V, @@ -31,7 +30,11 @@ pub fn Wrapper(comptime V: type) type { fn render(context: *anyopaque, alloc: Allocator) Error![]const u8 { const self: *V = @ptrCast(@alignCast(context)); - return try self.render(alloc); + return try r(self, alloc); + } + + pub fn init(ptr: *V) Element { + return (Self{ .ptr = ptr }).element(); } pub fn element(self: Self) Element { diff --git a/src/eval/html/Literal.zig b/src/eval/html/Literal.zig index 9d6d91e..a5eb199 100644 --- a/src/eval/html/Literal.zig +++ b/src/eval/html/Literal.zig @@ -28,7 +28,7 @@ pub fn initNoEscape(alloc: Allocator, literal: []const u8) Error!*Element.Litera } pub fn element(self: *Self) Element { - return (Element.Wrapper(Self){ .ptr = self }).element(); + return Element.Wrapper(Self, render).init(self); } fn fromNode(context: *anyopaque) Element { @@ -36,6 +36,6 @@ fn fromNode(context: *anyopaque) Element { return self.element(); } -pub fn render(self: *Self, alloc: Allocator) Error![]const u8 { +fn render(self: *Self, alloc: Allocator) Error![]const u8 { return try alloc.dupe(u8, self.literal); } diff --git a/src/eval/html/Root.zig b/src/eval/html/Root.zig index 6c4c37a..af130d1 100644 --- a/src/eval/html/Root.zig +++ b/src/eval/html/Root.zig @@ -28,7 +28,7 @@ pub fn deinit(self: *Self) void { } pub fn element(self: *Self) Element { - return (Element.Wrapper(Self){ .ptr = self }).element(); + return Element.Wrapper(Self, render).init(self); } pub fn allocator(self: *Self) Allocator { @@ -50,7 +50,7 @@ fn fromNode(context: *anyopaque) Element { return self.element(); } -pub fn render(self: *Self, alloc: Allocator) Error![]const u8 { +fn render(self: *Self, alloc: Allocator) Error![]const u8 { if (self.content.first == null) return ""; var acc = try std.ArrayList(u8).initCapacity(alloc, 8); errdefer acc.deinit(alloc); diff --git a/src/eval/html/Void.zig b/src/eval/html/Void.zig index 431536c..17b369d 100644 --- a/src/eval/html/Void.zig +++ b/src/eval/html/Void.zig @@ -30,7 +30,7 @@ pub fn init(alloc: Allocator, tag: []const u8) Error!*Self { } pub fn element(self: *Self) Element { - return (Element.Wrapper(Self){ .ptr = self }).element(); + return Element.Wrapper(Self, render).init(self); } pub fn setAttribute(self: *Self, k: []const u8, v: []const u8) Error!void { @@ -62,7 +62,7 @@ fn fromNode(context: *anyopaque) Element { return self.element(); } -pub fn render(self: *Self, alloc: Allocator) Error![]const u8 { +fn render(self: *Self, alloc: Allocator) Error![]const u8 { const attr = try renderAttribute(alloc, &self.attributes, &self.class_list); defer if (attr) |it| alloc.free(it); var acc = try List(u8).initCapacity(alloc, self.tag.len + 2); -- cgit v1.2.3