completed error move.
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit
This commit is contained in:
parent
972c4686e0
commit
a26471088d
@ -99,7 +99,7 @@ mod id {
|
||||
Err(err) => match err.get_code() {
|
||||
ErrorType::IDErr(id) => match id {
|
||||
IDError::InvalidUuid(data) => assert_eq!(data, bad),
|
||||
}
|
||||
},
|
||||
_ => unreachable!("Returned wrong error"),
|
||||
},
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
pub mod id;
|
||||
mod record;
|
||||
mod table;
|
||||
pub mod table;
|
||||
|
||||
use crate::{
|
||||
data::table::Table,
|
||||
@ -47,8 +47,8 @@ impl Database {
|
||||
fn add_table(&mut self, name: &str, table: Table) -> Result<(), MTTError> {
|
||||
match self.tables.get(name) {
|
||||
Some(_) => {
|
||||
let error = ErrorType::TableRecordInvalidFieldName(name.to_string());
|
||||
return Err(MTTError::new(error));
|
||||
let err = DBError::DuplicateTable(name.to_string());
|
||||
return Err(err.into());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
@ -5,7 +5,41 @@ use crate::{
|
||||
},
|
||||
error::{ErrorType, MTTError},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, fmt};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TBLError {
|
||||
DuplicateField(String),
|
||||
InvalidField(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for TBLError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
TBLError::DuplicateField(data) => write!(f, "'{}' already exists", data),
|
||||
TBLError::InvalidField(data) => write!(f, "'{}' invalid field name", data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod errors {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn duplicate_field() {
|
||||
let data = "something";
|
||||
let err = TBLError::DuplicateField(data.to_string());
|
||||
assert_eq!(err.to_string(), format!("'{}' already exists", data));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_field() {
|
||||
let data = "something";
|
||||
let err = TBLError::InvalidField(data.to_string());
|
||||
assert_eq!(err.to_string(), format!("'{}' invalid field name", data));
|
||||
}
|
||||
}
|
||||
|
||||
enum FieldType {
|
||||
ID,
|
||||
@ -41,8 +75,8 @@ impl Table {
|
||||
fn add_field(&mut self, name: &str, field_type: FieldType) -> Result<(), MTTError> {
|
||||
match self.fields.get(name) {
|
||||
Some(_) => {
|
||||
let err = ErrorType::TableAddFieldDuplicate(name.to_string());
|
||||
Err(MTTError::new(err))
|
||||
let err = TBLError::DuplicateField(name.to_string());
|
||||
Err(err.into())
|
||||
}
|
||||
None => {
|
||||
self.fields.insert(name.to_string(), FieldDef::new());
|
||||
@ -56,8 +90,8 @@ impl Table {
|
||||
match self.fields.get(key) {
|
||||
Some(_) => {}
|
||||
None => {
|
||||
let err = ErrorType::TableRecordInvalidFieldName(key.to_string());
|
||||
return Err(MTTError::new(err));
|
||||
let err = TBLError::InvalidField(key.to_string());
|
||||
return Err(err.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -100,7 +134,10 @@ mod table {
|
||||
match tbl.add_field(name, FieldType::ID) {
|
||||
Ok(_) => unreachable!(" Should not duplicates."),
|
||||
Err(err) => match err.get_code() {
|
||||
ErrorType::TableAddFieldDuplicate(result) => assert_eq!(result, name),
|
||||
ErrorType::TBLErr(data) => {match data {
|
||||
TBLError::DuplicateField(fname) => assert_eq!(fname, name),
|
||||
_ => unreachable!("Should have been a dupluicate field error"),
|
||||
}}
|
||||
_ => unreachable!("should produce a duplicate name error"),
|
||||
},
|
||||
}
|
||||
@ -133,11 +170,12 @@ mod table {
|
||||
match tbl.add_record(rec) {
|
||||
Ok(_) => unreachable!("should have produced an error"),
|
||||
Err(err) => match err.get_code() {
|
||||
ErrorType::TableRecordInvalidFieldName(result) => {
|
||||
assert_eq!(result, name);
|
||||
}
|
||||
_ => unreachable!("should have been invalid name error"),
|
||||
ErrorType::TBLErr(error) => match error {
|
||||
TBLError::InvalidField(result) => assert_eq!(result, name),
|
||||
_ => unreachable!("should have been invalid field name"),
|
||||
},
|
||||
_ => unreachable!("should have been a table error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
63
src/error.rs
63
src/error.rs
@ -1,12 +1,11 @@
|
||||
use crate::data::{DBError, id::IDError};
|
||||
use crate::data::{id::IDError, DBError, table::TBLError};
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ErrorType {
|
||||
DBErr(DBError),
|
||||
IDErr(IDError),
|
||||
TableAddFieldDuplicate(String),
|
||||
TableRecordInvalidFieldName(String),
|
||||
TBLErr(TBLError),
|
||||
}
|
||||
|
||||
impl From<DBError> for ErrorType {
|
||||
@ -21,16 +20,18 @@ impl From<IDError> for ErrorType {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TBLError> for ErrorType {
|
||||
fn from(value: TBLError) -> Self {
|
||||
ErrorType::TBLErr(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ErrorType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ErrorType::DBErr(data) => write!(f, "database: {}", data),
|
||||
ErrorType::IDErr(data) => write!(f, "id: {}", data),
|
||||
|
||||
ErrorType::TableAddFieldDuplicate(data) => write!(f, "field '{}' already exists", data),
|
||||
ErrorType::TableRecordInvalidFieldName(data) => {
|
||||
write!(f, "invalid field name '{}'", data)
|
||||
}
|
||||
ErrorType::TBLErr(data) => write!(f, "table: {}", data),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -52,6 +53,13 @@ mod errortypes {
|
||||
let result = ErrorType::from(err.clone());
|
||||
assert_eq!(result.to_string(), format!("id: {}", err));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_error() {
|
||||
let err = TBLError::InvalidField("bad".to_string());
|
||||
let result = ErrorType::from(err.clone());
|
||||
assert_eq!(result.to_string(), format!("table: {}", err));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -83,13 +91,24 @@ impl fmt::Display for MTTError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DBError> for MTTError {
|
||||
fn from(value: DBError) -> Self {
|
||||
Self { err: value.into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IDError> for MTTError {
|
||||
fn from(value: IDError) -> Self {
|
||||
Self {
|
||||
err: value.into(),
|
||||
Self { err: value.into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TBLError> for MTTError {
|
||||
fn from(value: TBLError) -> Self {
|
||||
Self { err: value.into() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod errors {
|
||||
@ -112,24 +131,16 @@ mod errors {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_duplicate_table_name() {
|
||||
let data = rand_str();
|
||||
let etype = ErrorType::TableAddFieldDuplicate(data.clone());
|
||||
let result = MTTError::from(etype);
|
||||
match result.get_code() {
|
||||
ErrorType::TableAddFieldDuplicate(txt) => assert_eq!(txt, &data),
|
||||
_ => unreachable!("should have been ErrorType::FieldIDInvalid"),
|
||||
}
|
||||
assert_eq!(
|
||||
result.to_string(),
|
||||
format!("field '{}' already exists", data)
|
||||
);
|
||||
fn from_database_error() {
|
||||
let error = DBError::DuplicateTable(rand_str());
|
||||
let err = MTTError::from(error.clone());
|
||||
assert_eq!(err.to_string(), ErrorType::DBErr(error).to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_type_strings() {
|
||||
let data = rand_str();
|
||||
let etype = ErrorType::TableRecordInvalidFieldName(data.clone());
|
||||
assert_eq!(etype.to_string(), format!("invalid field name '{}'", data));
|
||||
fn from_table_error() {
|
||||
let error = TBLError::InvalidField(rand_str());
|
||||
let err = MTTError::from(error.clone());
|
||||
assert_eq!(err.to_string(), ErrorType::TBLErr(error).to_string());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user