summaryrefslogtreecommitdiff
path: root/src/request.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/request.zig
parentb02b7f71978a172848322f0671d580e425634916 (diff)
downloadzmission-f743e05e207046073999ee7234a157359d6a4f57.tar.gz
Diffstat (limited to 'src/request.zig')
-rw-r--r--src/request.zig389
1 files changed, 206 insertions, 183 deletions
diff --git a/src/request.zig b/src/request.zig
index 214f997..a306baf 100644
--- a/src/request.zig
+++ b/src/request.zig
@@ -6,64 +6,62 @@ pub const Request = @This();
pub const Method = enum {
// Torrent requests: Torrent action
// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md#31-torrent-action-requests
- torrent_start,
- torrent_start_now,
- torrent_stop,
- torrent_verify,
- torrent_reannounce,
+ @"torrent-start",
+ @"torrent-start-now",
+ @"torrent-stop",
+ @"torrent-verify",
+ @"torrent-reannounce",
// Torrent requests: Mutator
// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md#32-torrent-mutator-torrent-set
- torrent_set,
+ @"torrent-set",
// Torrent requests: Torrent accessor
// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md#33-torrent-accessor-torrent-get
- torrent_get,
+ @"torrent-get",
// Torrent requests: Torrent adding
// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md#34-adding-a-torrent
- torrent_add,
+ @"torrent-add",
// Torrent requests: Torrent removing
// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md#35-removing-a-torrent
- torrent_remove,
+ @"torrent-remove",
// Torrent requests: Torrent moving
// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md#36-moving-a-torrent
- torrent_set_location,
+ @"torrent-set-location",
// Torrent requests: Torrent moving
// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md#37-renaming-a-torrents-path
- torrent_rename_path,
+ @"torrent-rename-path",
// Session requests: Get
// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md#4--session-requests
- session_get,
+ @"session-get",
// Session requests: Mutator
// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md#411-mutators
- session_set,
- //session_stats,
+ @"session-set",
- const Self = @This();
+ // TODO
+ //@"session-stats",
- pub const json_map = util.JsonMap(Self, util.replaceUnderscores);
-
- pub fn jsonStringify(self: Self, options: std.json.StringifyOptions, out_stream: anytype) !void {
- try util.jsonStringify(self, options, out_stream);
+ pub fn jsonStringify(self: Method, options: std.json.StringifyOptions, out_stream: anytype) !void {
+ try std.json.stringify(@tagName(self), options, out_stream);
}
};
pub const TorrentIDs = union(enum) {
single: i32,
many: []const i32,
- recently_active,
+ @"recently-active",
pub fn jsonStringify(self: TorrentIDs, options: std.json.StringifyOptions, out_stream: anytype) !void {
switch (self) {
.single => |v| try std.json.stringify(v, options, out_stream),
.many => |v| try std.json.stringify(v, options, out_stream),
- .recently_active => try std.json.stringify("recently-active", options, out_stream),
+ .@"recently-active" => try std.json.stringify("recently-active", options, out_stream),
}
}
};
@@ -75,6 +73,34 @@ pub const TorrentStop = TorrentStart;
pub const TorrentVerify = TorrentStart;
pub const TorrentReannounce = TorrentStart;
+pub const TorrentAdd = struct {
+
+ // path to download the torrent to
+ @"download-dir": ?[]const u8 = null,
+
+ filename: ?[]const u8 = null,
+
+ labels: ?[][]const u8 = null,
+
+ // base64-encoded .torrent content
+ metainfo: ?[]const u8 = null,
+
+ // if true, don't start the torrent
+ paused: ?bool = null,
+
+ // TODO:
+ // maximum number of peers
+ //@"peer-limit": ?i32 = null,
+ //cookies string pointer to a string of one or more cookies.
+ //bandwidthPriority number torrent's bandwidth tr_priority_t
+ //files-wanted array indices of file(s) to download
+ //files-unwanted array indices of file(s) to not download
+ //priority-high array indices of high-priority file(s)
+ //priority-low array indices of low-priority file(s)
+ //priority-normal array indices of normal-priority file(s)
+
+};
+
pub const TorrentGet = struct {
pub const Fields = enum {
activityDate,
@@ -96,7 +122,7 @@ pub const TorrentGet = struct {
errorString,
eta,
etaIdle,
- file_count,
+ @"file-count",
files,
fileStats,
group,
@@ -115,7 +141,7 @@ pub const TorrentGet = struct {
maxConnectedPeers,
metadataPercentComplete,
name,
- peer_limit,
+ @"peer-limit",
peers,
peersConnected,
peersFrom,
@@ -127,7 +153,7 @@ pub const TorrentGet = struct {
pieceCount,
pieceSize,
priorities,
- primary_mime_type,
+ @"primary-mime-type",
queuePosition,
rateDownload,
rateUpload,
@@ -155,12 +181,8 @@ pub const TorrentGet = struct {
webseeds,
webseedsSendingToUs,
- const Self = @This();
-
- pub const json_map = util.JsonMap(Self, util.replaceUnderscores);
-
- pub fn jsonStringify(self: Self, options: std.json.StringifyOptions, out_stream: anytype) !void {
- try util.jsonStringify(self, options, out_stream);
+ pub fn jsonStringify(self: @This(), options: std.json.StringifyOptions, out_stream: anytype) !void {
+ try std.json.stringify(@tagName(self), options, out_stream);
}
};
@@ -172,68 +194,64 @@ pub const TorrentGet = struct {
pub const SessionGet = struct {
pub const Fields = enum {
- alt_speed_down,
- alt_speed_enabled,
- alt_speed_time_begin,
- alt_speed_time_day,
- alt_speed_time_enabled,
- alt_speed_time_end,
- alt_speed_up,
- blocklist_enabled,
- blocklist_size,
- blocklist_url,
- cache_size_mb,
- config_dir,
- default_trackers,
- dht_enabled,
- download_dir,
- download_dir_free_space,
- download_queue_enabled,
- download_queue_size,
+ @"alt-speed-down",
+ @"alt-speed-enabled",
+ @"alt-speed-time-begin",
+ @"alt-speed-time-day",
+ @"alt-speed-time-enabled",
+ @"alt-speed-time-end",
+ @"alt-speed-up",
+ @"blocklist-enabled",
+ @"blocklist-size",
+ @"blocklist-url",
+ @"cache-size-mb",
+ @"config-dir",
+ @"default-trackers",
+ @"dht-enabled",
+ @"download-dir",
+ @"download-dir-free-space",
+ @"download-queue-enabled",
+ @"download-queue-size",
encryption,
- idle_seeding_limit_enabled,
- idle_seeding_limit,
- incomplete_dir_enabled,
- incomplete_dir,
- lpd_enabled,
- peer_limit_global,
- peer_limit_per_torrent,
- peer_port_random_on_start,
- peer_port,
- pex_enabled,
- port_forwarding_enabled,
- queue_stalled_enabled,
- queue_stalled_minutes,
- rename_partial_files,
- rpc_version_minimum,
- rpc_version_semver,
- rpc_version,
- script_torrent_added_enabled,
- script_torrent_added_filename,
- script_torrent_done_enabled,
- script_torrent_done_filename,
- script_torrent_done_seeding_enabled,
- script_torrent_done_seeding_filename,
- seed_queue_enabled,
- seed_queue_size,
+ @"idle-seeding-limit-enabled",
+ @"idle-seeding-limit",
+ @"incomplete-dir-enabled",
+ @"incomplete-dir",
+ @"lpd-enabled",
+ @"peer-limit-global",
+ @"peer-limit-per-torrent",
+ @"peer-port-random-on-start",
+ @"peer-port",
+ @"pex-enabled",
+ @"port-forwarding-enabled",
+ @"queue-stalled-enabled",
+ @"queue-stalled-minutes",
+ @"rename-partial-files",
+ @"rpc-version-minimum",
+ @"rpc-version-semver",
+ @"rpc-version",
+ @"script-torrent-added-enabled",
+ @"script-torrent-added-filename",
+ @"script-torrent-done-enabled",
+ @"script-torrent-done-filename",
+ @"script-torrent-done-seeding-enabled",
+ @"script-torrent-done-seeding-filename",
+ @"seed-queue-enabled",
+ @"seed-queue-size",
seedRatioLimit,
seedRatioLimited,
- speed_limit_down_enabled,
- speed_limit_down,
- speed_limit_up_enabled,
- speed_limit_up,
- start_added_torrents,
- trash_original_torrent_files,
+ @"speed-limit-down-enabled",
+ @"speed-limit-down",
+ @"speed-limit-up-enabled",
+ @"speed-limit-up",
+ @"start-added-torrents",
+ @"trash-original-torrent-files",
units,
- utp_enabled,
+ @"utp-enabled",
version,
- const Self = @This();
-
- pub const json_map = util.JsonMap(Self, util.replaceUnderscores);
-
- pub fn jsonStringify(self: Self, options: std.json.StringifyOptions, out_stream: anytype) !void {
- try util.jsonStringify(self, options, out_stream);
+ pub fn jsonStringify(self: @This(), options: std.json.StringifyOptions, out_stream: anytype) !void {
+ try std.json.stringify(@tagName(self), options, out_stream);
}
};
@@ -243,92 +261,82 @@ pub const SessionGet = struct {
};
pub const SessionSet = struct {
- alt_speed_down: ?usize = null,
- alt_speed_enabled: ?bool = null,
- alt_speed_time_begin: ?usize = null,
- alt_speed_time_day: ?usize = null,
- alt_speed_time_enabled: ?bool = null,
- alt_speed_time_end: ?usize = null,
- alt_speed_up: ?usize = null,
- blocklist_enabled: ?bool = null,
- blocklist_url: ?[]u8 = null,
- cache_size_mb: ?usize = null,
- default_trackers: ?[]u8 = null,
- dht_enabled: ?bool = null,
- download_dir: ?[]u8 = null,
- download_dir_free_space: ?usize = null,
- download_queue_enabled: ?bool = null,
- download_queue_size: ?usize = null,
+ @"alt-speed-down": ?usize = null,
+ @"alt-speed-enabled": ?bool = null,
+ @"alt-speed-time-begin": ?usize = null,
+ @"alt-speed-time-day": ?usize = null,
+ @"alt-speed-time-enabled": ?bool = null,
+ @"alt-speed-time-end": ?usize = null,
+ @"alt-speed-up": ?usize = null,
+ @"blocklist-enabled": ?bool = null,
+ @"blocklist-url": ?[]u8 = null,
+ @"cache-size-mb": ?usize = null,
+ @"default-trackers": ?[]u8 = null,
+ @"dht-enabled": ?bool = null,
+ @"download-dir": ?[]u8 = null,
+ @"download-dir-free-space": ?usize = null,
+ @"download-queue-enabled": ?bool = null,
+ @"download-queue-size": ?usize = null,
encryption: ?enum {
required,
preferred,
tolerated,
- const Self = @This();
-
- pub const json_map = util.JsonMap(Self, util.replaceUnderscores);
-
- pub fn jsonStringify(self: Self, options: std.json.StringifyOptions, out_stream: anytype) !void {
- try util.jsonStringify(self, options, out_stream);
+ pub fn jsonStringify(self: @This(), options: std.json.StringifyOptions, out_stream: anytype) !void {
+ try std.json.stringify(@tagName(self), options, out_stream);
}
} = null,
- idle_seeding_limit_enabled: ?bool = null,
- idle_seeding_limit: ?usize = null,
- incomplete_dir_enabled: ?bool = null,
- incomplete_dir: ?[]u8 = null,
- lpd_enabled: ?bool = null,
- peer_limit_global: ?usize = null,
- peer_limit_per_torrent: ?usize = null,
- peer_port_random_on_start: ?bool = null,
- peer_port: ?usize = null,
- pex_enabled: ?bool = null,
- port_forwarding_enabled: ?bool = null,
- queue_stalled_enabled: ?bool = null,
- queue_stalled_minutes: ?usize = null,
- rename_partial_files: ?bool = null,
- script_torrent_added_enabled: ?bool = null,
- script_torrent_added_filename: ?[]u8 = null,
- script_torrent_done_enabled: ?bool = null,
- script_torrent_done_filename: ?[]u8 = null,
- script_torrent_done_seeding_enabled: ?bool = null,
- script_torrent_done_seeding_filename: ?[]u8 = null,
- seed_queue_enabled: ?bool = null,
- seed_queue_size: ?usize = null,
+ @"idle-seeding-limit-enabled": ?bool = null,
+ @"idle-seeding-limit": ?usize = null,
+ @"incomplete-dir-enabled": ?bool = null,
+ @"incomplete-dir": ?[]u8 = null,
+ @"lpd-enabled": ?bool = null,
+ @"peer-limit-global": ?usize = null,
+ @"peer-limit-per-torrent": ?usize = null,
+ @"peer-port-random-on-start": ?bool = null,
+ @"peer-port": ?usize = null,
+ @"pex-enabled": ?bool = null,
+ @"port-forwarding-enabled": ?bool = null,
+ @"queue-stalled-enabled": ?bool = null,
+ @"queue-stalled-minutes": ?usize = null,
+ @"rename-partial-files": ?bool = null,
+ @"script-torrent-added-enabled": ?bool = null,
+ @"script-torrent-added-filename": ?[]u8 = null,
+ @"script-torrent-done-enabled": ?bool = null,
+ @"script-torrent-done-filename": ?[]u8 = null,
+ @"script-torrent-done-seeding-enabled": ?bool = null,
+ @"script-torrent-done-seeding-filename": ?[]u8 = null,
+ @"seed-queue-enabled": ?bool = null,
+ @"seed-queue-size": ?usize = null,
seedRatioLimit: ?f64 = null,
seedRatioLimited: ?bool = null,
- speed_limit_down_enabled: ?bool = null,
- speed_limit_down: ?usize = null,
- speed_limit_up_enabled: ?bool = null,
- speed_limit_up: ?usize = null,
- start_added_torrents: ?bool = null,
- trash_original_torrent_files: ?bool = null,
- utp_enabled: ?bool = null,
-
- pub const json_map = util.JsonMap(SessionSet, util.replaceUnderscores);
-
- pub fn jsonStringify(self: SessionSet, options: std.json.StringifyOptions, out_stream: anytype) !void {
- try util.jsonStringify(self, options, out_stream);
- }
+ @"speed-limit-down-enabled": ?bool = null,
+ @"speed-limit-down": ?usize = null,
+ @"speed-limit-up-enabled": ?bool = null,
+ @"speed-limit-up": ?usize = null,
+ @"start-added-torrents": ?bool = null,
+ @"trash-original-torrent-files": ?bool = null,
+ @"utp-enabled": ?bool = null,
};
pub const Object = struct {
method: Method,
arguments: union(Method) {
- torrent_start: TorrentStart,
- torrent_start_now: TorrentStartNow,
- torrent_stop: TorrentStop,
- torrent_verify: TorrentVerify,
- torrent_reannounce: TorrentReannounce,
-
- torrent_set: u8,
- torrent_get: TorrentGet,
- torrent_add: u8,
- torrent_remove: u8,
- torrent_set_location: u8,
- torrent_rename_path: u8,
-
- session_get: SessionGet,
- session_set: SessionSet,
+ @"torrent-start": TorrentStart,
+ @"torrent-start-now": TorrentStartNow,
+ @"torrent-stop": TorrentStop,
+ @"torrent-verify": TorrentVerify,
+ @"torrent-reannounce": TorrentReannounce,
+
+ @"torrent-set": u8,
+ @"torrent-get": TorrentGet,
+ @"torrent-add": TorrentAdd,
+ @"torrent-remove": u8,
+ @"torrent-set-location": u8,
+ @"torrent-rename-path": u8,
+
+ @"session-get": SessionGet,
+ @"session-set": SessionSet,
const Self = @This();
@@ -338,18 +346,18 @@ pub const Object = struct {
};
switch (self) {
- .torrent_start,
- .torrent_start_now,
- .torrent_stop,
- .torrent_verify,
- .torrent_reannounce,
+ .@"torrent-start",
+ .@"torrent-start-now",
+ .@"torrent-stop",
+ .@"torrent-verify",
+ .@"torrent-reannounce",
=> |v| try std.json.stringify(v, options, out_stream),
- .torrent_get => |v| try std.json.stringify(v, options, out_stream),
+ .@"torrent-get" => |v| try std.json.stringify(v, options, out_stream),
+ .@"torrent-add" => |v| try std.json.stringify(v, options, out_stream),
- .session_set => |v| try std.json.stringify(v, options, out_stream),
-
- .session_get => |v| try std.json.stringify(v, options, out_stream),
+ .@"session-set" => |v| try std.json.stringify(v, options, out_stream),
+ .@"session-get" => |v| try std.json.stringify(v, options, out_stream),
else => unreachable,
}
}
@@ -377,14 +385,29 @@ test "json request encoding" {
const test_cases = [_]test_case{
.{
+ .name = "torrent-add",
+ .request = .{
+ .method = .@"torrent-add",
+ .arguments = .{
+ .@"torrent-add" = .{
+ .filename = "foobar",
+ },
+ },
+ },
+ .expected =
+ \\{"method":"torrent-add","arguments":{"filename":"foobar"}}
+ ,
+ },
+
+ .{
.name = "session-get",
.request = .{
- .method = .session_get,
+ .method = .@"session-get",
.arguments = .{
- .session_get = .{
+ .@"session-get" = .{
.fields = &[_]SessionGet.Fields{
.version,
- .utp_enabled,
+ .@"utp-enabled",
},
},
},
@@ -397,10 +420,10 @@ test "json request encoding" {
.{
.name = "session-set",
.request = .{
- .method = .session_set,
+ .method = .@"session-set",
.arguments = .{
- .session_set = .{
- .lpd_enabled = true,
+ .@"session-set" = .{
+ .@"lpd-enabled" = true,
.encryption = .required,
},
},
@@ -413,9 +436,9 @@ test "json request encoding" {
.{
.name = "torrent-reannounce single id",
.request = .{
- .method = .torrent_reannounce,
+ .method = .@"torrent-reannounce",
.arguments = .{
- .torrent_reannounce = .{
+ .@"torrent-reannounce" = .{
.ids = .{ .single = 1 },
},
},
@@ -428,9 +451,9 @@ test "json request encoding" {
.{
.name = "torrent-reannounce multiple id",
.request = .{
- .method = .torrent_reannounce,
+ .method = .@"torrent-reannounce",
.arguments = .{
- .torrent_reannounce = .{
+ .@"torrent-reannounce" = .{
.ids = .{ .many = &[_]i32{ 1, 2 } },
},
},
@@ -443,10 +466,10 @@ test "json request encoding" {
.{
.name = "torrent-reannounce recently-active",
.request = .{
- .method = .torrent_reannounce,
+ .method = .@"torrent-reannounce",
.arguments = .{
- .torrent_reannounce = .{
- .ids = .recently_active,
+ .@"torrent-reannounce" = .{
+ .ids = .@"recently-active",
},
},
},
@@ -458,14 +481,14 @@ test "json request encoding" {
.{
.name = "torrent-get",
.request = .{
- .method = .torrent_get,
+ .method = .@"torrent-get",
.arguments = .{
- .torrent_get = .{
+ .@"torrent-get" = .{
.fields = &[_]TorrentGet.Fields{
.id,
.name,
},
- .ids = .recently_active,
+ .ids = .@"recently-active",
},
},
},