From 490eb400283285022aa458a711a54478ca5f5a1f Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Thu, 18 Apr 2024 13:18:16 +0200 Subject: [PATCH] Add method cdp function Signed-off-by: Francis Bouvier --- src/cdp/cdp.zig | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index def8e2ff..2d2ae708 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -52,11 +52,11 @@ pub fn do( const id = try std.fmt.parseUnsigned(u64, (try scanner.next()).number, 10); try checkKey("method", (try scanner.next()).string); - const method = (try scanner.next()).string; + const method_name = (try scanner.next()).string; - std.log.debug("cmd: id {any}, method {s}", .{ id, method }); + std.log.debug("cmd: id {any}, method {s}", .{ id, method_name }); - var iter = std.mem.splitScalar(u8, method, '.'); + var iter = std.mem.splitScalar(u8, method_name, '.'); const domain = std.meta.stringToEnum(Domains, iter.first()) orelse return error.UnknonwDomain; @@ -121,6 +121,24 @@ pub fn result( return stringify(alloc, resp); } +// caller owns the slice returned +pub fn method( + alloc: std.mem.Allocator, + name: []const u8, + comptime T: type, + params: T, + sessionID: ?[]const u8, +) ![]const u8 { + const Resp = struct { + method: []const u8, + params: T, + sessionId: ?[]const u8, + }; + const resp = Resp{ .method = name, .params = params, .sessionId = sessionID }; + + return stringify(alloc, resp); +} + pub fn getParams( alloc: std.mem.Allocator, comptime T: type,