mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
bin/taud: create error type
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -6036,6 +6036,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"simplelog",
|
||||
"smol",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -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"
|
||||
|
||||
13
bin/taud/src/error.rs
Normal file
13
bin/taud/src/error.rs
Normal file
@@ -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<T> = std::result::Result<T, TaudError>;
|
||||
@@ -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;
|
||||
|
||||
@@ -16,6 +16,7 @@ use darkfi::{
|
||||
};
|
||||
|
||||
mod crdt;
|
||||
mod error;
|
||||
mod jsonrpc;
|
||||
mod month_tasks;
|
||||
mod task_info;
|
||||
|
||||
@@ -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<Vec<TaskInfo>> {
|
||||
pub fn objects(&self) -> TaudResult<Vec<TaskInfo>> {
|
||||
let mut tks: Vec<TaskInfo> = 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>(&Self::get_path(&self.created_at, &self.settings), self)
|
||||
.map_err(TaudError::Darkfi)
|
||||
}
|
||||
|
||||
pub fn load_or_create(date: &Timestamp, settings: &Settings) -> Result<Self> {
|
||||
pub fn load_or_create(date: &Timestamp, settings: &Settings) -> TaudResult<Self> {
|
||||
match crate::util::load::<Self>(&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<Vec<TaskInfo>> {
|
||||
pub fn load_current_open_tasks(settings: &Settings) -> TaudResult<Vec<TaskInfo>> {
|
||||
let mt = Self::load_or_create(&get_current_time(), settings)?;
|
||||
Ok(mt.objects()?.into_iter().filter(|t| t.get_state() != "stop").collect())
|
||||
}
|
||||
|
||||
@@ -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<Timestamp>,
|
||||
rank: u32,
|
||||
settings: &Settings,
|
||||
) -> Result<Self> {
|
||||
) -> TaudResult<Self> {
|
||||
// generate ref_id
|
||||
let ref_id = random_ref_id();
|
||||
|
||||
@@ -80,6 +78,7 @@ impl TaskInfo {
|
||||
|
||||
let task_ids: Vec<u32> =
|
||||
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<Self> {
|
||||
pub fn load(ref_id: &str, settings: &Settings) -> TaudResult<Self> {
|
||||
let mut task = crate::util::load::<Self>(&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>(&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<S: io::Write>(&self, s: S) -> Result<usize> {
|
||||
fn encode<S: io::Write>(&self, s: S) -> darkfi::Result<usize> {
|
||||
encode_vec(&self.0, s)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for TaskEvents {
|
||||
fn decode<D: io::Read>(d: D) -> Result<Self> {
|
||||
fn decode<D: io::Read>(d: D) -> darkfi::Result<Self> {
|
||||
Ok(Self(decode_vec(d)?))
|
||||
}
|
||||
}
|
||||
impl Encodable for TaskComments {
|
||||
fn encode<S: io::Write>(&self, s: S) -> Result<usize> {
|
||||
fn encode<S: io::Write>(&self, s: S) -> darkfi::Result<usize> {
|
||||
encode_vec(&self.0, s)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for TaskComments {
|
||||
fn decode<D: io::Read>(d: D) -> Result<Self> {
|
||||
fn decode<D: io::Read>(d: D) -> darkfi::Result<Self> {
|
||||
Ok(Self(decode_vec(d)?))
|
||||
}
|
||||
}
|
||||
impl Encodable for TaskProjects {
|
||||
fn encode<S: io::Write>(&self, s: S) -> Result<usize> {
|
||||
fn encode<S: io::Write>(&self, s: S) -> darkfi::Result<usize> {
|
||||
encode_vec(&self.0, s)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for TaskProjects {
|
||||
fn decode<D: io::Read>(d: D) -> Result<Self> {
|
||||
fn decode<D: io::Read>(d: D) -> darkfi::Result<Self> {
|
||||
Ok(Self(decode_vec(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for TaskAssigns {
|
||||
fn encode<S: io::Write>(&self, s: S) -> Result<usize> {
|
||||
fn encode<S: io::Write>(&self, s: S) -> darkfi::Result<usize> {
|
||||
encode_vec(&self.0, s)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for TaskAssigns {
|
||||
fn decode<D: io::Read>(d: D) -> Result<Self> {
|
||||
fn decode<D: io::Read>(d: D) -> darkfi::Result<Self> {
|
||||
Ok(Self(decode_vec(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_vec<T: Encodable, S: io::Write>(vec: &Vec<T>, mut s: S) -> Result<usize> {
|
||||
fn encode_vec<T: Encodable, S: io::Write>(vec: &Vec<T>, mut s: S) -> darkfi::Result<usize> {
|
||||
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<T: Encodable, S: io::Write>(vec: &Vec<T>, mut s: S) -> Result<usiz
|
||||
Ok(len)
|
||||
}
|
||||
|
||||
fn decode_vec<T: Decodable, D: io::Read>(mut d: D) -> Result<Vec<T>> {
|
||||
fn decode_vec<T: Decodable, D: io::Read>(mut d: D) -> darkfi::Result<Vec<T>> {
|
||||
let len = VarInt::decode(&mut d)?.0;
|
||||
let mut ret = Vec::with_capacity(len as usize);
|
||||
for _ in 0..len {
|
||||
|
||||
Reference in New Issue
Block a user