1use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7use crate::de;
8
9#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
10pub struct Ref {
11 #[serde(default, deserialize_with = "de::id_string")]
12 pub id: String,
13 #[serde(default)]
14 pub name: Option<String>,
15}
16
17#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
18pub struct User {
19 #[serde(default, deserialize_with = "de::id_string")]
20 pub id: String,
21 #[serde(default)]
22 pub username: Option<String>,
23 #[serde(default)]
24 pub email: Option<String>,
25 #[serde(default)]
26 pub color: Option<String>,
27 #[serde(default)]
28 pub initials: Option<String>,
29 #[serde(default)]
30 pub timezone: Option<String>,
31 #[serde(default, rename = "profilePicture", alias = "profile_picture")]
32 pub profile_picture: Option<String>,
33}
34
35impl User {
36 pub fn display(&self) -> String {
38 self.username
39 .clone()
40 .filter(|u| !u.is_empty())
41 .unwrap_or_else(|| self.id.clone())
42 }
43}
44
45#[derive(Debug, Clone, Default, Serialize, Deserialize)]
46pub struct Member {
47 pub user: User,
48}
49
50#[derive(Debug, Clone, Default, Serialize, Deserialize)]
51pub struct Workspace {
52 #[serde(deserialize_with = "de::id_string")]
53 pub id: String,
54 pub name: String,
55 #[serde(default)]
56 pub color: Option<String>,
57 #[serde(default, deserialize_with = "de::null_default")]
58 pub members: Vec<Member>,
59}
60
61#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
62pub struct Status {
63 #[serde(default, deserialize_with = "de::opt_id_string")]
64 pub id: Option<String>,
65 pub status: String,
66 #[serde(default)]
67 pub color: Option<String>,
68 #[serde(default, deserialize_with = "de::opt_i64")]
69 pub orderindex: Option<i64>,
70 #[serde(default, rename = "type")]
71 pub kind: Option<String>,
72}
73
74impl Status {
75 pub fn is_closed(&self) -> bool {
76 matches!(self.kind.as_deref(), Some("closed") | Some("done"))
77 }
78}
79
80#[derive(Debug, Clone, Default, Serialize, Deserialize)]
81pub struct Space {
82 #[serde(deserialize_with = "de::id_string")]
83 pub id: String,
84 pub name: String,
85 #[serde(default)]
86 pub private: bool,
87 #[serde(default)]
88 pub archived: bool,
89 #[serde(default, deserialize_with = "de::null_default")]
90 pub statuses: Vec<Status>,
91}
92
93#[derive(Debug, Clone, Default, Serialize, Deserialize)]
94pub struct List {
95 #[serde(deserialize_with = "de::id_string")]
96 pub id: String,
97 pub name: String,
98 #[serde(default)]
99 pub folder: Option<Ref>,
100 #[serde(default)]
101 pub space: Option<Ref>,
102 #[serde(default, deserialize_with = "de::null_default")]
103 pub statuses: Vec<Status>,
104 #[serde(default)]
105 pub archived: bool,
106 #[serde(default)]
107 pub content: Option<String>,
108 #[serde(default, deserialize_with = "de::opt_i64")]
109 pub task_count: Option<i64>,
110}
111
112#[derive(Debug, Clone, Default, Serialize, Deserialize)]
113pub struct Folder {
114 #[serde(deserialize_with = "de::id_string")]
115 pub id: String,
116 pub name: String,
117 #[serde(default)]
118 pub hidden: bool,
119 #[serde(default)]
120 pub archived: bool,
121 #[serde(default)]
122 pub space: Option<Ref>,
123 #[serde(default, deserialize_with = "de::null_default")]
124 pub lists: Vec<List>,
125}
126
127#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
128pub struct Priority {
129 #[serde(default, deserialize_with = "de::opt_id_string")]
130 pub id: Option<String>,
131 #[serde(default)]
132 pub priority: Option<String>,
133 #[serde(default)]
134 pub color: Option<String>,
135}
136
137#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
138pub struct Tag {
139 pub name: String,
140 #[serde(default)]
141 pub tag_fg: Option<String>,
142 #[serde(default)]
143 pub tag_bg: Option<String>,
144}
145
146#[derive(Debug, Clone, Default, Serialize, Deserialize)]
147pub struct ChecklistItem {
148 #[serde(default, deserialize_with = "de::id_string")]
149 pub id: String,
150 #[serde(default)]
151 pub name: String,
152 #[serde(default)]
153 pub resolved: bool,
154 #[serde(default)]
155 pub assignee: Option<User>,
156}
157
158#[derive(Debug, Clone, Default, Serialize, Deserialize)]
159pub struct Checklist {
160 #[serde(default, deserialize_with = "de::id_string")]
161 pub id: String,
162 #[serde(default)]
163 pub name: String,
164 #[serde(default, deserialize_with = "de::null_default")]
165 pub items: Vec<ChecklistItem>,
166}
167
168#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
169pub struct FieldOption {
170 #[serde(default, deserialize_with = "de::id_string")]
171 pub id: String,
172 #[serde(default)]
173 pub name: String,
174 #[serde(default, deserialize_with = "de::opt_i64")]
175 pub orderindex: Option<i64>,
176}
177
178#[derive(Debug, Clone, Default, Serialize, Deserialize)]
181pub struct CustomField {
182 #[serde(deserialize_with = "de::id_string")]
183 pub id: String,
184 pub name: String,
185 #[serde(rename = "type")]
186 pub kind: String,
187 #[serde(default)]
188 pub type_config: Value,
189 #[serde(default)]
190 pub required: Option<bool>,
191 #[serde(default, skip_serializing_if = "Option::is_none")]
192 pub value: Option<Value>,
193}
194
195impl CustomField {
196 pub fn options(&self) -> Vec<FieldOption> {
198 self.type_config
199 .get("options")
200 .and_then(Value::as_array)
201 .map(|opts| {
202 opts.iter()
203 .map(|o| FieldOption {
204 id: o
205 .get("id")
206 .map(|v| v.as_str().map(str::to_string).unwrap_or(v.to_string()))
207 .unwrap_or_default(),
208 name: o
209 .get("name")
210 .or_else(|| o.get("label"))
211 .and_then(Value::as_str)
212 .unwrap_or_default()
213 .to_string(),
214 orderindex: o.get("orderindex").and_then(|v| {
215 v.as_i64()
216 .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
217 }),
218 })
219 .collect()
220 })
221 .unwrap_or_default()
222 }
223
224 pub fn display_value(&self, tz: Option<&str>) -> Option<String> {
226 let value = self.value.as_ref()?;
227 if value.is_null() {
228 return None;
229 }
230 let options = self.options();
231 let option_name = |v: &Value| -> String {
232 let by_id = v.as_str().and_then(|s| options.iter().find(|o| o.id == s));
233 let by_index = v
234 .as_i64()
235 .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
236 .and_then(|i| options.iter().find(|o| o.orderindex == Some(i)));
237 by_id
238 .or(by_index)
239 .map(|o| o.name.clone())
240 .unwrap_or_else(|| plain(v))
241 };
242 let rendered = match self.kind.as_str() {
243 "drop_down" => option_name(value),
244 "labels" => value
245 .as_array()
246 .map(|a| a.iter().map(option_name).collect::<Vec<_>>().join(", "))
247 .unwrap_or_else(|| plain(value)),
248 "date" => value
249 .as_i64()
250 .or_else(|| value.as_str().and_then(|s| s.parse().ok()))
251 .map(|ms| crate::time::format_ms(ms, tz))
252 .unwrap_or_else(|| plain(value)),
253 "users" | "tasks" | "list_relationship" => value
254 .as_array()
255 .map(|a| {
256 a.iter()
257 .map(|u| {
258 ["username", "name", "email", "id"]
259 .iter()
260 .find_map(|k| u.get(*k).filter(|v| !v.is_null()).map(plain))
261 .unwrap_or_else(|| plain(u))
262 })
263 .collect::<Vec<_>>()
264 .join(", ")
265 })
266 .unwrap_or_else(|| plain(value)),
267 "location" => value
268 .get("formatted_address")
269 .map(plain)
270 .unwrap_or_else(|| plain(value)),
271 "manual_progress" | "automatic_progress" => value
272 .get("percent_completed")
273 .or_else(|| value.get("current"))
274 .map(|v| format!("{}%", plain(v)))
275 .unwrap_or_else(|| plain(value)),
276 _ => plain(value),
277 };
278 if rendered.is_empty() {
279 None
280 } else {
281 Some(rendered)
282 }
283 }
284}
285
286fn plain(v: &Value) -> String {
287 match v {
288 Value::String(s) => s.clone(),
289 Value::Null => String::new(),
290 other => other.to_string(),
291 }
292}
293
294#[derive(Debug, Clone, Default, Serialize, Deserialize)]
295pub struct Task {
296 #[serde(deserialize_with = "de::id_string")]
297 pub id: String,
298 #[serde(default)]
299 pub custom_id: Option<String>,
300 #[serde(default)]
301 pub name: String,
302 #[serde(default)]
303 pub text_content: Option<String>,
304 #[serde(default)]
305 pub description: Option<String>,
306 #[serde(default)]
307 pub markdown_description: Option<String>,
308 #[serde(default)]
309 pub status: Status,
310 #[serde(default, deserialize_with = "de::opt_i64")]
311 pub date_created: Option<i64>,
312 #[serde(default, deserialize_with = "de::opt_i64")]
313 pub date_updated: Option<i64>,
314 #[serde(default, deserialize_with = "de::opt_i64")]
315 pub date_closed: Option<i64>,
316 #[serde(default, deserialize_with = "de::opt_i64")]
317 pub date_done: Option<i64>,
318 #[serde(default)]
319 pub archived: bool,
320 #[serde(default)]
321 pub creator: Option<User>,
322 #[serde(default, deserialize_with = "de::null_default")]
323 pub assignees: Vec<User>,
324 #[serde(default, deserialize_with = "de::null_default")]
325 pub watchers: Vec<User>,
326 #[serde(default, deserialize_with = "de::null_default")]
327 pub checklists: Vec<Checklist>,
328 #[serde(default, deserialize_with = "de::null_default")]
329 pub tags: Vec<Tag>,
330 #[serde(default)]
331 pub parent: Option<String>,
332 #[serde(default)]
333 pub priority: Option<Priority>,
334 #[serde(default, deserialize_with = "de::opt_i64")]
335 pub due_date: Option<i64>,
336 #[serde(default, deserialize_with = "de::opt_i64")]
337 pub start_date: Option<i64>,
338 #[serde(default, deserialize_with = "de::opt_f64")]
339 pub points: Option<f64>,
340 #[serde(default, deserialize_with = "de::opt_i64")]
341 pub time_estimate: Option<i64>,
342 #[serde(default, deserialize_with = "de::opt_i64")]
343 pub time_spent: Option<i64>,
344 #[serde(default, deserialize_with = "de::null_default")]
345 pub custom_fields: Vec<CustomField>,
346 #[serde(default, deserialize_with = "de::null_default")]
347 pub dependencies: Vec<Value>,
348 #[serde(default, deserialize_with = "de::null_default")]
349 pub linked_tasks: Vec<Value>,
350 #[serde(default, deserialize_with = "de::opt_id_string")]
351 pub team_id: Option<String>,
352 #[serde(default)]
353 pub url: Option<String>,
354 #[serde(default)]
355 pub list: Option<Ref>,
356 #[serde(default)]
357 pub folder: Option<Ref>,
358 #[serde(default)]
359 pub space: Option<Ref>,
360 #[serde(default, deserialize_with = "de::opt_i64")]
361 pub custom_item_id: Option<i64>,
362 #[serde(default, skip_serializing_if = "Option::is_none")]
363 pub subtasks: Option<Vec<Task>>,
364 #[serde(default, deserialize_with = "de::null_default")]
365 pub attachments: Vec<Value>,
366}
367
368impl Task {
369 pub fn display_id(&self) -> &str {
371 self.custom_id
372 .as_deref()
373 .filter(|s| !s.is_empty())
374 .unwrap_or(&self.id)
375 }
376
377 pub fn priority_name(&self) -> Option<&str> {
378 self.priority.as_ref()?.priority.as_deref()
379 }
380}
381
382#[derive(Debug, Clone, Default, Serialize, Deserialize)]
383pub struct Comment {
384 #[serde(deserialize_with = "de::id_string")]
385 pub id: String,
386 #[serde(default)]
387 pub comment_text: String,
388 #[serde(default)]
389 pub user: Option<User>,
390 #[serde(default, deserialize_with = "de::opt_i64")]
391 pub date: Option<i64>,
392 #[serde(default)]
393 pub resolved: bool,
394 #[serde(default)]
395 pub assignee: Option<User>,
396 #[serde(default, deserialize_with = "de::opt_i64")]
397 pub reply_count: Option<i64>,
398}
399
400#[derive(Debug, Clone, Default, Serialize, Deserialize)]
401pub struct TimeEntry {
402 #[serde(deserialize_with = "de::id_string")]
403 pub id: String,
404 #[serde(default)]
405 pub task: Option<Value>,
406 #[serde(default)]
407 pub user: Option<User>,
408 #[serde(default)]
409 pub billable: bool,
410 #[serde(default, deserialize_with = "de::opt_i64")]
411 pub start: Option<i64>,
412 #[serde(default, deserialize_with = "de::opt_i64")]
413 pub end: Option<i64>,
414 #[serde(default, deserialize_with = "de::opt_i64")]
416 pub duration: Option<i64>,
417 #[serde(default)]
418 pub description: Option<String>,
419 #[serde(default, deserialize_with = "de::null_default")]
420 pub tags: Vec<Value>,
421 #[serde(default, skip_serializing_if = "Option::is_none")]
423 pub task_location: Option<Value>,
424}
425
426impl TimeEntry {
427 pub fn is_running(&self) -> bool {
428 self.duration.is_some_and(|d| d < 0)
429 }
430
431 pub fn task_id(&self) -> Option<String> {
432 let t = self.task.as_ref()?;
433 t.get("custom_id")
434 .and_then(Value::as_str)
435 .or_else(|| t.get("id").and_then(Value::as_str))
436 .map(str::to_string)
437 }
438
439 pub fn task_name(&self) -> Option<String> {
440 self.task
441 .as_ref()?
442 .get("name")
443 .and_then(Value::as_str)
444 .map(str::to_string)
445 }
446
447 pub fn effective_ms(&self, now_ms: i64) -> i64 {
449 match (self.duration, self.start) {
450 (Some(d), Some(start)) if d < 0 => now_ms - start,
451 (Some(d), _) => d,
452 _ => 0,
453 }
454 }
455}
456
457pub fn parse_priority(input: &str) -> crate::Result<Option<u8>> {
459 match input.trim().to_ascii_lowercase().as_str() {
460 "1" | "urgent" => Ok(Some(1)),
461 "2" | "high" => Ok(Some(2)),
462 "3" | "normal" => Ok(Some(3)),
463 "4" | "low" => Ok(Some(4)),
464 "none" | "null" | "" => Ok(None),
465 other => Err(crate::ClickUpError::Invalid(format!(
466 "unknown priority '{other}' (use urgent, high, normal, low or none)"
467 ))),
468 }
469}
470
471#[cfg(test)]
472mod tests {
473 use super::*;
474 use serde_json::json;
475
476 #[test]
477 fn task_parses_string_timestamps_and_nulls() {
478 let t: Task = serde_json::from_value(json!({
479 "id": "abc", "custom_id": null, "name": "x",
480 "status": {"status": "open", "type": "open", "orderindex": 0},
481 "date_created": "1700000000000", "due_date": null, "points": "3",
482 "assignees": [{"id": 1, "username": "a"}], "tags": null,
483 "priority": {"id": "2", "priority": "high"},
484 "custom_fields": [{"id": "f", "name": "F", "type": "text", "value": "v"}]
485 }))
486 .unwrap();
487 assert_eq!(t.date_created, Some(1_700_000_000_000));
488 assert_eq!(t.points, Some(3.0));
489 assert_eq!(t.assignees[0].id, "1");
490 assert_eq!(t.display_id(), "abc");
491 assert_eq!(t.priority_name(), Some("high"));
492 }
493
494 #[test]
495 fn dropdown_and_labels_display_names() {
496 let mut f: CustomField = serde_json::from_value(json!({
497 "id": "f", "name": "Severity", "type": "drop_down",
498 "type_config": {"options": [
499 {"id": "o1", "name": "Low", "orderindex": 0},
500 {"id": "o2", "name": "High", "orderindex": 1}
501 ]},
502 "value": 1
503 }))
504 .unwrap();
505 assert_eq!(f.display_value(None).as_deref(), Some("High"));
506 f.value = Some(json!("o1"));
507 assert_eq!(f.display_value(None).as_deref(), Some("Low"));
508
509 let l: CustomField = serde_json::from_value(json!({
510 "id": "l", "name": "Areas", "type": "labels",
511 "type_config": {"options": [{"id": "a", "label": "API"}, {"id": "b", "label": "UI"}]},
512 "value": ["a", "b"]
513 }))
514 .unwrap();
515 assert_eq!(l.display_value(None).as_deref(), Some("API, UI"));
516 }
517
518 #[test]
519 fn priorities() {
520 assert_eq!(parse_priority("High").unwrap(), Some(2));
521 assert_eq!(parse_priority("none").unwrap(), None);
522 assert!(parse_priority("meh").is_err());
523 }
524}