Add stringify function in cdp

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-04-15 21:33:32 +02:00
parent e908cb0ec4
commit ffbfd36502

View File

@@ -49,6 +49,18 @@ fn checkKey(key: []const u8, token: []const u8) !void {
const resultNull = "{{\"id\": {d}, \"result\": {{}}}}"; const resultNull = "{{\"id\": {d}, \"result\": {{}}}}";
// caller owns the slice returned
pub fn stringify(alloc: std.mem.Allocator, res: anytype) ![]const u8 {
var out = std.ArrayList(u8).init(alloc);
defer out.deinit();
try std.json.stringify(res, .{}, out.writer());
const ret = try alloc.alloc(u8, out.items.len);
@memcpy(ret, out.items);
return ret;
}
// caller owns the slice returned
pub fn result( pub fn result(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
id: u64, id: u64,
@@ -63,13 +75,7 @@ pub fn result(
}; };
const resp = Resp{ .id = id, .result = res }; const resp = Resp{ .id = id, .result = res };
var out = std.ArrayList(u8).init(alloc); return stringify(alloc, resp);
defer out.deinit();
try std.json.stringify(resp, .{}, out.writer());
const ret = try alloc.alloc(u8, out.items.len);
@memcpy(ret, out.items);
return ret;
} }
pub fn getParams( pub fn getParams(