From a26471088d9fc57f4c36b75084cd45ecc2aebac8 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sun, 1 Dec 2024 23:00:07 -0500 Subject: [PATCH] completed error move. --- src/data/id.rs | 2 +- src/data/mod.rs | 6 ++--- src/data/table.rs | 60 +++++++++++++++++++++++++++++++++-------- src/error.rs | 69 +++++++++++++++++++++++++++-------------------- 4 files changed, 93 insertions(+), 44 deletions(-) diff --git a/src/data/id.rs b/src/data/id.rs index 5614cae..4529d3a 100644 --- a/src/data/id.rs +++ b/src/data/id.rs @@ -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"), }, } diff --git a/src/data/mod.rs b/src/data/mod.rs index 486acd1..e8578ea 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -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 => {} } diff --git a/src/data/table.rs b/src/data/table.rs index ee40540..df318ef 100644 --- a/src/data/table.rs +++ b/src/data/table.rs @@ -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"), + } } } } diff --git a/src/error.rs b/src/error.rs index c8a4954..b1642e2 100644 --- a/src/error.rs +++ b/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 for ErrorType { @@ -21,16 +20,18 @@ impl From for ErrorType { } } +impl From 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,14 +91,25 @@ impl fmt::Display for MTTError { } } -impl From for MTTError { - fn from(value: IDError) -> Self { - Self { - err: value.into(), - } +impl From for MTTError { + fn from(value: DBError) -> Self { + Self { err: value.into() } } } +impl From for MTTError { + fn from(value: IDError) -> Self { + Self { err: value.into() } + } +} + +impl From for MTTError { + fn from(value: TBLError) -> Self { + Self { err: value.into() } + } +} + + #[cfg(test)] mod errors { use super::*; @@ -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()); } }