taskr/src/task.rs

77 lines
2.1 KiB
Rust
Raw Normal View History

2024-03-20 11:35:12 -05:00
use std::{collections::HashSet, fmt::Display};
use serde::{Deserialize, Serialize};
use sled::IVec;
use crate::jira::Issue;
#[derive(Serialize, Deserialize, Debug)]
pub struct MergeRequestRef {
pub url: String,
pub state: String,
}
#[derive(Serialize, Deserialize, Debug)]
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>,
}
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;
f.write_fmt(format_args!("{jira_key}: {status} {description}",))
}
}
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()?,
status: value.fields.status.status_category.key.to_owned(),
tags,
})
}
}