const std = @import("std"); const Request = @import("request.zig"); const Types = @import("types.zig"); pub const Response = @This(); pub const default_parse_options = std.json.ParseOptions{ .ignore_unknown_fields = true, .duplicate_field_behavior = .@"error", }; allocator: std.mem.Allocator, data: Data, pub const Data = union(enum) { TorrentGet: TorrentGet, TorrentAdd: TorrentAdd, }; pub fn parseBody( allocator: std.mem.Allocator, method: Request.Method, body: []const u8, parseOptions: ?std.json.ParseOptions, ) !Response { @setEvalBranchQuota(10000); switch (method) { .@"torrent-get" => { const decoded = try std.json.parseFromSlice( TorrentGet, allocator, body, parseOptions orelse default_parse_options, ); return Response{ .data = Data{ .TorrentGet = decoded }, .allocator = allocator, }; }, .@"torrent-add" => { const decoded = try std.json.parseFromSlice( TorrentAdd, allocator, body, parseOptions orelse default_parse_options, ); return Response{ .data = Data{ .TorrentAdd = decoded }, .allocator = allocator, }; }, else => unreachable, } } pub fn deinit(self: *Response) void { std.json.parseFree(@TypeOf(self.data), self.allocator, self.*.data); } pub const TorrentGet = struct { pub const Arguments = struct { torrents: ?[]Types.Torrent = null, }; result: []const u8, arguments: @This().Arguments, }; pub const TorrentAdd = struct { pub const Arguments = struct { @"torrent-added": ?struct { hashString: []const u8, id: u64, name: []const u8, } = null, }; result: []const u8, arguments: @This().Arguments, };