diff --git a/Cargo.lock b/Cargo.lock index a54c92689..4c44b2fff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6036,6 +6036,7 @@ dependencies = [ "serde_json", "simplelog", "smol", + "thiserror", ] [[package]] diff --git a/bin/taud/Cargo.toml b/bin/taud/Cargo.toml index ba03cd75e..f6effb6b0 100644 --- a/bin/taud/Cargo.toml +++ b/bin/taud/Cargo.toml @@ -24,6 +24,7 @@ num_cpus = "1.13.1" simplelog = "0.12.0-alpha1" rand = "0.8.5" chrono = "0.4.19" +thiserror = "1.0.24" # Encoding and parsing serde_json = "1.0.79" diff --git a/bin/taud/src/error.rs b/bin/taud/src/error.rs new file mode 100644 index 000000000..0a256856c --- /dev/null +++ b/bin/taud/src/error.rs @@ -0,0 +1,13 @@ +#[derive(Debug, thiserror::Error)] +pub enum TaudError { + #[error("Due timestamp invalid")] + InvalidDueTime, + #[error("Invalid Id")] + InvalidId, + #[error("Invalid Data: `{0}` ")] + InvalidData(String), + #[error(transparent)] + Darkfi(#[from] darkfi::error::Error), +} + +pub type TaudResult = std::result::Result; diff --git a/bin/taud/src/jsonrpc.rs b/bin/taud/src/jsonrpc.rs index 2725fce36..4d94f6bb8 100644 --- a/bin/taud/src/jsonrpc.rs +++ b/bin/taud/src/jsonrpc.rs @@ -10,10 +10,11 @@ use darkfi::{ jsonrpc::{error as jsonerr, response as jsonresp, ErrorCode, JsonRequest, JsonResult}, rpcserver::RequestHandler, }, - Result, + Error, }; use crate::{ + error::TaudResult, month_tasks::MonthTasks, task_info::{Comment, TaskInfo}, util::{get_current_time, Settings, Timestamp}, @@ -142,10 +143,10 @@ impl JsonRpcInterface { ); } - let result: Result<()> = async move { + let result: TaudResult<()> = async move { task.save()?; task.activate()?; - self.notify_queue_sender.send(task).await?; + self.notify_queue_sender.send(task).await.map_err(Error::from)?; Ok(()) } .await; @@ -191,9 +192,9 @@ impl JsonRpcInterface { } }; - let result: Result<()> = async move { + let result: TaudResult<()> = async move { task.save()?; - self.notify_queue_sender.send(task).await?; + self.notify_queue_sender.send(task).await.map_err(Error::from)?; Ok(()) } .await; @@ -254,9 +255,9 @@ impl JsonRpcInterface { task.set_state(state); - let result: Result<()> = async move { + let result: TaudResult<()> = async move { task.save()?; - self.notify_queue_sender.send(task).await?; + self.notify_queue_sender.send(task).await.map_err(Error::from)?; Ok(()) } .await; @@ -305,9 +306,9 @@ impl JsonRpcInterface { task.set_comment(Comment::new(comment_content, comment_author)); - let result: Result<()> = async move { + let result: TaudResult<()> = async move { task.save()?; - self.notify_queue_sender.send(task).await?; + self.notify_queue_sender.send(task).await.map_err(Error::from)?; Ok(()) } .await; diff --git a/bin/taud/src/main.rs b/bin/taud/src/main.rs index 03f8c0b44..e0c0f8cbe 100644 --- a/bin/taud/src/main.rs +++ b/bin/taud/src/main.rs @@ -16,6 +16,7 @@ use darkfi::{ }; mod crdt; +mod error; mod jsonrpc; mod month_tasks; mod task_info; diff --git a/bin/taud/src/month_tasks.rs b/bin/taud/src/month_tasks.rs index 5b0de48b7..b15bb80e3 100644 --- a/bin/taud/src/month_tasks.rs +++ b/bin/taud/src/month_tasks.rs @@ -3,9 +3,8 @@ use std::path::PathBuf; use chrono::{TimeZone, Utc}; use serde::{Deserialize, Serialize}; -use darkfi::Result; - use crate::{ + error::{TaudError, TaudResult}, task_info::TaskInfo, util::{get_current_time, Settings, Timestamp}, }; @@ -30,7 +29,7 @@ impl MonthTasks { self.task_tks.push(ref_id.into()); } - pub fn objects(&self) -> Result> { + pub fn objects(&self) -> TaudResult> { let mut tks: Vec = vec![]; for ref_id in self.task_tks.iter() { @@ -65,11 +64,12 @@ impl MonthTasks { .join(Utc.timestamp(date.0, 0).format("%m%y").to_string()) } - pub fn save(&self) -> Result<()> { + pub fn save(&self) -> TaudResult<()> { crate::util::save::(&Self::get_path(&self.created_at, &self.settings), self) + .map_err(TaudError::Darkfi) } - pub fn load_or_create(date: &Timestamp, settings: &Settings) -> Result { + pub fn load_or_create(date: &Timestamp, settings: &Settings) -> TaudResult { match crate::util::load::(&Self::get_path(date, settings)) { Ok(mut mt) => { mt.set_settings(settings); @@ -84,7 +84,7 @@ impl MonthTasks { } } - pub fn load_current_open_tasks(settings: &Settings) -> Result> { + pub fn load_current_open_tasks(settings: &Settings) -> TaudResult> { let mt = Self::load_or_create(&get_current_time(), settings)?; Ok(mt.objects()?.into_iter().filter(|t| t.get_state() != "stop").collect()) } diff --git a/bin/taud/src/task_info.rs b/bin/taud/src/task_info.rs index 4ca81ed72..76c6c17dc 100644 --- a/bin/taud/src/task_info.rs +++ b/bin/taud/src/task_info.rs @@ -2,14 +2,12 @@ use std::{io, path::PathBuf}; use serde::{Deserialize, Serialize}; -use darkfi::{ - util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable}, - Result, -}; +use darkfi::util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable}; use darkfi::util::serial::VarInt; use crate::{ + error::{TaudError, TaudResult}, month_tasks::MonthTasks, util::{find_free_id, get_current_time, random_ref_id, Settings, Timestamp}, }; @@ -72,7 +70,7 @@ impl TaskInfo { due: Option, rank: u32, settings: &Settings, - ) -> Result { + ) -> TaudResult { // generate ref_id let ref_id = random_ref_id(); @@ -80,6 +78,7 @@ impl TaskInfo { let task_ids: Vec = MonthTasks::load_current_open_tasks(settings)?.into_iter().map(|t| t.id).collect(); + let id: u32 = find_free_id(&task_ids); Ok(Self { @@ -98,17 +97,18 @@ impl TaskInfo { }) } - pub fn load(ref_id: &str, settings: &Settings) -> Result { + pub fn load(ref_id: &str, settings: &Settings) -> TaudResult { let mut task = crate::util::load::(&Self::get_path(ref_id, settings))?; task.set_settings(settings); Ok(task) } - pub fn save(&self) -> Result<()> { + pub fn save(&self) -> TaudResult<()> { crate::util::save::(&Self::get_path(&self.ref_id, &self.settings), self) + .map_err(TaudError::Darkfi) } - pub fn activate(&self) -> Result<()> { + pub fn activate(&self) -> TaudResult<()> { let mut mt = MonthTasks::load_or_create(&self.created_at, &self.settings)?; mt.add(&self.ref_id); mt.save() @@ -175,52 +175,52 @@ impl TaskInfo { } impl Encodable for TaskEvents { - fn encode(&self, s: S) -> Result { + fn encode(&self, s: S) -> darkfi::Result { encode_vec(&self.0, s) } } impl Decodable for TaskEvents { - fn decode(d: D) -> Result { + fn decode(d: D) -> darkfi::Result { Ok(Self(decode_vec(d)?)) } } impl Encodable for TaskComments { - fn encode(&self, s: S) -> Result { + fn encode(&self, s: S) -> darkfi::Result { encode_vec(&self.0, s) } } impl Decodable for TaskComments { - fn decode(d: D) -> Result { + fn decode(d: D) -> darkfi::Result { Ok(Self(decode_vec(d)?)) } } impl Encodable for TaskProjects { - fn encode(&self, s: S) -> Result { + fn encode(&self, s: S) -> darkfi::Result { encode_vec(&self.0, s) } } impl Decodable for TaskProjects { - fn decode(d: D) -> Result { + fn decode(d: D) -> darkfi::Result { Ok(Self(decode_vec(d)?)) } } impl Encodable for TaskAssigns { - fn encode(&self, s: S) -> Result { + fn encode(&self, s: S) -> darkfi::Result { encode_vec(&self.0, s) } } impl Decodable for TaskAssigns { - fn decode(d: D) -> Result { + fn decode(d: D) -> darkfi::Result { Ok(Self(decode_vec(d)?)) } } -fn encode_vec(vec: &Vec, mut s: S) -> Result { +fn encode_vec(vec: &Vec, mut s: S) -> darkfi::Result { let mut len = 0; len += VarInt(vec.len() as u64).encode(&mut s)?; for c in vec.iter() { @@ -229,7 +229,7 @@ fn encode_vec(vec: &Vec, mut s: S) -> Result(mut d: D) -> Result> { +fn decode_vec(mut d: D) -> darkfi::Result> { let len = VarInt::decode(&mut d)?.0; let mut ret = Vec::with_capacity(len as usize); for _ in 0..len {