Skip to content

Commit f02de77

Browse files
Add getContent
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 9974b56 commit f02de77

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/cdp/cdp.zig

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,54 @@ pub fn getSessionID(scanner: *std.json.Scanner) !?[]const u8 {
186186
return (try scanner.next()).string;
187187
}
188188

189+
pub fn getContent(
190+
alloc: std.mem.Allocator,
191+
comptime T: type,
192+
scanner: *std.json.Scanner,
193+
) !struct { params: T, sessionID: ?[]const u8 } {
194+
195+
// if next token is the end of the object, error
196+
const t = try scanner.next();
197+
if (t == .object_end) return error.CDPNoContent;
198+
199+
var params: T = undefined;
200+
var sessionID: ?[]const u8 = null;
201+
202+
var n = t.string;
203+
204+
// params
205+
if (std.mem.eql(u8, n, "params")) {
206+
if (T == void) {
207+
208+
// ignore empty params
209+
_ = (try scanner.next()).object_begin;
210+
_ = (try scanner.next()).object_end;
211+
n = (try scanner.next()).string;
212+
params = void{};
213+
} else {
214+
215+
// parse "params"
216+
const options = std.json.ParseOptions{
217+
.max_value_len = scanner.input.len,
218+
.allocate = .alloc_if_needed,
219+
};
220+
params = try std.json.innerParse(T, alloc, scanner, options);
221+
}
222+
} else {
223+
params = switch (@typeInfo(T)) {
224+
.Void => void{},
225+
.Optional => null,
226+
else => return error.CDPNoParams,
227+
};
228+
}
229+
230+
if (std.mem.eql(u8, n, "sessionId")) {
231+
sessionID = (try scanner.next()).string;
232+
}
233+
234+
return .{ .params = params, .sessionID = sessionID };
235+
}
236+
189237
// Common
190238
// ------
191239

0 commit comments

Comments
 (0)