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.
This commit is contained in:
Dustin J. Mitchell 2022-03-27 17:48:47 -04:00
parent 0dd2d9cd30
commit 1b586a425f
No known key found for this signature in database
6 changed files with 31 additions and 62 deletions

View file

@ -87,7 +87,7 @@ mod test {
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use std::convert::TryInto; use std::convert::TryInto;
use taskchampion::chrono::{TimeZone, Utc}; use taskchampion::chrono::{TimeZone, Utc};
use taskchampion::{Priority, Status}; use taskchampion::Status;
use tempfile::TempDir; use tempfile::TempDir;
#[test] #[test]
@ -113,7 +113,7 @@ mod test {
.unwrap(); .unwrap();
assert_eq!(task.get_description(), "snake 🐍"); assert_eq!(task.get_description(), "snake 🐍");
assert_eq!(task.get_status(), Status::Pending); 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_wait(), None);
assert_eq!( assert_eq!(
task.get_modified(), task.get_modified(),
@ -128,7 +128,7 @@ mod test {
.unwrap(); .unwrap();
assert_eq!(task.get_description(), "[TEST] foo"); assert_eq!(task.get_description(), "[TEST] foo");
assert_eq!(task.get_status(), Status::Completed); 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_wait(), None);
assert_eq!( assert_eq!(
task.get_modified(), task.get_modified(),

View file

@ -156,7 +156,7 @@ mod test {
use serde_json::json; use serde_json::json;
use std::convert::TryInto; use std::convert::TryInto;
use taskchampion::chrono::{TimeZone, Utc}; use taskchampion::chrono::{TimeZone, Utc};
use taskchampion::{Priority, Status}; use taskchampion::Status;
#[test] #[test]
fn stringify_string() { fn stringify_string() {
@ -235,7 +235,7 @@ mod test {
.unwrap(); .unwrap();
assert_eq!(task.get_description(), "repair window"); assert_eq!(task.get_description(), "repair window");
assert_eq!(task.get_status(), Status::Completed); assert_eq!(task.get_status(), Status::Completed);
assert_eq!(task.get_priority(), Priority::M); assert_eq!(task.get_priority(), "M".to_string());
assert_eq!( assert_eq!(
task.get_wait(), task.get_wait(),
Some(Utc.ymd(2021, 12, 25).and_hms(00, 15, 23)) Some(Utc.ymd(2021, 12, 25).and_hms(00, 15, 23))

View file

@ -63,7 +63,7 @@ pub use errors::Error;
pub use replica::Replica; pub use replica::Replica;
pub use server::{Server, ServerConfig}; pub use server::{Server, ServerConfig};
pub use storage::StorageConfig; 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; pub use workingset::WorkingSet;
/// Re-exported type from the `uuid` crate, for ease of compatibility for consumers of this crate. /// Re-exported type from the `uuid` crate, for ease of compatibility for consumers of this crate.

View file

@ -2,13 +2,11 @@
use chrono::prelude::*; use chrono::prelude::*;
mod annotation; mod annotation;
mod priority;
mod status; mod status;
mod tag; mod tag;
mod task; mod task;
pub use annotation::Annotation; pub use annotation::Annotation;
pub use priority::Priority;
pub use status::Status; pub use status::Status;
pub use tag::Tag; pub use tag::Tag;
pub use task::{Task, TaskMut}; pub use task::{Task, TaskMut};

View file

@ -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);
}
}

View file

@ -1,5 +1,5 @@
use super::tag::{SyntheticTag, TagInner}; use super::tag::{SyntheticTag, TagInner};
use super::{Annotation, Priority, Status, Tag, Timestamp}; use super::{Annotation, Status, Tag, Timestamp};
use crate::depmap::DependencyMap; use crate::depmap::DependencyMap;
use crate::replica::Replica; use crate::replica::Replica;
use crate::storage::TaskMap; use crate::storage::TaskMap;
@ -66,6 +66,7 @@ enum Prop {
Modified, Modified,
Start, Start,
Status, Status,
Priority,
Wait, Wait,
End, End,
Entry, Entry,
@ -138,11 +139,11 @@ impl Task {
self.get_timestamp(Prop::Entry.as_ref()) self.get_timestamp(Prop::Entry.as_ref())
} }
pub fn get_priority(&self) -> Priority { pub fn get_priority(&self) -> &str {
self.taskmap self.taskmap
.get(Prop::Status.as_ref()) .get(Prop::Priority.as_ref())
.map(|s| Priority::from_taskmap(s)) .map(|s| s.as_ref())
.unwrap_or(Priority::M) .unwrap_or("")
} }
/// Get the wait time. If this value is set, it will be returned, even /// 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)) 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<DateTime<Utc>>) -> anyhow::Result<()> { pub fn set_entry(&mut self, entry: Option<DateTime<Utc>>) -> anyhow::Result<()> {
self.set_timestamp(Prop::Entry.as_ref(), entry) 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] #[test]
fn test_get_annotations() { fn test_get_annotations() {
let task = Task::new( 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] #[test]
fn test_set_status_pending() { fn test_set_status_pending() {
with_mut_task(|mut task| { with_mut_task(|mut task| {