summaryrefslogtreecommitdiff
path: root/src/request.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/request.zig')
-rw-r--r--src/request.zig421
1 files changed, 203 insertions, 218 deletions
diff --git a/src/request.zig b/src/request.zig
index 8641776..2633428 100644
--- a/src/request.zig
+++ b/src/request.zig
@@ -45,21 +45,204 @@ const Method = enum {
const Self = @This();
- // Transmission RPC uses hyphens instead of underscores, this creates a
- // table of method names using the same enum field names but with
- // underscores.
- pub const Fields = util.enumFieldsToStringSlice(Self);
+ pub const json_map = util.JsonMap(Self, util.replaceUnderscores);
- pub fn str(self: Self) []const u8 {
- return Fields[@enumToInt(self)];
+ pub fn jsonStringify(self: Self, options: std.json.StringifyOptions, out_stream: anytype) !void {
+ try util.jsonStringify(self, options, out_stream);
}
+};
+
+pub const TorrentIDs = union(enum) {
+ single: usize,
+ many: []const usize,
+ recently_active,
+
+ const Self = @This();
pub fn jsonStringify(self: Self, options: std.json.StringifyOptions, out_stream: anytype) !void {
- try std.json.stringify(self.str(), options, out_stream);
+ 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),
+ }
}
};
-pub const SessionSetFields = struct {
+// all equal just to we can switch on them at the same time
+pub const TorrentStart = struct { ids: TorrentIDs };
+pub const TorrentStartNow = TorrentStart;
+pub const TorrentStop = TorrentStart;
+pub const TorrentVerify = TorrentStart;
+pub const TorrentReannounce = TorrentStart;
+
+pub const TorrentGet = struct {
+ pub const Fields = enum {
+ activityDate,
+ addedDate,
+ availability,
+ bandwidthPriority,
+ comment,
+ corruptEver,
+ creator,
+ dateCreated,
+ desiredAvailable,
+ doneDate,
+ downloadDir,
+ downloadedEver,
+ downloadLimit,
+ downloadLimited,
+ editDate,
+ @"error",
+ errorString,
+ eta,
+ etaIdle,
+ file_count,
+ files,
+ fileStats,
+ group,
+ hashString,
+ haveUnchecked,
+ haveValid,
+ honorsSessionLimits,
+ id,
+ isFinished,
+ isPrivate,
+ isStalled,
+ labels,
+ leftUntilDone,
+ magnetLink,
+ manualAnnounceTime,
+ maxConnectedPeers,
+ metadataPercentComplete,
+ name,
+ peer_limit,
+ peers,
+ peersConnected,
+ peersFrom,
+ peersGettingFromUs,
+ peersSendingToUs,
+ percentComplete,
+ percentDone,
+ pieces,
+ pieceCount,
+ pieceSize,
+ priorities,
+ primary_mime_type,
+ queuePosition,
+ rateDownload,
+ rateUpload,
+ recheckProgress,
+ secondsDownloading,
+ secondsSeeding,
+ seedIdleLimit,
+ seedIdleMode,
+ seedRatioLimit,
+ seedRatioMode,
+ sequentialDownload,
+ sizeWhenDone,
+ startDate,
+ status,
+ trackers,
+ trackerList,
+ trackerStats,
+ totalSize,
+ torrentFile,
+ uploadedEver,
+ uploadLimit,
+ uploadLimited,
+ uploadRatio,
+ wanted,
+ 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 const all_fields = util.enumFieldsSlice(@This().Fields);
+
+ ids: ?TorrentIDs = null,
+ fields: []const Fields,
+};
+
+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,
+ 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,
+ seedRatioLimit,
+ seedRatioLimited,
+ speed_limit_down_enabled,
+ speed_limit_down,
+ speed_limit_up_enabled,
+ speed_limit_up,
+ start_added_torrents,
+ trash_original_torrent_files,
+ units,
+ 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 const all_fields = util.enumFieldsSlice(@This().Fields);
+
+ fields: []const Fields,
+};
+
+pub const SessionSet = struct {
alt_speed_down: ?usize = null,
alt_speed_enabled: ?bool = null,
alt_speed_time_begin: ?usize = null,
@@ -83,14 +266,10 @@ pub const SessionSetFields = struct {
const Self = @This();
- pub const Fields = util.enumFieldsToStringSlice(Self);
-
- pub fn str(self: Self) []const u8 {
- return Fields[@enumToInt(self)];
- }
+ pub const json_map = util.JsonMap(Self, util.replaceUnderscores);
pub fn jsonStringify(self: Self, options: std.json.StringifyOptions, out_stream: anytype) !void {
- try std.json.stringify(self.str(), options, out_stream);
+ try util.jsonStringify(self, options, out_stream);
}
} = null,
idle_seeding_limit_enabled: ?bool = null,
@@ -124,211 +303,14 @@ pub const SessionSetFields = struct {
start_added_torrents: ?bool = null,
trash_original_torrent_files: ?bool = null,
utp_enabled: ?bool = null,
-};
-pub const TorrentGetFields = enum {
- activityDate,
- addedDate,
- availability,
- bandwidthPriority,
- comment,
- corruptEver,
- creator,
- dateCreated,
- desiredAvailable,
- doneDate,
- downloadDir,
- downloadedEver,
- downloadLimit,
- downloadLimited,
- editDate,
- @"error",
- errorString,
- eta,
- etaIdle,
- file_count,
- files,
- fileStats,
- group,
- hashString,
- haveUnchecked,
- haveValid,
- honorsSessionLimits,
- id,
- isFinished,
- isPrivate,
- isStalled,
- labels,
- leftUntilDone,
- magnetLink,
- manualAnnounceTime,
- maxConnectedPeers,
- metadataPercentComplete,
- name,
- peer_limit,
- peers,
- peersConnected,
- peersFrom,
- peersGettingFromUs,
- peersSendingToUs,
- percentComplete,
- percentDone,
- pieces,
- pieceCount,
- pieceSize,
- priorities,
- primary_mime_type,
- queuePosition,
- rateDownload,
- rateUpload,
- recheckProgress,
- secondsDownloading,
- secondsSeeding,
- seedIdleLimit,
- seedIdleMode,
- seedRatioLimit,
- seedRatioMode,
- sequentialDownload,
- sizeWhenDone,
- startDate,
- status,
- trackers,
- trackerList,
- trackerStats,
- totalSize,
- torrentFile,
- uploadedEver,
- uploadLimit,
- uploadLimited,
- uploadRatio,
- wanted,
- webseeds,
- webseedsSendingToUs,
+ pub const json_map = util.JsonMap(SessionSet, util.replaceUnderscores);
- const Self = @This();
-
- pub const Fields = util.enumFieldsToStringSlice(Self);
-
- pub fn str(self: Self) []const u8 {
- return Fields[@enumToInt(self)];
- }
-
- // We have to add our own custom stringification because the default
- // one will nest the enum inside the method name.
- pub fn jsonStringify(self: Self, options: std.json.StringifyOptions, out_stream: anytype) !void {
- try std.json.stringify(self.str(), options, out_stream);
- }
-};
-
-pub const SessionGetFields = 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,
- 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,
- seedRatioLimit,
- seedRatioLimited,
- speed_limit_down_enabled,
- speed_limit_down,
- speed_limit_up_enabled,
- speed_limit_up,
- start_added_torrents,
- trash_original_torrent_files,
- units,
- utp_enabled,
- version,
-
- const Self = @This();
-
- pub const Fields = util.enumFieldsToStringSlice(Self);
-
- pub fn str(self: Self) []const u8 {
- return Fields[@enumToInt(self)];
- }
-
- // We have to add our own custom stringification because the default
- // one will nest the enum inside the method name.
- pub fn jsonStringify(self: Self, options: std.json.StringifyOptions, out_stream: anytype) !void {
- try std.json.stringify(self.str(), options, out_stream);
+ pub fn jsonStringify(self: SessionSet, options: std.json.StringifyOptions, out_stream: anytype) !void {
+ try util.jsonStringify(self, options, out_stream);
}
};
-pub const TorrentIDs = union(enum) {
- single: usize,
- many: []const usize,
- recently_active,
-
- const Self = @This();
-
- pub fn jsonStringify(self: Self, 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),
- }
- }
-};
-
-pub const TorrentActionFields = struct {
- ids: TorrentIDs,
-};
-
-pub const TorrentStart = TorrentActionFields;
-pub const TorrentStartNow = TorrentActionFields;
-pub const TorrentStop = TorrentActionFields;
-pub const TorrentVerify = TorrentActionFields;
-pub const TorrentReannounce = TorrentActionFields;
-
-pub const TorrentGet = struct {
- ids: ?TorrentIDs = null,
- fields: []const TorrentGetFields,
-};
-
-pub const SessionGet = struct {
- fields: []const SessionGetFields,
-};
-
-pub const SessionSet = SessionSetFields;
-
pub const Request = struct {
method: Method,
arguments: union(Method) {
@@ -385,7 +367,10 @@ test "json request encoding" {
defer req.deinit();
try std.json.stringify(self.request, .{}, req.writer());
std.testing.expect(std.mem.eql(u8, req.items, self.expected)) catch {
- std.debug.panic("{s}\n", .{req.items});
+ std.debug.print("name: {s}\n", .{self.name});
+ std.debug.print("expected: \t{s}\n", .{self.expected});
+ std.debug.print("got: \t\t{s}\n", .{req.items});
+ return error.InvalidOutput;
};
}
};
@@ -397,7 +382,7 @@ test "json request encoding" {
.method = .session_get,
.arguments = .{
.session_get = .{
- .fields = &[_]SessionGetFields{
+ .fields = &[_]SessionGet.Fields{
.version,
.utp_enabled,
},
@@ -421,7 +406,7 @@ test "json request encoding" {
},
},
.expected =
- \\{"method":"session-set","arguments":{"encryption":"required","lpd_enabled":true}}
+ \\{"method":"session-set","arguments":{"encryption":"required","lpd-enabled":true}}
,
},
@@ -476,7 +461,7 @@ test "json request encoding" {
.method = .torrent_get,
.arguments = .{
.torrent_get = .{
- .fields = &[_]TorrentGetFields{
+ .fields = &[_]TorrentGet.Fields{
.id,
.name,
},