1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr, TimestampSeconds};

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Version {
    pub enabled_features: Vec<String>,

    pub version: String,
}

/// Full status of a task.
///
/// <https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellStatus>
#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Status {
    /// GID of the download.
    pub gid: String,

    pub status: TaskStatus,

    #[serde_as(as = "DisplayFromStr")]
    pub total_length: u64,

    #[serde_as(as = "DisplayFromStr")]
    pub completed_length: u64,

    #[serde_as(as = "DisplayFromStr")]
    pub upload_length: u64,

    /// Hexadecimal representation of the download progress.
    ///
    /// The highest bit corresponds to the piece at index 0.
    ///
    /// Any set bits indicate loaded pieces, while
    /// unset bits indicate not yet loaded and/or missing pieces.
    ///
    /// Any overflow bits at the end are set to zero.
    ///
    /// When the download was not started yet,
    /// this key will not be included in the response.
    pub bitfield: Option<String>,

    #[serde_as(as = "DisplayFromStr")]
    pub download_speed: u64,

    #[serde_as(as = "DisplayFromStr")]
    pub upload_speed: u64,

    /// InfoHash. BitTorrent only
    pub info_hash: Option<String>,

    #[serde_as(as = "Option<DisplayFromStr>")]
    pub num_seeders: Option<u64>,

    /// true if the local endpoint is a seeder. Otherwise false. BitTorrent only.
    #[serde_as(as = "Option<DisplayFromStr>")]
    pub seeder: Option<bool>,

    #[serde_as(as = "DisplayFromStr")]
    pub piece_length: u64,

    #[serde_as(as = "DisplayFromStr")]
    pub num_pieces: u64,

    #[serde_as(as = "DisplayFromStr")]
    pub connections: u64,

    pub error_code: Option<String>,

    pub error_message: Option<String>,
    /// List of GIDs which are generated as the result of this download.
    ///
    /// For example, when aria2 downloads a Metalink file,
    /// it generates downloads described in the Metalink (see the --follow-metalink option).
    ///
    /// This value is useful to track auto-generated downloads.
    ///
    /// If there are no such downloads, this key will not be included in the response.
    pub followed_by: Option<Vec<String>>,

    /// The reverse link for followedBy.
    ///
    /// A download included in followedBy has this object's GID in its following value.
    pub following: Option<String>,

    /// GID of a parent download.
    ///
    /// Some downloads are a part of another download.
    ///
    /// For example, if a file in a Metalink has BitTorrent resources,
    /// the downloads of ".torrent" files are parts of that parent.
    ///
    /// If this download has no parent, this key will not be included in the response.
    pub belongs_to: Option<String>,

    pub dir: String,

    pub files: Vec<File>,

    pub bittorrent: Option<BittorrentStatus>,

    /// The number of verified number of bytes while the files are being hash checked.
    ///
    /// This key exists only when this download is being hash checked.
    #[serde_as(as = "Option<DisplayFromStr>")]
    pub verified_length: Option<u64>,

    /// `true` if this download is waiting for the hash check in a queue.
    ///
    /// This key exists only when this download is in the queue.
    #[serde_as(as = "Option<DisplayFromStr>")]
    pub verify_integrity_pending: Option<bool>,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct BittorrentStatus {
    pub announce_list: Vec<Vec<String>>,

    pub comment: Option<String>,

    #[serde_as(as = "Option<TimestampSeconds<i64>>")]
    pub creation_date: Option<DateTime<Utc>>,

    pub mode: Option<BitTorrentFileMode>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum BitTorrentFileMode {
    Single,
    Multi,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct File {
    #[serde_as(as = "DisplayFromStr")]
    pub index: u64,

    pub path: String,

    #[serde_as(as = "DisplayFromStr")]
    pub length: u64,

    #[serde_as(as = "DisplayFromStr")]
    pub completed_length: u64,

    #[serde_as(as = "DisplayFromStr")]
    pub selected: bool,

    pub uris: Vec<Uri>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Uri {
    pub status: UriStatus,

    pub uri: String,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum UriStatus {
    Used,
    Waiting,
}

/// Task status returned by `aria2.tellStatus`.
///
/// `Active` for currently downloading/seeding downloads.
///
/// `Waiting` for downloads in the queue; download is not started.
///
/// `Paused` for paused downloads.
///
/// `Error` for downloads that were stopped because of error.
///
/// `Complete` for stopped and completed downloads.
///
/// `Removed` for the downloads removed by user.
///
/// <https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellStatus>
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum TaskStatus {
    Active,
    Waiting,
    Paused,
    Error,
    Complete,
    Removed,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Peer {
    #[serde_as(as = "DisplayFromStr")]
    pub am_choking: bool,

    pub bitfield: String,

    #[serde_as(as = "DisplayFromStr")]
    pub download_speed: u64,

    pub ip: String,

    #[serde_as(as = "DisplayFromStr")]
    pub peer_choking: bool,

    pub peer_id: String,

    #[serde_as(as = "DisplayFromStr")]
    pub port: u16,

    #[serde_as(as = "DisplayFromStr")]
    pub seeder: bool,

    #[serde_as(as = "DisplayFromStr")]
    pub upload_speed: u64,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct GlobalStat {
    #[serde_as(as = "DisplayFromStr")]
    pub download_speed: u64,

    #[serde_as(as = "DisplayFromStr")]
    pub upload_speed: u64,

    #[serde_as(as = "DisplayFromStr")]
    pub num_active: i32,

    #[serde_as(as = "DisplayFromStr")]
    pub num_waiting: i32,

    #[serde_as(as = "DisplayFromStr")]
    pub num_stopped: i32,

    #[serde_as(as = "DisplayFromStr")]
    pub num_stopped_total: i32,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct SessionInfo {
    pub session_id: String,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct GetServersResult {
    #[serde_as(as = "DisplayFromStr")]
    pub index: i32,

    pub servers: Vec<Server>,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Server {
    pub uri: String,

    pub current_uri: String,

    #[serde_as(as = "DisplayFromStr")]
    pub download_speed: u64,
}