taskr/src/task.rs

114 lines
3.1 KiB
Rust
Raw Normal View History

2024-03-20 12:20:20 -05:00
use std::{cmp::Ordering, collections::HashSet, fmt::Display};
2024-03-20 11:35:12 -05:00
use serde::{Deserialize, Serialize};
use sled::IVec;
use crate::jira::Issue;
2024-04-17 10:26:15 -05:00
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
2024-03-20 11:35:12 -05:00
pub struct MergeRequestRef {
pub url: String,
pub state: String,
}
2024-04-17 10:26:15 -05:00
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
2024-03-20 11:35:12 -05:00
pub struct Task {
pub jira_key: String,
pub description: String,
pub merge_requests: Vec<MergeRequestRef>,
pub jira_priority: i64,
pub local_priority: i64,
pub status: String,
pub tags: HashSet<String>,
}
2024-03-20 12:20:20 -05:00
const STATUS_PRIORITY: [&str; 4] = ["Blocked", "InProgress", "DevReady", "Backlog"];
impl PartialOrd for Task {
2024-04-16 15:15:40 -05:00
/// We sort tasks first by their status. If those are equal, we sort by their priority, favoring the `local_priority`.
2024-03-20 12:20:20 -05:00
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
2024-04-16 15:15:40 -05:00
Some(self.cmp(other))
}
}
impl Ord for Task {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
2024-03-20 12:20:20 -05:00
if self.eq(other) {
2024-04-16 15:15:40 -05:00
return Ordering::Equal;
2024-03-20 12:20:20 -05:00
}
2024-04-16 15:15:40 -05:00
{
let a = STATUS_PRIORITY.iter().position(|s| **s == self.status);
let b = STATUS_PRIORITY.iter().position(|s| **s == other.status);
let o = a.cmp(&b);
2024-03-20 12:20:20 -05:00
if o != Ordering::Equal {
return o;
}
}
2024-04-16 15:15:40 -05:00
{
let o = self.local_priority.cmp(&other.local_priority);
if o != Ordering::Equal {
return o;
}
}
self.jira_priority.cmp(&other.jira_priority)
2024-03-20 12:20:20 -05:00
}
}
2024-03-20 11:35:12 -05:00
impl Display for Task {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self {
description,
jira_key,
merge_requests,
jira_priority,
local_priority,
status,
tags,
} = self;
2024-03-20 12:20:20 -05:00
f.write_fmt(format_args!(
2024-04-16 22:49:24 -05:00
"{jira_key} {status:>10} {jira_priority} {description}",
2024-03-20 12:20:20 -05:00
))
2024-03-20 11:35:12 -05:00
}
}
impl TryFrom<IVec> for Task {
type Error = anyhow::Error;
fn try_from(value: IVec) -> std::prelude::v1::Result<Self, Self::Error> {
serde_json::from_slice(&value).map_err(|e| e.into())
}
}
impl TryInto<IVec> for &Task {
type Error = anyhow::Error;
fn try_into(self) -> std::prelude::v1::Result<IVec, Self::Error> {
Ok(IVec::from(serde_json::to_vec(self)?))
}
}
impl TryFrom<&Issue> for Task {
type Error = anyhow::Error;
fn try_from(value: &Issue) -> std::prelude::v1::Result<Self, Self::Error> {
let mut tags = HashSet::from_iter(value.fields.labels.iter().map(|s| s.to_owned()));
if let Some(cs) = &value.fields.components {
for c in cs.iter().map(|c| c.name.to_owned()) {
tags.insert(c);
}
}
Ok(Self {
jira_key: value.key.to_owned(),
description: value.fields.summary.to_owned(),
merge_requests: vec![],
jira_priority: value.fields.priority.id.parse()?,
local_priority: value.fields.priority.id.parse()?,
2024-03-20 12:20:20 -05:00
status: value.fields.status.name.to_owned(),
2024-03-20 11:35:12 -05:00
tags,
})
}
}