ticktickrs 0.1.4

A CLI Tool for TickTick tasks
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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use super::{ChecklistItem, ChecklistItemRequest, Priority, Status};

/// Task model matching TickTick API format
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Task {
    pub id: String,
    pub project_id: String,
    pub title: String,
    #[serde(default)]
    pub is_all_day: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completed_time: Option<DateTime<Utc>>,
    #[serde(default)]
    pub content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub due_date: Option<DateTime<Utc>>,
    #[serde(default)]
    pub items: Vec<ChecklistItem>,
    #[serde(default)]
    pub priority: Priority,
    #[serde(default)]
    pub reminders: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repeat_flag: Option<String>,
    #[serde(default)]
    pub sort_order: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_date: Option<DateTime<Utc>>,
    #[serde(default)]
    pub status: Status,
    #[serde(default)]
    pub time_zone: String,
    #[serde(default)]
    pub tags: Vec<String>,
}

impl Task {
    /// Check if the task is complete
    pub fn is_complete(&self) -> bool {
        self.status.is_complete()
    }
}

/// Builder for creating new [`Task`] instances with a fluent API.
///
/// Use this builder when you need to construct a task programmatically
/// with optional fields. The builder provides sensible defaults for
/// all optional fields.
///
/// # Required Fields
///
/// - `project_id` - The project to add the task to
/// - `title` - The task title
///
/// # Example
///
/// ```
/// use ticktickrs::models::task::TaskBuilder;
/// use ticktickrs::models::{Priority, ChecklistItemRequest};
///
/// let task = TaskBuilder::new("proj123", "Complete documentation")
///     .content("Add doc comments to all public APIs")
///     .priority(Priority::High)
///     .tags(vec!["work".to_string(), "docs".to_string()])
///     .build();
///
/// assert_eq!(task.title, "Complete documentation");
/// assert_eq!(task.priority, Priority::High);
///
/// // Create a task with subtasks
/// let task_with_subtasks = TaskBuilder::new("proj123", "Pack for trip")
///     .items(vec![
///         ChecklistItemRequest::new("Passport"),
///         ChecklistItemRequest::new("Clothes"),
///     ])
///     .build();
/// ```
#[derive(Default)]
#[allow(dead_code)] // Available for external use; tested in tests
pub struct TaskBuilder {
    project_id: String,
    title: String,
    is_all_day: bool,
    content: Option<String>,
    due_date: Option<DateTime<Utc>>,
    priority: Priority,
    start_date: Option<DateTime<Utc>>,
    time_zone: Option<String>,
    tags: Vec<String>,
    items: Vec<ChecklistItemRequest>,
}

#[allow(dead_code)] // Builder methods available for external use; tested
impl TaskBuilder {
    /// Create a new TaskBuilder with required fields.
    ///
    /// # Arguments
    ///
    /// * `project_id` - The ID of the project to add the task to
    /// * `title` - The task title
    pub fn new(project_id: impl Into<String>, title: impl Into<String>) -> Self {
        Self {
            project_id: project_id.into(),
            title: title.into(),
            ..Default::default()
        }
    }

    /// Set the task description/notes.
    pub fn content(mut self, content: impl Into<String>) -> Self {
        self.content = Some(content.into());
        self
    }

    /// Set the task priority level.
    pub fn priority(mut self, priority: Priority) -> Self {
        self.priority = priority;
        self
    }

    /// Set the task due date.
    pub fn due_date(mut self, due: DateTime<Utc>) -> Self {
        self.due_date = Some(due);
        self
    }

    /// Set the task start date.
    pub fn start_date(mut self, start: DateTime<Utc>) -> Self {
        self.start_date = Some(start);
        self
    }

    /// Set whether this is an all-day task (no specific time).
    pub fn all_day(mut self, is_all_day: bool) -> Self {
        self.is_all_day = is_all_day;
        self
    }

    /// Set the task timezone (IANA format, e.g., "America/New_York").
    pub fn time_zone(mut self, tz: impl Into<String>) -> Self {
        self.time_zone = Some(tz.into());
        self
    }

    /// Set the task tags.
    pub fn tags(mut self, tags: Vec<String>) -> Self {
        self.tags = tags;
        self
    }

    /// Set the task's subtasks/checklist items.
    ///
    /// # Example
    ///
    /// ```
    /// use ticktickrs::models::task::TaskBuilder;
    /// use ticktickrs::models::ChecklistItemRequest;
    ///
    /// let task = TaskBuilder::new("proj123", "Shopping list")
    ///     .items(vec![
    ///         ChecklistItemRequest::new("Milk"),
    ///         ChecklistItemRequest::new("Bread").with_sort_order(1),
    ///         ChecklistItemRequest::new("Eggs").completed(),
    ///     ])
    ///     .build();
    /// ```
    pub fn items(mut self, items: Vec<ChecklistItemRequest>) -> Self {
        self.items = items;
        self
    }

    /// Build the [`Task`] instance.
    ///
    /// The returned task will have an empty `id` field, which will be
    /// populated by the API when the task is created.
    ///
    /// Note: Subtasks set via [`items()`](Self::items) are not included in the
    /// built Task. Use [`into_create_request()`](Self::into_create_request) to
    /// create a request that includes subtasks for the API.
    pub fn build(self) -> Task {
        Task {
            id: String::new(), // Will be set by API
            project_id: self.project_id,
            title: self.title,
            is_all_day: self.is_all_day,
            completed_time: None,
            content: self.content.unwrap_or_default(),
            due_date: self.due_date,
            items: Vec::new(),
            priority: self.priority,
            reminders: Vec::new(),
            repeat_flag: None,
            sort_order: 0,
            start_date: self.start_date,
            status: Status::Normal,
            time_zone: self.time_zone.unwrap_or_default(),
            tags: self.tags,
        }
    }

    /// Build a [`CreateTaskRequest`](crate::api::CreateTaskRequest) for the API.
    ///
    /// This method creates a request that can be passed to
    /// [`TickTickClient::create_task()`](crate::api::TickTickClient::create_task).
    /// Unlike [`build()`](Self::build), this includes subtasks set via
    /// [`items()`](Self::items).
    ///
    /// # Example
    ///
    /// ```
    /// use ticktickrs::models::task::TaskBuilder;
    /// use ticktickrs::models::ChecklistItemRequest;
    ///
    /// let request = TaskBuilder::new("proj123", "Pack for trip")
    ///     .items(vec![
    ///         ChecklistItemRequest::new("Passport"),
    ///         ChecklistItemRequest::new("Clothes"),
    ///     ])
    ///     .into_create_request();
    ///
    /// // Now use: client.create_task(&request).await
    /// ```
    pub fn into_create_request(self) -> crate::api::CreateTaskRequest {
        crate::api::CreateTaskRequest {
            title: self.title,
            project_id: self.project_id,
            content: self.content,
            is_all_day: if self.is_all_day { Some(true) } else { None },
            start_date: self.start_date.map(|d| d.to_rfc3339()),
            due_date: self.due_date.map(|d| d.to_rfc3339()),
            priority: if self.priority != Priority::None {
                Some(self.priority.to_api_value())
            } else {
                None
            },
            time_zone: self.time_zone,
            tags: if self.tags.is_empty() {
                None
            } else {
                Some(self.tags)
            },
            items: if self.items.is_empty() {
                None
            } else {
                Some(self.items)
            },
        }
    }
}

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

    #[test]
    fn test_task_deserialization() {
        let json = r#"{
            "id": "task123",
            "projectId": "proj456",
            "title": "Test Task",
            "isAllDay": false,
            "content": "Task description",
            "priority": 3,
            "status": 0,
            "tags": ["work", "urgent"],
            "items": [],
            "reminders": [],
            "sortOrder": 0,
            "timeZone": "UTC"
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        assert_eq!(task.id, "task123");
        assert_eq!(task.project_id, "proj456");
        assert_eq!(task.title, "Test Task");
        assert_eq!(task.priority, Priority::Medium);
        assert!(!task.is_complete());
        assert_eq!(task.tags, vec!["work", "urgent"]);
    }

    #[test]
    fn test_task_builder() {
        let task = TaskBuilder::new("proj123", "New Task")
            .content("Description")
            .priority(Priority::High)
            .all_day(true)
            .tags(vec!["test".to_string()])
            .build();

        assert_eq!(task.project_id, "proj123");
        assert_eq!(task.title, "New Task");
        assert_eq!(task.content, "Description");
        assert_eq!(task.priority, Priority::High);
        assert!(task.is_all_day);
        assert_eq!(task.tags, vec!["test"]);
    }

    #[test]
    fn test_task_special_characters_in_title() {
        let json = r#"{
            "id": "task789",
            "projectId": "proj456",
            "title": "Test <script>alert('xss')</script> & \"quotes\" 'apostrophes' émojis 🎉",
            "isAllDay": false,
            "content": "",
            "priority": 0,
            "status": 0,
            "tags": [],
            "items": [],
            "reminders": [],
            "sortOrder": 0,
            "timeZone": ""
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        assert_eq!(
            task.title,
            "Test <script>alert('xss')</script> & \"quotes\" 'apostrophes' émojis 🎉"
        );

        // Verify round-trip serialization
        let serialized = serde_json::to_string(&task).unwrap();
        let task2: Task = serde_json::from_str(&serialized).unwrap();
        assert_eq!(task.title, task2.title);
    }

    #[test]
    fn test_task_with_subtasks() {
        let json = r#"{
            "id": "task123",
            "projectId": "proj456",
            "title": "Task with subtasks",
            "isAllDay": false,
            "content": "",
            "priority": 0,
            "status": 0,
            "tags": [],
            "items": [
                {"id": "sub1", "title": "Subtask 1", "status": 0, "completedTime": 0, "isAllDay": false, "sortOrder": 0, "timeZone": ""},
                {"id": "sub2", "title": "Subtask 2", "status": 1, "completedTime": 1704067200, "isAllDay": false, "sortOrder": 1, "timeZone": ""}
            ],
            "reminders": [],
            "sortOrder": 0,
            "timeZone": ""
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        assert_eq!(task.items.len(), 2);
        assert_eq!(task.items[0].title, "Subtask 1");
        assert!(!task.items[0].is_complete());
        assert_eq!(task.items[1].title, "Subtask 2");
        assert!(task.items[1].is_complete());
    }

    #[test]
    fn test_task_with_dates() {
        let json = r#"{
            "id": "task123",
            "projectId": "proj456",
            "title": "Task with dates",
            "isAllDay": true,
            "content": "",
            "dueDate": "2026-01-15T14:00:00Z",
            "startDate": "2026-01-10T09:00:00Z",
            "priority": 5,
            "status": 0,
            "tags": [],
            "items": [],
            "reminders": [],
            "sortOrder": 0,
            "timeZone": "America/New_York"
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        assert!(task.is_all_day);
        assert!(task.due_date.is_some());
        assert!(task.start_date.is_some());
        assert_eq!(task.time_zone, "America/New_York");
        assert_eq!(task.priority, Priority::High);
    }

    #[test]
    fn test_task_completed() {
        let json = r#"{
            "id": "task123",
            "projectId": "proj456",
            "title": "Completed task",
            "isAllDay": false,
            "completedTime": "2026-01-14T10:30:00Z",
            "content": "",
            "priority": 0,
            "status": 2,
            "tags": [],
            "items": [],
            "reminders": [],
            "sortOrder": 0,
            "timeZone": ""
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        assert!(task.is_complete());
        assert!(task.completed_time.is_some());
        assert_eq!(task.status, Status::Complete);
    }

    #[test]
    fn test_task_minimal_json() {
        // Test deserializing a task with only required fields
        let json = r#"{
            "id": "task123",
            "projectId": "proj456",
            "title": "Minimal task"
        }"#;

        let task: Task = serde_json::from_str(json).unwrap();
        assert_eq!(task.id, "task123");
        assert_eq!(task.project_id, "proj456");
        assert_eq!(task.title, "Minimal task");
        assert!(!task.is_all_day);
        assert!(task.items.is_empty());
        assert!(task.tags.is_empty());
        assert_eq!(task.priority, Priority::None);
        assert_eq!(task.status, Status::Normal);
    }
}