authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-22 15:19:00 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-22 15:19:00 -07:00
log50e32dd75d6577813c092d81bc217dffe6a2ae26
tree95db18b8517b6183236ea21c854d5e3b5f8e1bf8
parenta7c053bacbdfcdfce905f93fc6f6629cd618939f
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

QoL rework of the way we call '{#' and '{##' custom functions

construct the tuple in a way that can't fail so that any compile errors are thrown by @call this leans on the compiler more and allows for instance now seeing the offending function in the notes

1 files changed, 16 insertions(+), 24 deletions(-)

src/lib.zig+16-24
...@@ -301,15 +301,11 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val...@@ -301,15 +301,11 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val
301 const func = @field(opts.Ctx, "pek_" ++ v.name);301 const func = @field(opts.Ctx, "pek_" ++ v.name);
302 var list = std.ArrayList(u8).init(alloc);302 var list = std.ArrayList(u8).init(alloc);
303 defer list.deinit();303 defer list.deinit();
304 var args: std.meta.ArgsTuple(@TypeOf(func)) = undefined;304 comptime var types: [v.args.len]type = @splat(void);
305 comptime std.debug.assert(args.len - 2 == v.args.len);305 inline for (v.args, &types) |arg, *T| T.* = ResolveArg(arg, @TypeOf(data), @TypeOf(ctx));
306 args.@"0" = alloc;306 var args: std.meta.Tuple(&types) = undefined;
307 args.@"1" = list.writer();307 inline for (v.args, 0..) |arg, i| args[i] = try resolveArg(arg, alloc, data, ctx, opts);
308 inline for (v.args, 0..) |arg, i| {308 try @call(.auto, func, .{ alloc, list.writer() } ++ args);
309 const field_name = comptime nio.fmt.comptimePrint("{d}", .{i + 2});
310 @field(args, field_name) = try resolveArg(arg, alloc, data, ctx, opts);
311 }
312 try @call(.auto, func, args);
313 try writeReplacementString(writer, v.raw, opts.escaped, try list.toOwnedSlice());309 try writeReplacementString(writer, v.raw, opts.escaped, try list.toOwnedSlice());
314 return;310 return;
315 }311 }
...@@ -317,22 +313,18 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val...@@ -317,22 +313,18 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val
317 const func = @field(opts.Ctx, "pek__" ++ v.name);313 const func = @field(opts.Ctx, "pek__" ++ v.name);
318 var list = std.ArrayList(u8).init(alloc);314 var list = std.ArrayList(u8).init(alloc);
319 defer list.deinit();315 defer list.deinit();
320 const AT = std.meta.ArgsTuple(@TypeOf(func));316 const Tup = @typeInfo(@TypeOf(func)).@"fn".params[3].type.?;
321 const ATT = std.meta.fieldInfo(AT, .@"3").type;317 if (@typeInfo(Tup).@"struct".fields.len == 0) {
322 if (v.args.len != std.meta.fields(ATT).len) @compileError(nio.fmt.comptimePrint("expected:{d} - actual:{d}", .{ std.meta.fields(ATT).len, v.args.len }));318 // edge case branch because 'struct {}' is counted as a non-tuple
323 var tupargs = @as(ATT, undefined);319 try @call(.auto, func, .{ alloc, list.writer(), opts, Tup{} });
324 _ = &tupargs;320 try writeReplacementString(writer, v.raw, opts.escaped, list.items);
325 var args = .{321 return;
326 alloc,
327 list.writer(),
328 opts,
329 tupargs,
330 };
331 inline for (v.args, 0..) |arg, i| {
332 const field_name = comptime nio.fmt.comptimePrint("{d}", .{i});
333 @field(args[3], field_name) = try resolveArg(arg, alloc, data, ctx, opts);
334 }322 }
335 try @call(.auto, func, args);323 comptime var types: [v.args.len]type = @splat(void);
324 inline for (v.args, &types) |arg, *T| T.* = ResolveArg(arg, @TypeOf(data), @TypeOf(ctx));
325 var args: std.meta.Tuple(&types) = undefined;
326 inline for (v.args, 0..) |arg, i| args[i] = try resolveArg(arg, alloc, data, ctx, opts);
327 try @call(.auto, func, .{ alloc, list.writer(), opts, args });
336 try writeReplacementString(writer, v.raw, opts.escaped, list.items);328 try writeReplacementString(writer, v.raw, opts.escaped, list.items);
337 return;329 return;
338 }330 }