icalendar/components/
todo.rsuse chrono::*;
use super::*;
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct Todo {
pub(super) inner: InnerComponent,
}
impl Todo {
pub fn new() -> Self {
Default::default()
}
pub fn done(&mut self) -> Self {
Todo {
inner: self.inner.done(),
}
}
pub fn percent_complete(&mut self, percent: u8) -> &mut Self {
self.add_property("PERCENT-COMPLETE", percent.to_string())
}
pub fn get_percent_complete(&self) -> Option<u8> {
self.property_value("PERCENT-COMPLETE")?.parse().ok()
}
pub fn due<T: Into<DatePerhapsTime>>(&mut self, dt: T) -> &mut Self {
let calendar_dt: DatePerhapsTime = dt.into();
self.append_property(calendar_dt.to_property("DUE"))
}
pub fn get_due(&self) -> Option<DatePerhapsTime> {
DatePerhapsTime::from_property(self.properties().get("DUE")?)
}
pub fn completed(&mut self, dt: DateTime<Utc>) -> &mut Self {
self.add_property("COMPLETED", format_utc_date_time(dt))
}
pub fn get_completed(&self) -> Option<DateTime<Utc>> {
let completed = self.property_value("COMPLETED")?;
parse_utc_date_time(completed)
}
pub fn status(&mut self, status: TodoStatus) -> &mut Self {
self.append_property(status)
}
pub fn get_status(&self) -> Option<TodoStatus> {
TodoStatus::from_str(self.property_value("STATUS")?)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_properties_unset() {
let todo = Todo::new();
assert_eq!(todo.get_percent_complete(), None);
assert_eq!(todo.get_status(), None);
assert_eq!(todo.get_completed(), None);
assert_eq!(todo.get_due(), None);
}
#[test]
fn get_properties_set() {
let completed = Utc.with_ymd_and_hms(2001, 3, 13, 14, 15, 16).unwrap();
let todo = Todo::new()
.percent_complete(42)
.status(TodoStatus::NeedsAction)
.completed(completed)
.done();
assert_eq!(todo.get_percent_complete(), Some(42));
assert_eq!(todo.get_status(), Some(TodoStatus::NeedsAction));
assert_eq!(todo.get_completed(), Some(completed))
}
#[test]
fn get_date_times_naive() {
let naive_date_time = NaiveDate::from_ymd_opt(2001, 3, 13)
.unwrap()
.and_hms_opt(14, 15, 16)
.unwrap();
let todo = Todo::new().due(naive_date_time).done();
assert_eq!(todo.get_due(), Some(naive_date_time.into()));
}
#[test]
fn get_date_times_utc() {
let utc_date_time = Utc.with_ymd_and_hms(2001, 3, 13, 14, 15, 16).unwrap();
let todo = Todo::new()
.due(utc_date_time)
.completed(utc_date_time)
.done();
assert_eq!(todo.get_due(), Some(utc_date_time.into()));
assert_eq!(todo.get_completed(), Some(utc_date_time));
}
#[test]
fn get_dates_naive() {
let naive_date = NaiveDate::from_ymd_opt(2001, 3, 13).unwrap();
let todo = Todo::new().due(naive_date).done();
assert_eq!(todo.get_due(), Some(naive_date.into()));
}
}