From 1b586a425f405a8e413f5a6522061476af98b1e1 Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Sun, 27 Mar 2022 17:48:47 -0400 Subject: [PATCH] Treat priority as an arbitrary string This matches what TaskWarrior does: priority is a UDA, and can be redefined by the user's local config. --- cli/src/invocation/cmd/import_tdb2.rs | 6 ++-- cli/src/invocation/cmd/import_tw.rs | 4 +-- taskchampion/src/lib.rs | 2 +- taskchampion/src/task/mod.rs | 2 -- taskchampion/src/task/priority.rs | 49 --------------------------- taskchampion/src/task/task.rs | 30 +++++++++++++--- 6 files changed, 31 insertions(+), 62 deletions(-) delete mode 100644 taskchampion/src/task/priority.rs diff --git a/cli/src/invocation/cmd/import_tdb2.rs b/cli/src/invocation/cmd/import_tdb2.rs index a01ae70fc..c3eb9ba5f 100644 --- a/cli/src/invocation/cmd/import_tdb2.rs +++ b/cli/src/invocation/cmd/import_tdb2.rs @@ -87,7 +87,7 @@ mod test { use pretty_assertions::assert_eq; use std::convert::TryInto; use taskchampion::chrono::{TimeZone, Utc}; - use taskchampion::{Priority, Status}; + use taskchampion::Status; use tempfile::TempDir; #[test] @@ -113,7 +113,7 @@ mod test { .unwrap(); assert_eq!(task.get_description(), "snake 🐍"); assert_eq!(task.get_status(), Status::Pending); - assert_eq!(task.get_priority(), Priority::M); + assert_eq!(task.get_priority(), "M"); assert_eq!(task.get_wait(), None); assert_eq!( task.get_modified(), @@ -128,7 +128,7 @@ mod test { .unwrap(); assert_eq!(task.get_description(), "[TEST] foo"); assert_eq!(task.get_status(), Status::Completed); - assert_eq!(task.get_priority(), Priority::M); + assert_eq!(task.get_priority(), "M".to_string()); assert_eq!(task.get_wait(), None); assert_eq!( task.get_modified(), diff --git a/cli/src/invocation/cmd/import_tw.rs b/cli/src/invocation/cmd/import_tw.rs index fc5158b66..aaee30d90 100644 --- a/cli/src/invocation/cmd/import_tw.rs +++ b/cli/src/invocation/cmd/import_tw.rs @@ -156,7 +156,7 @@ mod test { use serde_json::json; use std::convert::TryInto; use taskchampion::chrono::{TimeZone, Utc}; - use taskchampion::{Priority, Status}; + use taskchampion::Status; #[test] fn stringify_string() { @@ -235,7 +235,7 @@ mod test { .unwrap(); assert_eq!(task.get_description(), "repair window"); assert_eq!(task.get_status(), Status::Completed); - assert_eq!(task.get_priority(), Priority::M); + assert_eq!(task.get_priority(), "M".to_string()); assert_eq!( task.get_wait(), Some(Utc.ymd(2021, 12, 25).and_hms(00, 15, 23)) diff --git a/taskchampion/src/lib.rs b/taskchampion/src/lib.rs index 2e22b992e..63e5c20ea 100644 --- a/taskchampion/src/lib.rs +++ b/taskchampion/src/lib.rs @@ -63,7 +63,7 @@ pub use errors::Error; pub use replica::Replica; pub use server::{Server, ServerConfig}; pub use storage::StorageConfig; -pub use task::{Annotation, Priority, Status, Tag, Task, TaskMut}; +pub use task::{Annotation, Status, Tag, Task, TaskMut}; pub use workingset::WorkingSet; /// Re-exported type from the `uuid` crate, for ease of compatibility for consumers of this crate. diff --git a/taskchampion/src/task/mod.rs b/taskchampion/src/task/mod.rs index bd0808015..2e95b2a09 100644 --- a/taskchampion/src/task/mod.rs +++ b/taskchampion/src/task/mod.rs @@ -2,13 +2,11 @@ use chrono::prelude::*; mod annotation; -mod priority; mod status; mod tag; mod task; pub use annotation::Annotation; -pub use priority::Priority; pub use status::Status; pub use tag::Tag; pub use task::{Task, TaskMut}; diff --git a/taskchampion/src/task/priority.rs b/taskchampion/src/task/priority.rs deleted file mode 100644 index cbe786524..000000000 --- a/taskchampion/src/task/priority.rs +++ /dev/null @@ -1,49 +0,0 @@ -/// The priority of a task -#[derive(Debug, PartialEq)] -pub enum Priority { - /// Low - L, - /// Medium - M, - /// High - H, -} - -#[allow(dead_code)] -impl Priority { - /// Get a Priority from the 1-character value in a TaskMap, - /// defaulting to M - pub(crate) fn from_taskmap(s: &str) -> Priority { - match s { - "L" => Priority::L, - "M" => Priority::M, - "H" => Priority::H, - _ => Priority::M, - } - } - - /// Get the 1-character value for this priority to use in the TaskMap. - pub(crate) fn to_taskmap(&self) -> &str { - match self { - Priority::L => "L", - Priority::M => "M", - Priority::H => "H", - } - } -} - -#[cfg(test)] -mod test { - use super::*; - use pretty_assertions::assert_eq; - - #[test] - fn test_priority() { - assert_eq!(Priority::L.to_taskmap(), "L"); - assert_eq!(Priority::M.to_taskmap(), "M"); - assert_eq!(Priority::H.to_taskmap(), "H"); - assert_eq!(Priority::from_taskmap("L"), Priority::L); - assert_eq!(Priority::from_taskmap("M"), Priority::M); - assert_eq!(Priority::from_taskmap("H"), Priority::H); - } -} diff --git a/taskchampion/src/task/task.rs b/taskchampion/src/task/task.rs index 2a8464837..a3f4384ac 100644 --- a/taskchampion/src/task/task.rs +++ b/taskchampion/src/task/task.rs @@ -1,5 +1,5 @@ use super::tag::{SyntheticTag, TagInner}; -use super::{Annotation, Priority, Status, Tag, Timestamp}; +use super::{Annotation, Status, Tag, Timestamp}; use crate::depmap::DependencyMap; use crate::replica::Replica; use crate::storage::TaskMap; @@ -66,6 +66,7 @@ enum Prop { Modified, Start, Status, + Priority, Wait, End, Entry, @@ -138,11 +139,11 @@ impl Task { self.get_timestamp(Prop::Entry.as_ref()) } - pub fn get_priority(&self) -> Priority { + pub fn get_priority(&self) -> &str { self.taskmap - .get(Prop::Status.as_ref()) - .map(|s| Priority::from_taskmap(s)) - .unwrap_or(Priority::M) + .get(Prop::Priority.as_ref()) + .map(|s| s.as_ref()) + .unwrap_or("") } /// Get the wait time. If this value is set, it will be returned, even @@ -347,6 +348,10 @@ impl<'r> TaskMut<'r> { self.set_string(Prop::Description.as_ref(), Some(description)) } + pub fn set_priority(&mut self, priority: String) -> anyhow::Result<()> { + self.set_string(Prop::Priority.as_ref(), Some(priority)) + } + pub fn set_entry(&mut self, entry: Option>) -> anyhow::Result<()> { self.set_timestamp(Prop::Entry.as_ref(), entry) } @@ -725,6 +730,12 @@ mod test { ); } + #[test] + fn test_get_priority_default() { + let task = Task::new(Uuid::new_v4(), TaskMap::new(), dm()); + assert_eq!(task.get_priority(), ""); + } + #[test] fn test_get_annotations() { let task = Task::new( @@ -810,6 +821,15 @@ mod test { }); } + #[test] + fn test_set_get_priority() { + with_mut_task(|mut task| { + assert_eq!(task.get_priority(), ""); + task.set_priority("H".into()).unwrap(); + assert_eq!(task.get_priority(), "H"); + }); + } + #[test] fn test_set_status_pending() { with_mut_task(|mut task| {