diff --git a/src/commands/todo.rs b/src/commands/todo.rs index 6e9e9b3..4055e88 100644 --- a/src/commands/todo.rs +++ b/src/commands/todo.rs @@ -5,6 +5,8 @@ use std::{ collections::HashMap, + default::Default, + fmt::{Display, Formatter}, sync::{ atomic::{AtomicI32, Ordering}, Mutex, @@ -227,10 +229,7 @@ pub async fn add( }; let assignee = assignee.map(|m| m.user.id.0 as i64); - let priority = match priority { - Some(priority) => priority as i32, - None => 0, - }; + let priority = priority.unwrap_or_default() as i32; let new_todo = NewTodo { channel_id: &(i64::from(ctx.channel_id())), @@ -399,15 +398,13 @@ pub async fn set_priority( ) -> Result<()> { use crate::schema::todos::dsl::{channel_id, id, priority, todo, todos}; - let new_priority = match new_priority { - Some(prio) => prio as i32, - None => 0, - }; + let new_priority = new_priority.unwrap_or_default(); + let new_priority_int = new_priority as i32; let reassigned: QueryResult = diesel::update(todos) .filter(channel_id.eq(i64::from(ctx.channel_id()))) .filter(id.eq(todo_id as i32)) - .set(priority.eq(new_priority)) + .set(priority.eq(new_priority_int)) .returning(todo) .get_result(&mut ctx.data().db.get().unwrap()); @@ -519,28 +516,42 @@ enum EmbedData { // TODO: this implementation is so bad, improve pls -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub enum Priority { + None, Low, Medium, High, } impl Priority { - const VALUES: [&str; 3] = ["Low", "Medium", "High"]; + const VALUES: [&'static str; 4] = ["None", "Low", "Medium", "High"]; } impl From for Priority { fn from(value: i32) -> Self { match value { - 0 => Priority::Low, - 1 => Priority::Medium, - 2 => Priority::High, + 0 => Priority::None, + 1 => Priority::Low, + 2 => Priority::Medium, + 3 => Priority::High, _ => unreachable!(), } } } +impl Display for Priority { + fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { + write!(f, "{}", Priority::VALUES[*self as usize]) + } +} + +impl Default for Priority { + fn default() -> Self { + Priority::None + } +} + #[async_trait] impl SlashArgument for Priority { fn choices() -> Vec { @@ -611,7 +622,7 @@ fn create_embed(builder: &mut CreateEmbed, data: EmbedData) -> &mut CreateEmbed if entry.priority != 0 { let priority = Priority::from(entry.priority); - title = format!("{title} - {:?}", priority); + title = format!("{title} - {priority}"); } if entry.completed {