morethantext-web/src/morethantext/error.rs

196 lines
4.4 KiB
Rust
Raw Normal View History

2022-07-23 21:28:34 -04:00
use std::{error::Error, fmt, sync::Arc};
2022-07-22 20:34:50 -04:00
#[derive(Debug)]
2022-07-23 21:28:34 -04:00
pub enum MTTError {
Generic(Generic),
}
2022-08-06 12:03:47 -04:00
impl MTTError {
pub fn new<S>(detail: S) -> Self
where
S: Into<String>,
{
Generic::new(detail).into()
}
pub fn add_source<E>(&mut self, source: E)
where
E: Into<MTTError>,
{
match self {
MTTError::Generic(err) => err.add_source(source),
}
}
}
2022-07-23 21:28:34 -04:00
impl Error for MTTError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
MTTError::Generic(err) => err.source(),
}
}
}
impl fmt::Display for MTTError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MTTError::Generic(err) => write!(f, "{}", err),
}
}
}
impl From<Generic> for MTTError {
fn from(err: Generic) -> Self {
MTTError::Generic(err)
}
}
#[derive(Debug)]
pub struct Generic {
2022-07-22 20:34:50 -04:00
detail: String,
2022-07-23 21:28:34 -04:00
source: Option<Arc<MTTError>>,
2022-07-22 20:34:50 -04:00
}
2022-07-23 21:28:34 -04:00
impl Generic {
2022-08-06 12:03:47 -04:00
fn new<S>(detail: S) -> Self
where
S: Into<String>,
{
2022-07-22 20:34:50 -04:00
Self {
2022-08-06 12:03:47 -04:00
detail: detail.into(),
2022-07-23 21:28:34 -04:00
source: None,
2022-07-22 20:34:50 -04:00
}
}
2022-07-23 21:28:34 -04:00
2022-07-23 21:50:37 -04:00
fn add_source<E>(&mut self, source: E)
where
E: Into<MTTError>,
{
self.source = Some(Arc::new(source.into()));
2022-07-23 21:28:34 -04:00
}
2022-07-22 20:34:50 -04:00
}
2022-07-23 21:28:34 -04:00
impl Error for Generic {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self.source {
Some(err) => Some(err.as_ref()),
None => None,
}
}
}
impl fmt::Display for Generic {
2022-07-22 20:34:50 -04:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.detail)
}
}
#[cfg(test)]
2022-08-06 12:03:47 -04:00
mod mtterror {
use super::*;
#[test]
fn create_with_str() {
let detail = "Something";
let err = MTTError::new(detail);
assert!(
err.to_string() == detail,
"\n\nGot: {}\nWant: {}\n\n",
err.to_string(),
detail
);
assert!(
err.source().is_none(),
"Error source should initialoze to None."
);
}
#[test]
fn create_with_string() {
let detail = "massive".to_string();
let err = MTTError::new(detail.clone());
assert!(
err.to_string() == detail,
"\n\nGot: {}\nWant: {}\n\n",
err.to_string(),
detail
);
}
#[test]
fn with_source() {
let mut err = MTTError::new("the error");
let detail = "This is the cause";
let src = MTTError::new(detail);
err.add_source(src);
assert!(
err.source().unwrap().to_string() == detail,
"/n/nGot: {}\nWant: {}\n\n",
err.source().unwrap().to_string(),
detail
);
}
}
#[cfg(test)]
mod generic {
2022-07-22 20:34:50 -04:00
use super::*;
#[test]
2022-08-06 12:03:47 -04:00
fn create_with_str() {
2022-07-22 20:34:50 -04:00
let detail = "new error";
2022-07-23 21:28:34 -04:00
let err = Generic::new(detail);
2022-07-22 20:34:50 -04:00
assert!(
err.to_string() == detail,
"\n\nGot: {}\nWant: {}\n\n",
err.to_string(),
detail
);
2022-07-23 21:28:34 -04:00
assert!(
err.source().is_none(),
"Error source should initialoze to None."
);
let error: MTTError = err.into();
assert!(
error.to_string() == detail,
"\n\nGot: {}\nWant: {}\n\n",
error.to_string(),
detail
);
}
#[test]
2022-08-06 12:03:47 -04:00
fn create_with_string() {
let detail = "some error".to_string();
let err = Generic::new(detail.clone());
assert!(
err.to_string() == detail,
"\n\nGot: {}\nWant: {}\n\n",
err.to_string(),
detail
);
}
#[test]
fn with_source() {
2022-07-23 21:28:34 -04:00
let par_detail = "parent error";
let cld_detail = "child error";
let par_err = Generic::new(par_detail);
let mut cld_err = Generic::new(cld_detail);
cld_err.add_source(par_err);
2022-07-23 21:28:34 -04:00
assert!(
cld_err.source().unwrap().to_string() == par_detail,
"/n/nGot: {}\nWant: {}\n\n",
cld_err.source().unwrap().to_string(),
par_detail
);
let error: MTTError = cld_err.into();
assert!(
error.source().unwrap().to_string() == par_detail,
"/n/nGot: {}\nWant: {}\n\n",
error.source().unwrap().to_string(),
par_detail
);
2022-07-22 20:34:50 -04:00
}
}