diff --git a/taskchampion/src/replica.rs b/taskchampion/src/replica.rs index 271a39436..2b833a323 100644 --- a/taskchampion/src/replica.rs +++ b/taskchampion/src/replica.rs @@ -274,7 +274,7 @@ mod tests { uuid: t.get_uuid(), property: "status".into(), old_value: None, - value: Some("P".into()), + value: Some("pending".into()), timestamp: now, }, ReplicaOp::Update { @@ -294,8 +294,8 @@ mod tests { ReplicaOp::Update { uuid: t.get_uuid(), property: "status".into(), - old_value: Some("P".into()), - value: Some("C".into()), + old_value: Some("pending".into()), + value: Some("completed".into()), timestamp: now, }, ] diff --git a/taskchampion/src/task/status.rs b/taskchampion/src/task/status.rs index fbc4ed866..2b2afb6ba 100644 --- a/taskchampion/src/task/status.rs +++ b/taskchampion/src/task/status.rs @@ -1,9 +1,14 @@ -/// The status of a task. The default status in "Pending". -#[derive(Debug, PartialEq, Clone)] +/// The status of a task, as defined by the task data model. +#[derive(Debug, PartialEq, Clone, strum_macros::Display)] pub enum Status { Pending, Completed, Deleted, + /// Unknown signifies a status in the task DB that was not + /// recognized. This supports forward-compatibility if a + /// new status is added. Tasks with unknown status should + /// be ignored (but not deleted). + Unknown(String), } impl Status { @@ -11,29 +16,20 @@ impl Status { /// defaulting to Pending pub(crate) fn from_taskmap(s: &str) -> Status { match s { - "P" => Status::Pending, - "C" => Status::Completed, - "D" => Status::Deleted, - _ => Status::Pending, + "pending" => Status::Pending, + "completed" => Status::Completed, + "deleted" => Status::Deleted, + v => Status::Unknown(v.to_string()), } } /// Get the 1-character value for this status to use in the TaskMap. pub(crate) fn to_taskmap(&self) -> &str { match self { - Status::Pending => "P", - Status::Completed => "C", - Status::Deleted => "D", - } - } - - /// Get the full-name value for this status to use in the TaskMap. - pub fn to_string(&self) -> &str { - // TODO: should be impl Display - match self { - Status::Pending => "Pending", - Status::Completed => "Completed", - Status::Deleted => "Deleted", + Status::Pending => "pending", + Status::Completed => "completed", + Status::Deleted => "deleted", + Status::Unknown(v) => v.as_ref(), } } } @@ -44,12 +40,29 @@ mod test { use pretty_assertions::assert_eq; #[test] - fn test_status() { - assert_eq!(Status::Pending.to_taskmap(), "P"); - assert_eq!(Status::Completed.to_taskmap(), "C"); - assert_eq!(Status::Deleted.to_taskmap(), "D"); - assert_eq!(Status::from_taskmap("P"), Status::Pending); - assert_eq!(Status::from_taskmap("C"), Status::Completed); - assert_eq!(Status::from_taskmap("D"), Status::Deleted); + fn to_taskmap() { + assert_eq!(Status::Pending.to_taskmap(), "pending"); + assert_eq!(Status::Completed.to_taskmap(), "completed"); + assert_eq!(Status::Deleted.to_taskmap(), "deleted"); + assert_eq!(Status::Unknown("wishful".into()).to_taskmap(), "wishful"); + } + + #[test] + fn from_taskmap() { + assert_eq!(Status::from_taskmap("pending"), Status::Pending); + assert_eq!(Status::from_taskmap("completed"), Status::Completed); + assert_eq!(Status::from_taskmap("deleted"), Status::Deleted); + assert_eq!( + Status::from_taskmap("something-else"), + Status::Unknown("something-else".into()) + ); + } + + #[test] + fn display() { + assert_eq!(format!("{}", Status::Pending), "Pending"); + assert_eq!(format!("{}", Status::Completed), "Completed"); + assert_eq!(format!("{}", Status::Deleted), "Deleted"); + assert_eq!(format!("{}", Status::Unknown("wishful".into())), "Unknown"); } }