Skip to content

Commit

Permalink
[change] improve priorities system
Browse files Browse the repository at this point in the history
  • Loading branch information
seqre committed Sep 24, 2023
1 parent faecc70 commit ffe2b4a
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/commands/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use std::{
collections::HashMap,
default::Default,
fmt::{Display, Formatter},
sync::{
atomic::{AtomicI32, Ordering},
Mutex,
Expand Down Expand Up @@ -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())),
Expand Down Expand Up @@ -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<String> = 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());

Expand Down Expand Up @@ -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<i32> 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<poise::CommandParameterChoice> {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ffe2b4a

Please # to comment.