diff --git a/Cargo.lock b/Cargo.lock index d0f5fa1..2c2d8f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1325,6 +1325,7 @@ version = "0.1.0" dependencies = [ "anyhow", "color-eyre", + "regex", "reqwest", "reqwest-middleware", "reqwest-retry", diff --git a/Cargo.toml b/Cargo.toml index 9ae6b00..b5ee3a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] anyhow = "1.0.81" color-eyre = "0.6.3" +regex = "1.10.3" reqwest = { version = "0.11.26", features = ["json", "socks"] } reqwest-middleware = "0.2.5" reqwest-retry = "0.4.0" diff --git a/src/tasks.rs b/src/tasks.rs index 8c31f8d..ffcb93b 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -1,4 +1,5 @@ -use std::{collections::HashSet, env, process::Command}; +use std::{sync::OnceLock, collections::HashSet, env, process::Command}; +use regex::Regex; use serde::{Deserialize, Serialize}; use sled::IVec; @@ -37,6 +38,14 @@ impl TryFrom for Task { } } +impl TryInto for &Task { + type Error = anyhow::Error; + + fn try_into(self) -> std::prelude::v1::Result { + Ok(IVec::from(serde_json::to_vec(self)?)) + } +} + #[derive(Debug)] pub struct Desyncs { issues: Vec, @@ -76,6 +85,42 @@ impl Tasks { .collect::>>() } + pub fn get(&self, key: &str) -> Result> { + let ivec = self.db.open_tree("tasks")?.get(key)?; + match ivec { + Some(v) => Ok(Some(Task::try_from(v)?)), + None => Ok(None), + } + } + + pub fn save(&self, task: &Task) -> Result<()> { + let ivec: IVec = task.try_into()?; + let _previous_value = self.db.open_tree("tasks")?.insert(&task.jira_key, ivec)?; + self.db.flush()?; + Ok(()) + } + + pub fn delete(&self, task: &Task) -> Result<()> { + let _previous_value = self.db.open_tree("tasks")?.remove(&task.jira_key)?; + self.db.flush()?; + Ok(()) + } + + pub fn purge_all(&self) -> Result<()> { + for t in self.all()? { + self.delete(&t)?; + } + self.db.flush()?; + Ok(()) + } + + pub fn cleanup(&self, task: &mut Task) -> Result<()> { + static RE: OnceLock = OnceLock::new(|| Regex::new(r"^\s*\:[a-zA-Z0-9_-]+\:\s*").unwrap()); + let mut changed = false; + t.description.match + Ok(()) + } + /// fetches jira issues and compares to known local tasks /// for use when sync'ing local tasks to remote state (jira, gitlab) pub async fn desyncs(&self) -> Result {