| ... | ... | @@ -127,17 +127,15 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val |
| 127 | 127 | const TI = @typeInfo(TO); |
| 128 | 128 | |
| 129 | 129 | if (comptime extras.isZigString(TO)) { |
| 130 | | return writeReplacementString(writer, repl.raw, opts.escaped, x); |
| 130 | if (repl.raw) return writer.writeAll(x); |
| 131 | return writeReplacementString(writer, opts.escaped, x); |
| 131 | 132 | } |
| 132 | 133 | if (TI == .int or TI == .float or TI == .comptime_int or TI == .comptime_float) { |
| 133 | 134 | try writer.print("{d}", .{x}); |
| 134 | 135 | return; |
| 135 | 136 | } |
| 136 | 137 | if (comptime isArrayOf(u8)(TO)) { |
| 137 | | if (repl.raw) { |
| 138 | | try writer.writeAll(&x); |
| 139 | | return; |
| 140 | | } |
| 138 | if (repl.raw) return writer.writeAll(&x); |
| 141 | 139 | const s = std.mem.trim(u8, &x, "\n"); |
| 142 | 140 | if (opts.escaped) try writeEscaped(s, writer); |
| 143 | 141 | if (!opts.escaped) try writer.writeAll(s); |
| ... | ... | @@ -157,7 +155,8 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val |
| 157 | 155 | } |
| 158 | 156 | if (TI == .optional) { |
| 159 | 157 | if (comptime extras.isZigString(std.meta.Child(TO))) { |
| 160 | | return writeReplacementString(writer, repl.raw, opts.escaped, x.?); |
| 158 | if (repl.raw) return writer.writeAll(x.?); |
| 159 | return writeReplacementString(writer, opts.escaped, x.?); |
| 161 | 160 | } |
| 162 | 161 | } |
| 163 | 162 | return x.nprint(writer); |
| ... | ... | @@ -310,27 +309,23 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val |
| 310 | 309 | var args: std.meta.Tuple(&types) = undefined; |
| 311 | 310 | inline for (v.args, 0..) |arg, i| args[i] = try resolveArg(arg, alloc, data, ctx, opts); |
| 312 | 311 | try @call(.auto, func, .{ alloc, &list } ++ args); |
| 313 | | try writeReplacementString(writer, v.raw, opts.escaped, list.items); |
| 312 | try writeReplacementString(writer, opts.escaped, list.items); |
| 314 | 313 | return; |
| 315 | 314 | } |
| 316 | 315 | if (v.raw) { |
| 317 | 316 | if (@hasDecl(opts.Ctx, "pek_" ++ v.name)) @compileError("pek: attempted to call safe custom function: '" ++ v.name ++ "' but did not use '{" ++ v.name ++ "}'"); |
| 318 | 317 | const func = @field(opts.Ctx, "pek__" ++ v.name); |
| 319 | | var list: nio.AllocatingWriter = .init(alloc); |
| 320 | | defer list.deinit(); |
| 321 | 318 | const Tup = @typeInfo(@TypeOf(func)).@"fn".params[3].type.?; |
| 322 | 319 | if (@typeInfo(Tup).@"struct".fields.len == 0) { |
| 323 | 320 | // edge case branch because 'struct {}' is counted as a non-tuple |
| 324 | | try @call(.auto, func, .{ alloc, &list, opts, Tup{} }); |
| 325 | | try writeReplacementString(writer, v.raw, opts.escaped, list.items); |
| 321 | try @call(.auto, func, .{ alloc, writer, opts, Tup{} }); |
| 326 | 322 | return; |
| 327 | 323 | } |
| 328 | 324 | comptime var types: [v.args.len]type = @splat(void); |
| 329 | 325 | inline for (v.args, &types) |arg, *T| T.* = ResolveArg(arg, @TypeOf(data), @TypeOf(ctx)); |
| 330 | 326 | var args: std.meta.Tuple(&types) = undefined; |
| 331 | 327 | inline for (v.args, 0..) |arg, i| args[i] = try resolveArg(arg, alloc, data, ctx, opts); |
| 332 | | try @call(.auto, func, .{ alloc, &list, opts, args }); |
| 333 | | try writeReplacementString(writer, v.raw, opts.escaped, list.items); |
| 328 | try @call(.auto, func, .{ alloc, writer, opts, args }); |
| 334 | 329 | return; |
| 335 | 330 | } |
| 336 | 331 | comptime unreachable; |
| ... | ... | @@ -423,11 +418,7 @@ pub fn writeEscaped(s: string, writer: anytype) !void { |
| 423 | 418 | } |
| 424 | 419 | } |
| 425 | 420 | |
| 426 | | fn writeReplacementString(writer: anytype, raw: bool, escaped: bool, bytes: []const u8) !void { |
| 427 | | if (raw) { |
| 428 | | try writer.writeAll(bytes); |
| 429 | | return; |
| 430 | | } |
| 421 | fn writeReplacementString(writer: anytype, escaped: bool, bytes: []const u8) !void { |
| 431 | 422 | const s = std.mem.trim(u8, bytes, "\n"); |
| 432 | 423 | if (escaped) try writeEscaped(s, writer); |
| 433 | 424 | if (!escaped) try writer.writeAll(s); |