todoist-api-rs 0.1.4

Todoist API client library
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Task model for the Todoist API.
//!
//! This module defines the Task struct and related types that represent
//! tasks from the Todoist API v1/REST v2.

use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};

use super::common::{Deadline, Due, Duration};

#[cfg(test)]
use super::common::DurationUnit;

/// A task in Todoist.
///
/// Tasks are the core entity in Todoist, representing items that can be
/// completed, assigned, scheduled, and organized into projects and sections.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Task {
    /// The unique identifier for the task.
    pub id: String,

    /// The text content of the task.
    pub content: String,

    /// A detailed description of the task (supports Markdown).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// The ID of the project the task belongs to.
    pub project_id: String,

    /// The ID of the section the task belongs to (if any).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub section_id: Option<String>,

    /// The ID of the parent task (if this is a subtask).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<String>,

    /// The order of the task within its parent (project, section, or parent task).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub order: Option<i32>,

    /// Labels attached to the task.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub labels: Vec<String>,

    /// Task priority from 1 (normal) to 4 (urgent).
    /// Note: In the API, 1 is normal and 4 is urgent (opposite of UI display).
    #[serde(default = "default_priority")]
    pub priority: i32,

    /// The due date/time information for the task.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub due: Option<Due>,

    /// The deadline for the task (separate from due date).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub deadline: Option<Deadline>,

    /// The estimated duration for the task.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration: Option<Duration>,

    /// Whether the task is completed.
    #[serde(default)]
    pub is_completed: bool,

    /// The URL to view the task in Todoist.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,

    /// The number of comments on the task.
    #[serde(default)]
    pub comment_count: i32,

    /// When the task was created.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created_at: Option<DateTime<Utc>>,

    /// The ID of the user who created the task.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub creator_id: Option<String>,

    /// The ID of the user the task is assigned to.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub assignee_id: Option<String>,

    /// The ID of the user who assigned the task.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub assigner_id: Option<String>,
}

fn default_priority() -> i32 {
    1
}

impl Task {
    /// Returns true if the task has a due date set.
    pub fn has_due_date(&self) -> bool {
        self.due.is_some()
    }

    /// Returns true if the task is a subtask (has a parent).
    pub fn is_subtask(&self) -> bool {
        self.parent_id.is_some()
    }

    /// Returns true if the task is recurring.
    pub fn is_recurring(&self) -> bool {
        self.due.as_ref().is_some_and(|d| d.is_recurring)
    }

    /// Returns the due date as a NaiveDate if set.
    pub fn due_date(&self) -> Option<NaiveDate> {
        self.due
            .as_ref()
            .and_then(|d| NaiveDate::parse_from_str(&d.date, "%Y-%m-%d").ok())
    }

    /// Returns true if this is a high priority task (priority 3 or 4).
    pub fn is_high_priority(&self) -> bool {
        self.priority >= 3
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Datelike;

    #[test]
    fn test_task_deserialize_minimal() {
        let json = r#"{
            "id": "123",
            "content": "Buy milk",
            "project_id": "456"
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        assert_eq!(task.id, "123");
        assert_eq!(task.content, "Buy milk");
        assert_eq!(task.project_id, "456");
        assert_eq!(task.priority, 1);
        assert!(!task.is_completed);
        assert!(task.labels.is_empty());
    }

    #[test]
    fn test_task_deserialize_full() {
        let json = r#"{
            "id": "123",
            "content": "Buy milk",
            "description": "From the store",
            "project_id": "456",
            "section_id": "789",
            "parent_id": null,
            "order": 1,
            "labels": ["shopping", "urgent"],
            "priority": 4,
            "due": {
                "date": "2026-01-25",
                "datetime": "2026-01-25T15:00:00Z",
                "is_recurring": false,
                "string": "Jan 25 at 3pm",
                "timezone": "America/New_York"
            },
            "is_completed": false,
            "url": "https://todoist.com/app/task/123",
            "comment_count": 2,
            "created_at": "2026-01-20T10:00:00Z",
            "creator_id": "user1",
            "assignee_id": "user2",
            "assigner_id": "user1"
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        assert_eq!(task.id, "123");
        assert_eq!(task.content, "Buy milk");
        assert_eq!(task.description, Some("From the store".to_string()));
        assert_eq!(task.section_id, Some("789".to_string()));
        assert_eq!(task.priority, 4);
        assert!(task.has_due_date());
        assert!(task.is_high_priority());

        let due = task.due.as_ref().unwrap();
        assert_eq!(due.date, "2026-01-25");
        assert!(due.has_time());
        assert!(!due.is_recurring);
    }

    #[test]
    fn test_task_serialize() {
        let task = Task {
            id: "123".to_string(),
            content: "Test task".to_string(),
            description: None,
            project_id: "456".to_string(),
            section_id: None,
            parent_id: None,
            order: None,
            labels: vec![],
            priority: 1,
            due: None,
            deadline: None,
            duration: None,
            is_completed: false,
            url: None,
            comment_count: 0,
            created_at: None,
            creator_id: None,
            assignee_id: None,
            assigner_id: None,
        };

        let json = serde_json::to_string(&task).unwrap();
        assert!(json.contains("\"id\":\"123\""));
        assert!(json.contains("\"content\":\"Test task\""));
        // Optional None fields should be skipped
        assert!(!json.contains("description"));
        assert!(!json.contains("section_id"));
    }

    #[test]
    fn test_task_is_subtask() {
        let mut task = Task {
            id: "123".to_string(),
            content: "Test".to_string(),
            description: None,
            project_id: "456".to_string(),
            section_id: None,
            parent_id: None,
            order: None,
            labels: vec![],
            priority: 1,
            due: None,
            deadline: None,
            duration: None,
            is_completed: false,
            url: None,
            comment_count: 0,
            created_at: None,
            creator_id: None,
            assignee_id: None,
            assigner_id: None,
        };

        assert!(!task.is_subtask());
        task.parent_id = Some("parent123".to_string());
        assert!(task.is_subtask());
    }

    #[test]
    fn test_task_is_recurring() {
        let task_no_due = Task {
            id: "123".to_string(),
            content: "Test".to_string(),
            description: None,
            project_id: "456".to_string(),
            section_id: None,
            parent_id: None,
            order: None,
            labels: vec![],
            priority: 1,
            due: None,
            deadline: None,
            duration: None,
            is_completed: false,
            url: None,
            comment_count: 0,
            created_at: None,
            creator_id: None,
            assignee_id: None,
            assigner_id: None,
        };
        assert!(!task_no_due.is_recurring());

        let task_recurring = Task {
            due: Some(Due {
                date: "2026-01-25".to_string(),
                datetime: None,
                is_recurring: true,
                string: Some("every day".to_string()),
                timezone: None,
                lang: None,
            }),
            ..task_no_due.clone()
        };
        assert!(task_recurring.is_recurring());
    }

    #[test]
    fn test_task_due_date() {
        let task = Task {
            id: "123".to_string(),
            content: "Test".to_string(),
            description: None,
            project_id: "456".to_string(),
            section_id: None,
            parent_id: None,
            order: None,
            labels: vec![],
            priority: 1,
            due: Some(Due::from_date("2026-01-25")),
            deadline: None,
            duration: None,
            is_completed: false,
            url: None,
            comment_count: 0,
            created_at: None,
            creator_id: None,
            assignee_id: None,
            assigner_id: None,
        };

        let due_date = task.due_date().unwrap();
        assert_eq!(due_date.year(), 2026);
        assert_eq!(due_date.month(), 1);
        assert_eq!(due_date.day(), 25);
    }

    #[test]
    fn test_due_from_date() {
        let due = Due::from_date("2026-01-25");
        assert_eq!(due.date, "2026-01-25");
        assert!(!due.is_recurring);
        assert!(!due.has_time());
    }

    #[test]
    fn test_due_from_datetime() {
        let due = Due::from_datetime("2026-01-25", "2026-01-25T15:00:00Z");
        assert_eq!(due.date, "2026-01-25");
        assert_eq!(due.datetime, Some("2026-01-25T15:00:00Z".to_string()));
        assert!(due.has_time());
    }

    #[test]
    fn test_due_as_naive_date() {
        let due = Due::from_date("2026-01-25");
        let date = due.as_naive_date().unwrap();
        assert_eq!(date.year(), 2026);
        assert_eq!(date.month(), 1);
        assert_eq!(date.day(), 25);
    }

    #[test]
    fn test_deadline_deserialize() {
        let json = r#"{"date": "2026-01-30"}"#;
        let deadline: Deadline = serde_json::from_str(json).unwrap();
        assert_eq!(deadline.date, "2026-01-30");
    }

    #[test]
    fn test_duration_minutes() {
        let duration = Duration::minutes(30);
        assert_eq!(duration.amount, 30);
        assert_eq!(duration.unit, DurationUnit::Minute);
        assert_eq!(duration.as_minutes(), 30);
    }

    #[test]
    fn test_duration_days() {
        let duration = Duration::days(2);
        assert_eq!(duration.amount, 2);
        assert_eq!(duration.unit, DurationUnit::Day);
        assert_eq!(duration.as_minutes(), 2 * 24 * 60);
    }

    #[test]
    fn test_duration_unit_serialize() {
        let minute = DurationUnit::Minute;
        let day = DurationUnit::Day;

        assert_eq!(serde_json::to_string(&minute).unwrap(), "\"minute\"");
        assert_eq!(serde_json::to_string(&day).unwrap(), "\"day\"");
    }

    #[test]
    fn test_duration_unit_deserialize() {
        let minute: DurationUnit = serde_json::from_str("\"minute\"").unwrap();
        let day: DurationUnit = serde_json::from_str("\"day\"").unwrap();

        assert_eq!(minute, DurationUnit::Minute);
        assert_eq!(day, DurationUnit::Day);
    }

    #[test]
    fn test_task_with_duration() {
        let json = r#"{
            "id": "123",
            "content": "Meeting",
            "project_id": "456",
            "duration": {
                "amount": 60,
                "unit": "minute"
            }
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        let duration = task.duration.unwrap();
        assert_eq!(duration.amount, 60);
        assert_eq!(duration.unit, DurationUnit::Minute);
    }

    #[test]
    fn test_task_with_deadline() {
        let json = r#"{
            "id": "123",
            "content": "Project deadline",
            "project_id": "456",
            "deadline": {
                "date": "2026-02-15"
            }
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        let deadline = task.deadline.unwrap();
        assert_eq!(deadline.date, "2026-02-15");
    }

    #[test]
    fn test_task_priority_levels() {
        let task_normal = Task {
            id: "1".to_string(),
            content: "Normal".to_string(),
            description: None,
            project_id: "p".to_string(),
            section_id: None,
            parent_id: None,
            order: None,
            labels: vec![],
            priority: 1,
            due: None,
            deadline: None,
            duration: None,
            is_completed: false,
            url: None,
            comment_count: 0,
            created_at: None,
            creator_id: None,
            assignee_id: None,
            assigner_id: None,
        };
        assert!(!task_normal.is_high_priority());

        let task_high = Task {
            priority: 3,
            ..task_normal.clone()
        };
        assert!(task_high.is_high_priority());

        let task_urgent = Task {
            priority: 4,
            ..task_normal
        };
        assert!(task_urgent.is_high_priority());
    }

    #[test]
    fn test_task_labels_deserialization() {
        let json = r#"{
            "id": "123",
            "content": "Task with labels",
            "project_id": "456",
            "labels": ["work", "urgent", "review"]
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        assert_eq!(task.labels.len(), 3);
        assert!(task.labels.contains(&"work".to_string()));
        assert!(task.labels.contains(&"urgent".to_string()));
        assert!(task.labels.contains(&"review".to_string()));
    }
}