completed error move.
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit

This commit is contained in:
Jeff Baskin 2024-12-01 23:00:07 -05:00
parent 972c4686e0
commit a26471088d
4 changed files with 93 additions and 44 deletions

View File

@ -99,7 +99,7 @@ mod id {
Err(err) => match err.get_code() { Err(err) => match err.get_code() {
ErrorType::IDErr(id) => match id { ErrorType::IDErr(id) => match id {
IDError::InvalidUuid(data) => assert_eq!(data, bad), IDError::InvalidUuid(data) => assert_eq!(data, bad),
} },
_ => unreachable!("Returned wrong error"), _ => unreachable!("Returned wrong error"),
}, },
} }

View File

@ -1,6 +1,6 @@
pub mod id; pub mod id;
mod record; mod record;
mod table; pub mod table;
use crate::{ use crate::{
data::table::Table, data::table::Table,
@ -47,8 +47,8 @@ impl Database {
fn add_table(&mut self, name: &str, table: Table) -> Result<(), MTTError> { fn add_table(&mut self, name: &str, table: Table) -> Result<(), MTTError> {
match self.tables.get(name) { match self.tables.get(name) {
Some(_) => { Some(_) => {
let error = ErrorType::TableRecordInvalidFieldName(name.to_string()); let err = DBError::DuplicateTable(name.to_string());
return Err(MTTError::new(error)); return Err(err.into());
} }
None => {} None => {}
} }

View File

@ -5,7 +5,41 @@ use crate::{
}, },
error::{ErrorType, MTTError}, 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 { enum FieldType {
ID, ID,
@ -41,8 +75,8 @@ impl Table {
fn add_field(&mut self, name: &str, field_type: FieldType) -> Result<(), MTTError> { fn add_field(&mut self, name: &str, field_type: FieldType) -> Result<(), MTTError> {
match self.fields.get(name) { match self.fields.get(name) {
Some(_) => { Some(_) => {
let err = ErrorType::TableAddFieldDuplicate(name.to_string()); let err = TBLError::DuplicateField(name.to_string());
Err(MTTError::new(err)) Err(err.into())
} }
None => { None => {
self.fields.insert(name.to_string(), FieldDef::new()); self.fields.insert(name.to_string(), FieldDef::new());
@ -56,8 +90,8 @@ impl Table {
match self.fields.get(key) { match self.fields.get(key) {
Some(_) => {} Some(_) => {}
None => { None => {
let err = ErrorType::TableRecordInvalidFieldName(key.to_string()); let err = TBLError::InvalidField(key.to_string());
return Err(MTTError::new(err)); return Err(err.into());
} }
} }
} }
@ -100,7 +134,10 @@ mod table {
match tbl.add_field(name, FieldType::ID) { match tbl.add_field(name, FieldType::ID) {
Ok(_) => unreachable!(" Should not duplicates."), Ok(_) => unreachable!(" Should not duplicates."),
Err(err) => match err.get_code() { 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"), _ => unreachable!("should produce a duplicate name error"),
}, },
} }
@ -133,11 +170,12 @@ mod table {
match tbl.add_record(rec) { match tbl.add_record(rec) {
Ok(_) => unreachable!("should have produced an error"), Ok(_) => unreachable!("should have produced an error"),
Err(err) => match err.get_code() { Err(err) => match err.get_code() {
ErrorType::TableRecordInvalidFieldName(result) => { ErrorType::TBLErr(error) => match error {
assert_eq!(result, name); TBLError::InvalidField(result) => assert_eq!(result, name),
} _ => unreachable!("should have been invalid field name"),
_ => unreachable!("should have been invalid name error"), },
}, _ => unreachable!("should have been a table error"),
}
} }
} }
} }

View File

@ -1,12 +1,11 @@
use crate::data::{DBError, id::IDError}; use crate::data::{id::IDError, DBError, table::TBLError};
use std::{error::Error, fmt}; use std::{error::Error, fmt};
#[derive(Debug)] #[derive(Debug)]
pub enum ErrorType { pub enum ErrorType {
DBErr(DBError), DBErr(DBError),
IDErr(IDError), IDErr(IDError),
TableAddFieldDuplicate(String), TBLErr(TBLError),
TableRecordInvalidFieldName(String),
} }
impl From<DBError> for ErrorType { 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 { impl fmt::Display for ErrorType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
ErrorType::DBErr(data) => write!(f, "database: {}", data), ErrorType::DBErr(data) => write!(f, "database: {}", data),
ErrorType::IDErr(data) => write!(f, "id: {}", data), ErrorType::IDErr(data) => write!(f, "id: {}", data),
ErrorType::TBLErr(data) => write!(f, "table: {}", data),
ErrorType::TableAddFieldDuplicate(data) => write!(f, "field '{}' already exists", data),
ErrorType::TableRecordInvalidFieldName(data) => {
write!(f, "invalid field name '{}'", data)
}
} }
} }
} }
@ -52,6 +53,13 @@ mod errortypes {
let result = ErrorType::from(err.clone()); let result = ErrorType::from(err.clone());
assert_eq!(result.to_string(), format!("id: {}", err)); 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)] #[derive(Debug)]
@ -83,14 +91,25 @@ impl fmt::Display for MTTError {
} }
} }
impl From<IDError> for MTTError { impl From<DBError> for MTTError {
fn from(value: IDError) -> Self { fn from(value: DBError) -> Self {
Self { Self { err: value.into() }
err: value.into(),
}
} }
} }
impl From<IDError> for MTTError {
fn from(value: IDError) -> Self {
Self { err: value.into() }
}
}
impl From<TBLError> for MTTError {
fn from(value: TBLError) -> Self {
Self { err: value.into() }
}
}
#[cfg(test)] #[cfg(test)]
mod errors { mod errors {
use super::*; use super::*;
@ -112,24 +131,16 @@ mod errors {
} }
#[test] #[test]
fn from_duplicate_table_name() { fn from_database_error() {
let data = rand_str(); let error = DBError::DuplicateTable(rand_str());
let etype = ErrorType::TableAddFieldDuplicate(data.clone()); let err = MTTError::from(error.clone());
let result = MTTError::from(etype); assert_eq!(err.to_string(), ErrorType::DBErr(error).to_string());
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)
);
} }
#[test] #[test]
fn error_type_strings() { fn from_table_error() {
let data = rand_str(); let error = TBLError::InvalidField(rand_str());
let etype = ErrorType::TableRecordInvalidFieldName(data.clone()); let err = MTTError::from(error.clone());
assert_eq!(etype.to_string(), format!("invalid field name '{}'", data)); assert_eq!(err.to_string(), ErrorType::TBLErr(error).to_string());
} }
} }