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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use anyhow::{bail, ensure};
use chrono::{DateTime, Utc};
use reqwest::{header::IF_NONE_MATCH, Client, StatusCode};
use serde_derive::{Deserialize, Serialize};

use super::{Result, BASE_URL};

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Tasks {
    /// Type of the resource. This is always "tasks#tasks".
    pub kind: String,

    /// ETag of the resource.
    pub etag: String,

    /// Token used to access the next page of this result.
    pub next_page_token: Option<String>,

    /// Collection of tasks.
    pub items: Option<Vec<Task>>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Task {
    /// Type of the resource. This is always "tasks#task".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,

    /// Task identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// ETag of the resource.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub etag: Option<String>,

    /// Title of the task.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// Last modification time of the task (as a RFC 3339 timestamp).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated: Option<DateTime<Utc>>,

    /// URL pointing to this task. Used to retrieve, update, or delete this task.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub self_link: Option<String>,

    // Parent task identifier.
    // This field is omitted if it is a top-level task.
    // This field is read-only. Use the "move" method to move the task under a different parent or to the top level.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<String>,

    /// String indicating the position of the task among its sibling tasks under the same parent task or at the top level.
    /// If this string is greater than another task's corresponding position string according to lexicographical ordering,
    /// the task is positioned after the other task under the same parent task (or at the top level).
    ///
    /// This field is read-only.
    /// Use the "move" method to move the task to another position.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub position: Option<String>,

    /// Notes describing the task. Optional.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,

    /// Status of the task. This is either "needsAction" or "completed".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<TaskStatus>,

    /// Due date of the task (as a RFC 3339 timestamp). Optional.
    /// The due date only records date information; the time portion of the timestamp is discarded when setting the due date.
    /// It isn't possible to read or write the time that a task is due via the API.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub due: Option<DateTime<Utc>>,

    /// Completion date of the task (as a RFC 3339 timestamp).
    /// This field is omitted if the task has not been completed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completed: Option<DateTime<Utc>>,

    /// Flag indicating whether the task has been deleted. The default if False.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deleted: Option<bool>,

    /// Flag indicating whether the task is hidden.
    /// This is the case if the task had been marked completed when the task list was last cleared.
    /// The default is False. This field is read-only.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hidden: Option<bool>,

    /// Collection of links. This collection is read-only.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub links: Option<Vec<TaskLink>>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct TaskLink {
    #[serde(rename = "type")]
    /// Type of the link, e.g. "email".
    pub task_type: String,

    /// The description. In HTML speak: Everything between <a> and </a>.
    pub description: String,

    /// The URL.
    pub link: String,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum TaskStatus {
    NeedsAction,
    Completed,
}

#[derive(Serialize, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ListOptions {
    /// Upper bound for a task's completion date (as a RFC 3339 timestamp) to filter by.
    /// Optional. The default is not to filter by completion date.
    pub completed_max: Option<String>,

    /// Lower bound for a task's completion date (as a RFC 3339 timestamp) to filter by.
    /// Optional. The default is not to filter by completion date.
    pub completed_min: Option<String>,

    /// Upper bound for a task's due date (as a RFC 3339 timestamp) to filter by.
    /// Optional. The default is not to filter by due date.
    pub due_max: Option<DateTime<Utc>>,

    /// Lower bound for a task's due date (as a RFC 3339 timestamp) to filter by.
    /// Optional. The default is not to filter by due date.
    pub due_min: Option<DateTime<Utc>>,

    /// Maximum number of task lists returned on one page.
    /// Optional. The default is 20 (max allowed: 100).
    pub max_results: Option<u64>,

    /// Token specifying the result page to return. Optional.
    pub page_token: Option<String>,

    /// Flag indicating whether completed tasks are returned in the result.
    /// Optional. The default is True.
    pub show_completed: Option<bool>,

    /// Flag indicating whether deleted tasks are returned in the result.
    /// Optional. The default is False.
    pub show_deleted: Option<bool>,

    /// Flag indicating whether hidden tasks are returned in the result.
    /// Optional. The default is False.
    pub show_hidden: Option<bool>,

    /// Lower bound for a task's last modification time (as a RFC 3339 timestamp) to filter by.
    /// Optional. The default is not to filter by last modification time.
    pub updated_min: Option<DateTime<Utc>>,
}

// Returns all tasks in the specified task list.
pub fn list(
    client: &Client,
    tasklist_id: &str,
    opt: Option<ListOptions>,
    etag: Option<String>,
) -> Result<Option<Tasks>> {
    let url = format!(
        "{base_url}/lists/{tasklist_id}/tasks",
        base_url = BASE_URL,
        tasklist_id = tasklist_id
    );
    let mut builder = client.get(url.as_str());

    if let Some(if_none_match) = etag {
        builder = builder.header(IF_NONE_MATCH, if_none_match);
    }

    if let Some(q_opt) = opt {
        builder = builder.query(&q_opt);
    }

    let mut resp = builder.send()?;

    if resp.status() == StatusCode::NOT_MODIFIED {
        Ok(None)
    } else {
        ensure!(resp.status().is_success(), resp.text()?);
        Ok(Some(resp.json::<Tasks>()?))
    }
}

// Returns the specified task.
pub fn get(
    client: &Client,
    tasklist_id: &str,
    task_id: &str,
    etag: Option<String>,
) -> Result<Option<Task>> {
    let url = format!(
        "{base_url}/lists/{tasklist_id}/tasks/{task_id}",
        base_url = BASE_URL,
        tasklist_id = tasklist_id,
        task_id = task_id
    );

    let mut builder = client.get(url.as_str());
    if let Some(if_none_match) = etag {
        builder = builder.header(IF_NONE_MATCH, if_none_match);
    }

    let mut resp = builder.send()?;

    if resp.status() == StatusCode::NOT_MODIFIED {
        Ok(None)
    } else {
        ensure!(resp.status().is_success(), resp.text()?);
        Ok(Some(resp.json::<Task>()?))
    }
}

#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct InsertOptions {
    /// Parent task identifier.
    /// If the task is created at the top level, this parameter is omitted. Optional.
    pub parent: Option<String>,

    /// Previous sibling task identifier.
    /// If the task is created at the first position among its siblings, this parameter is omitted. Optional.
    pub previous: Option<String>,
}

// Creates a new task on the specified task list.
pub fn insert(
    client: &Client,
    tasklist_id: &str,
    v: Task,
    opts: Option<InsertOptions>,
) -> Result<Task> {
    let url = format!(
        "{base_url}/lists/{tasklist_id}/tasks",
        base_url = BASE_URL,
        tasklist_id = tasklist_id,
    );

    let mut builder = client.post(url.as_str()).body(serde_json::to_vec(&v)?);

    if let Some(query_params) = opts {
        builder = builder.query(&query_params);
    }

    let mut resp = builder.send()?;

    ensure!(resp.status().is_success(), resp.text()?);
    Ok(resp.json::<Task>()?)
}

// Updates the specified task.
pub fn update(client: &Client, tasklist_id: &str, mut v: Task) -> Result<Task> {
    let task_id = match v.id.as_ref() {
        Some(id) => id,
        None => bail!("id can not be none"),
    };

    let url = format!(
        "{base_url}/lists/{tasklist_id}/tasks/{task_id}",
        base_url = BASE_URL,
        tasklist_id = tasklist_id,
        task_id = task_id.as_str()
    );

    v.updated = None;

    let mut resp = client
        .put(url.as_str())
        .body(serde_json::to_vec(&v)?)
        .send()?;

    ensure!(resp.status().is_success(), resp.text()?);
    Ok(resp.json::<Task>()?)
}

// Deletes the specified task from the task list.
pub fn delete(client: &Client, tasklist_id: &str, task_id: &str) -> Result<()> {
    let url = format!(
        "{base_url}/lists/{tasklist_id}/tasks/{task_id}",
        base_url = BASE_URL,
        tasklist_id = tasklist_id,
        task_id = task_id,
    );

    let mut resp = client.delete(url.as_str()).send()?;

    ensure!(resp.status().is_success(), resp.text()?);
    Ok(())
}

// Clears all completed tasks from the specified task list.
// The affected tasks will be marked as 'hidden' and no longer be returned by default when retrieving all tasks for a task list.
pub fn clear(client: &Client, tasklist_id: &str) -> Result<()> {
    let url = format!(
        "{base_url}/lists/{tasklist_id}/clear",
        base_url = BASE_URL,
        tasklist_id = tasklist_id,
    );

    let mut resp = client.post(url.as_str()).send()?;
    ensure!(resp.status().is_success(), resp.text()?);
    Ok(())
}

// Moves the specified task to another position in the task list.
// This can include putting it as a child task under a new parent and/or move it to a different position among its sibling tasks.
pub fn move_task(
    client: &Client,
    tasklist_id: &str,
    task_id: &str,
    opts: InsertOptions,
) -> Result<Task> {
    let url = format!(
        "{base_url}/lists/{tasklist_id}/tasks/{task_id}/move",
        base_url = BASE_URL,
        tasklist_id = tasklist_id,
        task_id = task_id
    );

    let mut resp = client.post(url.as_str()).query(&opts).send()?;

    ensure!(resp.status().is_success(), resp.text()?);
    Ok(resp.json::<Task>()?)
}

// Updates the specified task. This method supports patch semantics.
pub fn patch(client: &Client, tasklist_id: &str, task_id: &str, v: Task) -> Result<Task> {
    let url = format!(
        "{base_url}/lists/{tasklist_id}/tasks/{task_id}",
        base_url = BASE_URL,
        tasklist_id = tasklist_id,
        task_id = task_id
    );

    let mut resp = client
        .patch(url.as_str())
        .body(serde_json::to_vec(&v)?)
        .send()?;

    ensure!(resp.status().is_success(), resp.text()?);
    Ok(resp.json::<Task>()?)
}