Initial Commit

This commit is contained in:
Franklin 2022-09-25 11:08:47 -04:00
commit f4ee8ef4b2
3 changed files with 77 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
Cargo.lock

14
Cargo.toml Normal file
View File

@ -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"] }

61
src/lib.rs Normal file
View File

@ -0,0 +1,61 @@
use std::fmt::Display;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MessageResource {
pub key: Option<String>,
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<T> From<T> 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<MessageResource>),
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 {}