From 625ab5bb37cbe35fd9662a33bc92f73520e59c81 Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 17 Feb 2020 01:14:03 +0100 Subject: added trait for interacting with persistent storage; tried to make Results easier --- src/util.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs index 34d5670..1aee8a4 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,6 @@ - +use std::error::Error; +use std::fmt::{Display, Formatter}; use std::cmp::{min, max}; use serde_json::Value; @@ -12,6 +13,46 @@ pub trait ToJson { } +pub type AnyError = Box; +pub type Result = std::result::Result; + +#[derive(Debug)] +pub struct AError { + text: String +} + +impl AError { + pub fn new(txt: &str) -> Self{ + AError { + text: txt.to_string() + } + } +} + +impl Error for AError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + None + } +} + +impl Display for AError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "Error: {}", self.text) + } +} + + +#[macro_export] +macro_rules! aerr { + ($description:expr) => {Box::new(crate::util::AError::new($description))} +} + +#[macro_export] +macro_rules! err { + ($description:expr) => {Err(crate::aerr!($description))} +} + + #[macro_export] macro_rules! hashmap { ( $($key:expr => $value:expr ),* ) => {{ -- cgit