refactor: add command.fmtbuf

This is useful for recursive command calling to avoid overwriting the
global command arguments buffer.
This commit is contained in:
CJ van den Berg 2026-01-20 16:13:40 +01:00
parent f34285d2aa
commit a7d4fc5729
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -18,11 +18,20 @@ pub const Context = struct {
cbor.writeValue(&context_buffer.writer, value) catch @panic("command.Context.fmt failed"); cbor.writeValue(&context_buffer.writer, value) catch @panic("command.Context.fmt failed");
return .{ .args = .{ .buf = context_buffer.written() } }; return .{ .args = .{ .buf = context_buffer.written() } };
} }
fn fmtbuf(buf: []u8, value: anytype) error{CommandContextBufferNoSpaceLeft}!Context {
var writer: std.Io.Writer = .fixed(buf);
cbor.writeValue(&writer, value) catch |e| return switch (e) {
error.WriteFailed => error.CommandContextBufferNoSpaceLeft,
};
return .{ .args = .{ .buf = writer.buffered() } };
}
}; };
const context_buffer_allocator = std.heap.c_allocator; const context_buffer_allocator = std.heap.c_allocator;
threadlocal var context_buffer: std.Io.Writer.Allocating = .init(context_buffer_allocator); threadlocal var context_buffer: std.Io.Writer.Allocating = .init(context_buffer_allocator);
pub const fmt = Context.fmt; pub const fmt = Context.fmt;
pub const fmtbuf = Context.fmtbuf;
const Vtable = struct { const Vtable = struct {
id: ID = ID_unknown, id: ID = ID_unknown,