summaryrefslogtreecommitdiff
path: root/src/response.zig
diff options
context:
space:
mode:
authorChristian Segundo2023-07-24 10:28:58 +0200
committerChristian Segundo2023-07-24 10:28:58 +0200
commitf743e05e207046073999ee7234a157359d6a4f57 (patch)
tree0ceb01ffdcdc3a094e488c28e80c80d89f8309c5 /src/response.zig
parentb02b7f71978a172848322f0671d580e425634916 (diff)
downloadzmission-master.tar.gz
Diffstat (limited to 'src/response.zig')
-rw-r--r--src/response.zig93
1 files changed, 62 insertions, 31 deletions
diff --git a/src/response.zig b/src/response.zig
index 8964de4..a370133 100644
--- a/src/response.zig
+++ b/src/response.zig
@@ -4,44 +4,75 @@ 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: Arguments,
+ arguments: @This().Arguments,
};
-pub const Object = struct {
- pub const Arguments = union(Request.Method) {
- torrent_start,
- torrent_start_now,
- torrent_stop,
- torrent_verify,
- torrent_reannounce,
- torrent_set,
- torrent_get: TorrentGet.Arguments,
- torrent_add,
- torrent_remove,
- torrent_set_location,
- torrent_rename_path,
- session_get,
- session_set,
+pub const TorrentAdd = struct {
+ pub const Arguments = struct {
+ @"torrent-added": ?struct {
+ hashString: []const u8,
+ id: u64,
+ name: []const u8,
+ } = null,
};
-
result: []const u8,
- arguments: Arguments,
-
- pub fn init(arguments: Arguments, result: []const u8) Object {
- switch (arguments) {
- .torrent_get => {
- return Object{
- .result = result,
- .arguments = arguments,
- };
- },
- else => unreachable,
- }
- }
+ arguments: @This().Arguments,
};