Implement modifying tasks' "wait" value

This commit is contained in:
Dustin J. Mitchell 2021-05-23 18:16:11 -04:00 committed by Dustin J. Mitchell
parent d7d703f135
commit e977fb294c
8 changed files with 166 additions and 4 deletions

View file

@ -211,6 +211,20 @@ impl Task {
.unwrap_or("")
}
/// Get the wait time. If this value is set, it will be returned, even
/// if it is in the past.
pub fn get_wait(&self) -> Option<DateTime<Utc>> {
self.get_timestamp("wait")
}
/// Determine whether this task is waiting now.
pub fn is_waiting(&self) -> bool {
if let Some(ts) = self.get_wait() {
return ts > Utc::now();
}
false
}
/// Determine whether this task is active -- that is, that it has been started
/// and not stopped.
pub fn is_active(&self) -> bool {
@ -275,6 +289,10 @@ impl<'r> TaskMut<'r> {
self.set_string("description", Some(description))
}
pub fn set_wait(&mut self, wait: Option<DateTime<Utc>>) -> anyhow::Result<()> {
self.set_timestamp("wait", wait)
}
pub fn set_modified(&mut self, modified: DateTime<Utc>) -> anyhow::Result<()> {
self.set_timestamp("modified", Some(modified))
}
@ -452,6 +470,43 @@ mod test {
assert!(!task.is_active());
}
#[test]
fn test_wait_not_set() {
let task = Task::new(Uuid::new_v4(), TaskMap::new());
assert!(!task.is_waiting());
assert_eq!(task.get_wait(), None);
}
#[test]
fn test_wait_in_past() {
let ts = Utc.ymd(1970, 1, 1).and_hms(0, 0, 0);
let task = Task::new(
Uuid::new_v4(),
vec![(String::from("wait"), format!("{}", ts.timestamp()))]
.drain(..)
.collect(),
);
dbg!(&task);
assert!(!task.is_waiting());
assert_eq!(task.get_wait(), Some(ts));
}
#[test]
fn test_wait_in_future() {
let ts = Utc.ymd(3000, 1, 1).and_hms(0, 0, 0);
let task = Task::new(
Uuid::new_v4(),
vec![(String::from("wait"), format!("{}", ts.timestamp()))]
.drain(..)
.collect(),
);
assert!(task.is_waiting());
assert_eq!(task.get_wait(), Some(ts));
}
#[test]
fn test_has_tag() {
let task = Task::new(