commit f4ee8ef4b2529e3d33bdf18384248361a957a63a Author: Franklin Date: Sun Sep 25 11:08:47 2022 -0400 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..869df07 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ab63493 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "err" +version = "0.1.0" +edition = "2021" +authors = ["Franklin E. Blanco"] +description = "Just some useful addons for actix web." +license = "MIT" +readme = "README.md" +repository = "https://github.com/franklinblanco/err.git" + +[lib] + +[dependencies] +serde = { version = "1.0", features = ["derive"] } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f58379e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,61 @@ +use std::fmt::Display; +use serde::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct MessageResource { + pub key: Option, + pub message: String, +} +impl Display for MessageResource { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "MessageResource Key: {:#?}, Message: {}", self.key, self.message) + } +} +impl MessageResource { + pub fn new(key: &str, msg: &str) -> Self { + Self { key: Some(key.to_string()), message: msg.to_string() } + } + pub fn new_from_str(msg: &str) -> Self { + Self { key: None, message: msg.to_string() } + } + pub fn new_from_string(msg: String) -> Self { + Self { key: None, message: msg } + } +} +impl From for MessageResource +where T: std::error::Error +{ + fn from(err: T) -> Self { + Self { key: None, message: err.to_string() } + } +} +impl Default for MessageResource{ + fn default() -> Self { + Self { key: None, message: "".to_string() } + } +} + +#[derive(Debug, Clone)] +pub enum Error { + Network(MessageResource), + IO(MessageResource), + Privilege(MessageResource), + UnexpectedStatusCode(u16, u16, Vec), + Serde(MessageResource), + Parser(MessageResource), + Unspecified +} +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::Network(message) => write!(f, "Error of type Network. MessageResource: {message}"), + Error::IO(message) => write!(f, "Error of type IO. MessageResource: {message}"), + Error::Privilege(message) => write!(f, "Error of type Privilege. MessageResource: {message}"), + Error::UnexpectedStatusCode(expected, actual, messages) => write!(f, "Error of type UnexpectedStatusCode. Expected: {expected}, Actual: {actual}, MessageResources: {:#?}", messages), + Error::Serde(message) => write!(f, "Error of type Serialization/Deserialization. MessageResource: {message}"), + Error::Unspecified => write!(f, "Error of type Unspecified."), + Error::Parser(message) => write!(f, "Error of type Parser. MessageResource: {message}"), + } + } +} +impl std::error::Error for Error {} \ No newline at end of file