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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use crate::{
    error,
    options::TaskOptions,
    response,
    utils::{value_into_vec, PushExt},
    Callbacks, Client, InnerClient, Result,
};
use serde::Serialize;
use serde_json::{json, to_value, Map, Value};
use snafu::prelude::*;

/// The parameter `how` in `changePosition`.
///
/// <https://aria2.github.io/manual/en/html/aria2c.html#aria2.changePosition>
#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
pub enum PositionHow {
    #[serde(rename = "POS_SET")]
    Set,
    #[serde(rename = "POS_CUR")]
    Cur,
    #[serde(rename = "POS_END")]
    End,
}

impl InnerClient {
    async fn custom_tell_multi(
        &self,
        method: &str,
        offset: i32,
        num: i32,
        keys: Option<Vec<String>>,
    ) -> Result<Vec<Map<String, Value>>> {
        let mut params = value_into_vec(json!([offset, num]));
        params.push_some(keys)?;
        self.call_and_wait(method, params).await
    }

    pub async fn get_version(&self) -> Result<response::Version> {
        self.call_and_wait("getVersion", vec![]).await
    }

    async fn returning_gid(&self, method: &str, gid: &str) -> Result<()> {
        self.call_and_wait::<String>(method, vec![Value::String(gid.to_string())])
            .await?;
        Ok(())
    }

    pub async fn remove(&self, gid: &str) -> Result<()> {
        self.returning_gid("remove", gid).await
    }

    pub async fn force_remove(&self, gid: &str) -> Result<()> {
        self.returning_gid("forceRemove", gid).await
    }

    pub async fn pause(&self, gid: &str) -> Result<()> {
        self.returning_gid("pause", gid).await
    }

    pub async fn pause_all(&self) -> Result<()> {
        self.call_and_wait::<String>("pauseAll", vec![]).await?;
        Ok(())
    }

    pub async fn force_pause(&self, gid: &str) -> Result<()> {
        self.returning_gid("forcePause", gid).await
    }

    pub async fn force_pause_all(&self) -> Result<()> {
        self.call_and_wait::<String>("forcePauseAll", vec![])
            .await?;
        Ok(())
    }

    pub async fn unpause(&self, gid: &str) -> Result<()> {
        self.returning_gid("unpause", gid).await
    }

    pub async fn unpause_all(&self) -> Result<()> {
        self.call_and_wait::<String>("unpauseAll", vec![]).await?;
        Ok(())
    }

    pub async fn custom_tell_status(
        &self,
        gid: &str,
        keys: Option<Vec<String>>,
    ) -> Result<Map<String, Value>> {
        let mut params = vec![Value::String(gid.to_string())];
        params.push_some(keys)?;
        self.call_and_wait("tellStatus", params).await
    }

    pub async fn tell_status(&self, gid: &str) -> Result<response::Status> {
        self.call_and_wait("tellStatus", vec![Value::String(gid.to_string())])
            .await
    }

    pub async fn get_uris(&self, gid: &str) -> Result<Vec<response::Uri>> {
        self.call_and_wait("getUris", vec![Value::String(gid.to_string())])
            .await
    }

    pub async fn get_files(&self, gid: &str) -> Result<Vec<response::File>> {
        self.call_and_wait("getFiles", vec![Value::String(gid.to_string())])
            .await
    }

    pub async fn get_peers(&self, gid: &str) -> Result<Vec<response::Peer>> {
        self.call_and_wait("getPeers", vec![Value::String(gid.to_string())])
            .await
    }

    pub async fn get_servers(&self, gid: &str) -> Result<Vec<response::GetServersResult>> {
        self.call_and_wait("getServers", vec![Value::String(gid.to_string())])
            .await
    }

    pub async fn tell_active(&self) -> Result<Vec<response::Status>> {
        self.call_and_wait("tellActive", vec![]).await
    }

    pub async fn tell_waiting(&self, offset: i32, num: i32) -> Result<Vec<response::Status>> {
        self.call_and_wait("tellWaiting", value_into_vec(json!([offset, num])))
            .await
    }

    pub async fn tell_stopped(&self, offset: i32, num: i32) -> Result<Vec<response::Status>> {
        self.call_and_wait("tellStopped", value_into_vec(json!([offset, num])))
            .await
    }

    pub async fn custom_tell_active(
        &self,
        keys: Option<Vec<String>>,
    ) -> Result<Vec<Map<String, Value>>> {
        let mut params = Vec::new();
        params.push_some(keys)?;
        self.call_and_wait("tellActive", params).await
    }

    pub async fn custom_tell_waiting(
        &self,
        offset: i32,
        num: i32,
        keys: Option<Vec<String>>,
    ) -> Result<Vec<Map<String, Value>>> {
        self.custom_tell_multi("tellWaiting", offset, num, keys)
            .await
    }

    pub async fn custom_tell_stopped(
        &self,
        offset: i32,
        num: i32,
        keys: Option<Vec<String>>,
    ) -> Result<Vec<Map<String, Value>>> {
        self.custom_tell_multi("tellStopped", offset, num, keys)
            .await
    }

    pub async fn change_position(&self, gid: &str, pos: i32, how: PositionHow) -> Result<i32> {
        let params = value_into_vec(json!([gid, pos, how]));
        self.call_and_wait("changePosition", params).await
    }

    /// # Returns
    /// This method returns a list which contains two integers.
    ///
    /// The first integer is the number of URIs deleted.
    /// The second integer is the number of URIs added.
    pub async fn change_uri(
        &self,
        gid: &str,
        file_index: i32,
        del_uris: Vec<String>,
        add_uris: Vec<String>,
        position: Option<i32>,
    ) -> Result<(i32, i32)> {
        let mut params = value_into_vec(json!([gid, file_index, del_uris, add_uris]));
        params.push_some(position)?;
        self.call_and_wait("changeUri", params).await
    }

    pub async fn get_option(&self, gid: &str) -> Result<TaskOptions> {
        self.call_and_wait("getOption", vec![Value::String(gid.to_string())])
            .await
    }

    pub async fn change_option(&self, gid: &str, options: TaskOptions) -> Result<()> {
        self.call_and_wait(
            "changeOption",
            vec![
                Value::String(gid.to_string()),
                to_value(options).context(error::JsonSnafu)?,
            ],
        )
        .await
    }

    pub async fn get_global_option(&self) -> Result<TaskOptions> {
        self.call_and_wait("getGlobalOption", vec![]).await
    }

    pub async fn change_global_option(&self, options: TaskOptions) -> Result<()> {
        self.call_and_wait(
            "changeGlobalOption",
            vec![to_value(options).context(error::JsonSnafu)?],
        )
        .await
    }

    pub async fn get_global_stat(&self) -> Result<response::GlobalStat> {
        self.call_and_wait("getGlobalStat", vec![]).await
    }

    pub async fn purge_download_result(&self) -> Result<()> {
        self.call_and_wait::<String>("purgeDownloadResult", vec![])
            .await?;
        Ok(())
    }

    pub async fn remove_download_result(&self, gid: &str) -> Result<()> {
        self.call_and_wait::<String>("removeDownloadResult", vec![Value::String(gid.to_string())])
            .await?;
        Ok(())
    }

    pub async fn get_session_info(&self) -> Result<response::SessionInfo> {
        self.call_and_wait("getSessionInfo", vec![]).await
    }

    pub async fn shutdown(&self) -> Result<()> {
        self.call_and_wait::<String>("shutdown", vec![]).await?;
        Ok(())
    }

    pub async fn force_shutdown(&self) -> Result<()> {
        self.call_and_wait::<String>("forceShutdown", vec![])
            .await?;
        Ok(())
    }

    pub async fn save_session(&self) -> Result<()> {
        self.call_and_wait::<String>("saveSession", vec![]).await?;
        Ok(())
    }
}

impl Client {
    async fn add_callbacks_option(&self, gid: &str, callbacks: Option<Callbacks>) {
        if let Some(callbacks) = callbacks {
            self.add_callbacks(gid.to_string(), callbacks).await;
        }
    }

    pub async fn add_uri(
        &self,
        uris: Vec<String>,
        options: Option<TaskOptions>,
        position: Option<u32>,
        callbacks: Option<Callbacks>,
    ) -> Result<String> {
        let mut params = vec![to_value(uris).context(error::JsonSnafu)?];
        params.push_else(options, json!({}))?;
        params.push_some(position)?;

        let gid: String = self.call_and_wait("addUri", params).await?;
        self.add_callbacks_option(&gid, callbacks).await;
        Ok(gid)
    }

    pub async fn add_torrent(
        &self,
        torrent: impl AsRef<[u8]>,
        uris: Option<Vec<String>>,
        options: Option<TaskOptions>,
        position: Option<u32>,
        callbacks: Option<Callbacks>,
    ) -> Result<String> {
        let mut params = vec![Value::String(base64::encode(torrent))];
        params.push_else(uris, json!([]))?;
        params.push_else(options, json!({}))?;
        params.push_some(position)?;

        let gid: String = self.call_and_wait("addTorrent", params).await?;
        self.add_callbacks_option(&gid, callbacks).await;
        Ok(gid)
    }

    pub async fn add_metalink(
        &self,
        metalink: impl AsRef<[u8]>,
        options: Option<TaskOptions>,
        position: Option<u32>,
        callbacks: Option<Callbacks>,
    ) -> Result<String> {
        let mut params = vec![Value::String(base64::encode(metalink))];
        params.push_else(options, json!({}))?;
        params.push_some(position)?;

        let gid: String = self.call_and_wait("addMetalink", params).await?;
        self.add_callbacks_option(&gid, callbacks).await;
        Ok(gid)
    }
}