aimcal_core/
todo.rs

1// SPDX-FileCopyrightText: 2025 Zexin Yuan <aim@yzx9.xyz>
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use std::{fmt::Display, num::NonZeroU32, str::FromStr};
6
7use chrono::{DateTime, Local, TimeZone, Utc};
8use icalendar::Component;
9
10use crate::{Config, DateTimeAnchor, LooseDateTime, Priority, SortOrder};
11
12/// Trait representing a todo item.
13pub trait Todo {
14    /// The short identifier for the todo.
15    /// It will be `None` if the event does not have a short ID.
16    /// It is used for display purposes and may not be unique.
17    fn short_id(&self) -> Option<NonZeroU32> {
18        None
19    }
20
21    /// The unique identifier for the todo item.
22    fn uid(&self) -> &str;
23
24    /// The description of the todo item.
25    fn completed(&self) -> Option<DateTime<Local>>;
26
27    /// The description of the todo item, if available.
28    fn description(&self) -> Option<&str>;
29
30    /// The due date and time of the todo item, if available.
31    fn due(&self) -> Option<LooseDateTime>;
32
33    /// The percent complete, from 0 to 100.
34    fn percent_complete(&self) -> Option<u8>;
35
36    /// The priority from 1 to 9, where 1 is the highest priority.
37    fn priority(&self) -> Priority;
38
39    /// The status of the todo item.
40    fn status(&self) -> TodoStatus;
41
42    /// The summary of the todo item.
43    fn summary(&self) -> &str;
44}
45
46impl Todo for icalendar::Todo {
47    fn uid(&self) -> &str {
48        self.get_uid().unwrap_or_default()
49    }
50
51    fn completed(&self) -> Option<DateTime<Local>> {
52        self.get_completed().map(|dt| dt.with_timezone(&Local))
53    }
54
55    fn description(&self) -> Option<&str> {
56        self.get_description()
57    }
58
59    fn due(&self) -> Option<LooseDateTime> {
60        self.get_due().map(Into::into)
61    }
62
63    fn percent_complete(&self) -> Option<u8> {
64        self.get_percent_complete()
65    }
66
67    fn priority(&self) -> Priority {
68        self.get_priority()
69            .map(|p| Priority::from(p as u8))
70            .unwrap_or_default()
71    }
72
73    fn status(&self) -> TodoStatus {
74        self.get_status().map(Into::into).unwrap_or_default()
75    }
76
77    fn summary(&self) -> &str {
78        self.get_summary().unwrap_or_default()
79    }
80}
81
82/// Darft for a todo item, used for creating new todos.
83#[derive(Debug)]
84pub struct TodoDraft {
85    /// The description of the todo item, if available.
86    pub description: Option<String>,
87
88    /// The due date and time of the todo item, if available.
89    pub due: Option<LooseDateTime>,
90
91    /// The percent complete, from 0 to 100, if available.
92    pub percent_complete: Option<u8>,
93
94    /// The priority of the todo item, if available.
95    pub priority: Option<Priority>,
96
97    /// The status of the todo item.
98    pub status: TodoStatus,
99
100    /// The summary of the todo item.
101    pub summary: String,
102}
103
104impl TodoDraft {
105    /// Creates a new empty patch.
106    pub(crate) fn default(config: &Config, now: &DateTime<Local>) -> Self {
107        Self {
108            description: None,
109            due: config.default_due.map(|d| d.resolve_since_datetime(now)),
110            percent_complete: None,
111            priority: Some(config.default_priority),
112            status: TodoStatus::default(),
113            summary: String::default(),
114        }
115    }
116
117    /// Converts the draft into a icalendar Todo component.
118    pub(crate) fn into_ics(
119        self,
120        config: &Config,
121        now: &DateTime<Local>,
122        uid: &str,
123    ) -> icalendar::Todo {
124        let mut todo = icalendar::Todo::with_uid(uid);
125
126        if let Some(description) = self.description {
127            Component::description(&mut todo, &description);
128        }
129
130        if let Some(due) = self.due {
131            icalendar::Todo::due(&mut todo, due);
132        } else if let Some(duration) = config.default_due {
133            icalendar::Todo::due(&mut todo, duration.resolve_since_datetime(now));
134        }
135
136        if let Some(percent) = self.percent_complete {
137            icalendar::Todo::percent_complete(&mut todo, percent.max(100));
138        }
139
140        if let Some(priority) = self.priority {
141            Component::priority(&mut todo, priority.into());
142        } else {
143            Component::priority(&mut todo, config.default_priority.into());
144        }
145
146        icalendar::Todo::status(&mut todo, self.status.into());
147
148        Component::summary(&mut todo, &self.summary);
149
150        todo
151    }
152}
153
154/// Patch for a todo item, allowing partial updates.
155#[derive(Debug, Default, Clone)]
156pub struct TodoPatch {
157    /// The description of the todo item, if available.
158    pub description: Option<Option<String>>,
159
160    /// The due date and time of the todo item, if available.
161    pub due: Option<Option<LooseDateTime>>,
162
163    /// The percent complete, from 0 to 100.
164    pub percent_complete: Option<Option<u8>>,
165
166    /// The priority of the todo item, from 1 to 9, where 1 is the highest priority.
167    pub priority: Option<Priority>,
168
169    /// The status of the todo item, if available.
170    pub status: Option<TodoStatus>,
171
172    /// The summary of the todo item, if available.
173    pub summary: Option<String>,
174}
175
176impl TodoPatch {
177    /// Is this patch empty, meaning no fields are set
178    pub fn is_empty(&self) -> bool {
179        self.description.is_none()
180            && self.due.is_none()
181            && self.percent_complete.is_none()
182            && self.priority.is_none()
183            && self.status.is_none()
184            && self.summary.is_none()
185    }
186
187    /// Applies the patch to a mutable todo item, modifying it in place.
188    pub(crate) fn apply_to<'a, Tz: TimeZone>(
189        &self,
190        now: &DateTime<Tz>,
191        t: &'a mut icalendar::Todo,
192    ) -> &'a mut icalendar::Todo {
193        if let Some(description) = &self.description {
194            match description {
195                Some(desc) => t.description(desc),
196                None => t.remove_description(),
197            };
198        }
199
200        if let Some(due) = &self.due {
201            match due {
202                Some(d) => t.due(*d),
203                None => t.remove_due(),
204            };
205        }
206
207        if let Some(percent) = self.percent_complete {
208            t.percent_complete(percent.unwrap_or(0).max(100));
209        }
210
211        if let Some(priority) = self.priority {
212            t.priority(priority.into());
213        }
214
215        if let Some(status) = self.status {
216            t.status(status.into());
217
218            match status {
219                TodoStatus::Completed => t.completed(now.with_timezone(&Utc)),
220                _ if t.get_completed().is_some() => t.remove_completed(),
221                _ => t,
222            };
223        }
224
225        if let Some(summary) = &self.summary {
226            t.summary(summary);
227        }
228
229        t
230    }
231}
232
233/// The status of a todo item, which can be one of several predefined states.
234#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
235#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
236pub enum TodoStatus {
237    /// The todo item needs action.
238    #[default]
239    NeedsAction,
240
241    /// The todo item has been completed.
242    Completed,
243
244    /// The todo item is currently in process.
245    InProcess,
246
247    /// The todo item has been cancelled.
248    Cancelled,
249}
250
251const STATUS_NEEDS_ACTION: &str = "NEEDS-ACTION";
252const STATUS_COMPLETED: &str = "COMPLETED";
253const STATUS_IN_PROCESS: &str = "IN-PROGRESS";
254const STATUS_CANCELLED: &str = "CANCELLED";
255
256impl AsRef<str> for TodoStatus {
257    fn as_ref(&self) -> &str {
258        match self {
259            TodoStatus::NeedsAction => STATUS_NEEDS_ACTION,
260            TodoStatus::Completed => STATUS_COMPLETED,
261            TodoStatus::InProcess => STATUS_IN_PROCESS,
262            TodoStatus::Cancelled => STATUS_CANCELLED,
263        }
264    }
265}
266
267impl Display for TodoStatus {
268    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
269        write!(f, "{}", self.as_ref())
270    }
271}
272
273impl FromStr for TodoStatus {
274    type Err = ();
275
276    fn from_str(value: &str) -> Result<Self, Self::Err> {
277        match value {
278            STATUS_NEEDS_ACTION => Ok(TodoStatus::NeedsAction),
279            STATUS_COMPLETED => Ok(TodoStatus::Completed),
280            STATUS_IN_PROCESS => Ok(TodoStatus::InProcess),
281            STATUS_CANCELLED => Ok(TodoStatus::Cancelled),
282            _ => Err(()),
283        }
284    }
285}
286
287impl From<TodoStatus> for icalendar::TodoStatus {
288    fn from(item: TodoStatus) -> icalendar::TodoStatus {
289        match item {
290            TodoStatus::NeedsAction => icalendar::TodoStatus::NeedsAction,
291            TodoStatus::Completed => icalendar::TodoStatus::Completed,
292            TodoStatus::InProcess => icalendar::TodoStatus::InProcess,
293            TodoStatus::Cancelled => icalendar::TodoStatus::Cancelled,
294        }
295    }
296}
297
298impl From<icalendar::TodoStatus> for TodoStatus {
299    fn from(status: icalendar::TodoStatus) -> Self {
300        match status {
301            icalendar::TodoStatus::NeedsAction => TodoStatus::NeedsAction,
302            icalendar::TodoStatus::Completed => TodoStatus::Completed,
303            icalendar::TodoStatus::InProcess => TodoStatus::InProcess,
304            icalendar::TodoStatus::Cancelled => TodoStatus::Cancelled,
305        }
306    }
307}
308
309/// Conditions for filtering todo items, such as current time, status, and due date.
310#[derive(Debug, Clone, Copy)]
311pub struct TodoConditions {
312    /// The status of the todo item to filter by, if any.
313    pub status: Option<TodoStatus>,
314
315    /// The priority of the todo item to filter by, if any.
316    pub due: Option<DateTimeAnchor>,
317}
318
319/// Conditions for filtering todo items, such as current time, status, and due date.
320#[derive(Debug, Clone, Copy)]
321pub(crate) struct ParsedTodoConditions {
322    /// The status of the todo item to filter by, if any.
323    pub status: Option<TodoStatus>,
324
325    /// The priority of the todo item to filter by, if any.
326    pub due: Option<DateTime<Local>>,
327}
328
329impl ParsedTodoConditions {
330    pub fn parse(now: &DateTime<Local>, conds: &TodoConditions) -> Self {
331        let status = conds.status;
332        let due = conds.due.map(|a| a.resolve_at_end_of_day(now));
333        ParsedTodoConditions { status, due }
334    }
335}
336
337/// The default sort key for todo items, which is by due date.
338#[derive(Debug, Clone, Copy)]
339pub enum TodoSort {
340    /// Sort by the due date and time of the todo item.
341    Due(SortOrder),
342
343    /// Sort by the priority of the todo item.
344    Priority {
345        /// Sort order, either ascending or descending.
346        order: SortOrder,
347
348        /// Put items with no priority first or last. If none, use the default
349        none_first: Option<bool>,
350    },
351}
352
353#[derive(Debug, Clone, Copy)]
354pub(crate) enum ParsedTodoSort {
355    /// Sort by the due date and time of the todo item.
356    Due(SortOrder),
357
358    /// Sort by the priority of the todo item.
359    Priority {
360        /// Sort order, either ascending or descending.
361        order: SortOrder,
362
363        /// Put items with no priority first or last.
364        none_first: bool,
365    },
366}
367
368impl ParsedTodoSort {
369    pub fn parse(config: &Config, sort: TodoSort) -> Self {
370        match sort {
371            TodoSort::Due(order) => ParsedTodoSort::Due(order),
372            TodoSort::Priority { order, none_first } => ParsedTodoSort::Priority {
373                order,
374                none_first: none_first.unwrap_or(config.default_priority_none_fist),
375            },
376        }
377    }
378
379    pub fn parse_vec(config: &Config, sort: &[TodoSort]) -> Vec<Self> {
380        sort.iter()
381            .map(|s| ParsedTodoSort::parse(config, *s))
382            .collect()
383    }
384}