aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-04-30 17:36:52 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2026-04-30 17:36:52 +0200
commit5ecd48d4b9f928beb4143f88e802ee4d9e25a2bd (patch)
tree6fd4944a8c81891046c7006336f46b808b56d08d /src
parent103efd52b59f77fbfde6a1b7daf65b16c2c26810 (diff)
refactor(element): supports more type in root append
Diffstat (limited to 'src')
-rw-r--r--src/content.zig2
-rw-r--r--src/eval/Root.zig8
-rw-r--r--src/eval/html/Element.zig4
-rw-r--r--src/eval/html/Root.zig2
-rw-r--r--src/link.zig5
-rw-r--r--src/paragraph.zig2
-rw-r--r--src/quote.zig2
7 files changed, 14 insertions, 11 deletions
diff --git a/src/content.zig b/src/content.zig
index 067c44f..7e0669a 100644
--- a/src/content.zig
+++ b/src/content.zig
@@ -17,7 +17,7 @@ pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
switch (v.kind) {
.literal => {
const el = try Element.Literal.init(alloc, v.content);
- content.append(el.element());
+ content.append(el);
},
.bold => content.append(try parseModifier(alloc, l, .bold, "b")),
.italic => content.append(try parseModifier(alloc, l, .italic, "em")),
diff --git a/src/eval/Root.zig b/src/eval/Root.zig
index 1f2b61b..048dd3b 100644
--- a/src/eval/Root.zig
+++ b/src/eval/Root.zig
@@ -31,7 +31,13 @@ pub fn allocator(self: *Self) Allocator {
return self.arena.allocator();
}
-pub fn append(self: *Self, el: Element) void {
+pub fn append(self: *Self, raw: anytype) void {
+ const el: Element = blk: {
+ const T = @TypeOf(raw);
+ if (T == Element) break :blk raw;
+ if (std.meta.hasMethod(T, "element")) break :blk raw.element();
+ @compileError("cannot convert " ++ @typeName(T) ++ " into " ++ @typeName(Element));
+ };
self.content.append(&el.node().node);
}
diff --git a/src/eval/html/Element.zig b/src/eval/html/Element.zig
index c6197b8..52ee58d 100644
--- a/src/eval/html/Element.zig
+++ b/src/eval/html/Element.zig
@@ -16,8 +16,8 @@ pub fn Wrapper(comptime V: type) type {
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 (!@hasDecl(V, "element")) @compileError("missing declaration 'element' for " ++ @typeName(V));
- if (!@hasDecl(V, "render")) @compileError("missing declaration 'render' for " ++ @typeName(V));
+ 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,
diff --git a/src/eval/html/Root.zig b/src/eval/html/Root.zig
index acfe82e..6c4c37a 100644
--- a/src/eval/html/Root.zig
+++ b/src/eval/html/Root.zig
@@ -39,7 +39,7 @@ pub fn append(self: *Self, raw: anytype) Error!void {
const el: Element = blk: {
const T = @TypeOf(raw);
if (T == Element) break :blk raw;
- if (@hasDecl(T, "html")) break :blk try raw.html(self.allocator());
+ if (std.meta.hasMethod(T, "html")) break :blk try raw.html(self.allocator());
@compileError("cannot convert " ++ @typeName(T) ++ " into " ++ @typeName(Element));
};
self.content.append(&el.node().node);
diff --git a/src/link.zig b/src/link.zig
index ecfad83..1fb4a59 100644
--- a/src/link.zig
+++ b/src/link.zig
@@ -25,10 +25,7 @@ pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
if (!eql(u8, next.content, "](")) return Error.InvalidLink;
break;
},
- else => {
- const in = try content.parse(el.allocator(), l);
- el.append(in);
- },
+ else => el.append(try content.parse(el.allocator(), l)),
};
const href = l.next() orelse return Error.InvalidLink;
if (href.kind != .literal) return Error.InvalidLink;
diff --git a/src/paragraph.zig b/src/paragraph.zig
index f1fca67..f4a6619 100644
--- a/src/paragraph.zig
+++ b/src/paragraph.zig
@@ -23,7 +23,7 @@ pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
l.consume();
const future = l.peek() orelse return el.element();
if (!future.kind.isInParagraph()) return el.element();
- root.append((try Element.Literal.init(alloc, " ")).element());
+ root.append(try Element.Literal.init(alloc, " "));
},
else => root.append(try parseLine(alloc, l)),
};
diff --git a/src/quote.zig b/src/quote.zig
index ad5a156..0011f60 100644
--- a/src/quote.zig
+++ b/src/quote.zig
@@ -20,7 +20,7 @@ pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
.weak_delimiter => {
l.consume();
if (l.peek()) |it| if (it.kind != .quote) break;
- root.append((try Element.Literal.init(alloc, " ")).element());
+ root.append(try Element.Literal.init(alloc, " "));
continue;
},
.strong_delimiter => break,