Added initial elements for SQL.

This commit is contained in:
2022-09-27 07:31:59 -04:00
parent dda166ecbe
commit 786c69d840
7 changed files with 363 additions and 533 deletions

View File

@ -1,195 +1,90 @@
use std::{error::Error, fmt, sync::Arc};
use std::{error::Error, fmt};
#[derive(Debug)]
pub enum MTTError {
Generic(Generic),
pub struct DBError {
msg: String,
src: Option<Box<dyn Error + 'static>>,
}
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),
}
}
}
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 {
detail: String,
source: Option<Arc<MTTError>>,
}
impl Generic {
fn new<S>(detail: S) -> Self
impl DBError {
pub fn new<S>(msg: S) -> Self
where
S: Into<String>,
{
Self {
detail: detail.into(),
source: None,
msg: msg.into(),
src: None,
}
}
fn add_source<E>(&mut self, source: E)
pub fn add_source<E>(&mut self, src: E)
where
E: Into<MTTError>,
E: Error + 'static,
{
self.source = Some(Arc::new(source.into()));
self.src = Some(Box::new(src));
}
}
impl Error for Generic {
impl Error for DBError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self.source {
match &self.src {
Some(err) => Some(err.as_ref()),
None => None,
}
}
}
impl fmt::Display for Generic {
impl fmt::Display for DBError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.detail)
write!(f, "{}", self.msg)
}
}
#[cfg(test)]
mod mtterror {
mod create {
use super::*;
#[test]
fn create_with_str() {
let detail = "Something";
let err = MTTError::new(detail);
fn with_str() {
let msg = "something happened";
let err = DBError::new(msg);
assert!(
err.to_string() == detail,
"\n\nGot: {}\nWant: {}\n\n",
err.to_string() == msg,
"Got: {} -- Want: {}",
err.to_string(),
detail
msg
);
assert!(
err.source().is_none(),
"Error source should initialoze to None."
"Error should initialize with no source."
);
}
#[test]
fn create_with_string() {
let detail = "massive".to_string();
let err = MTTError::new(detail.clone());
fn with_string() {
let msg = "it went boom".to_string();
let err = DBError::new(msg.clone());
assert!(
err.to_string() == detail,
"\n\nGot: {}\nWant: {}\n\n",
err.to_string() == msg,
"Got: {} -- Want: {}",
err.to_string(),
detail
msg
);
assert!(
err.source().is_none(),
"Error should initialize with no source."
);
}
#[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 {
use super::*;
#[test]
fn create_with_str() {
let detail = "new error";
let err = Generic::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."
);
let error: MTTError = err.into();
assert!(
error.to_string() == detail,
"\n\nGot: {}\nWant: {}\n\n",
error.to_string(),
detail
);
}
#[test]
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() {
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);
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
);
let msg = "but this caused the problem";
let mut par = DBError::new("parent error");
let src = DBError::new(msg);
par.add_source(src);
let output = par.source();
assert!(output.is_some(), "Should return source.");
let source = output.unwrap();
assert!(source.to_string() == msg);
}
}