Got tabler holding a record.
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit

This commit is contained in:
2024-11-25 09:12:31 -05:00
parent 508b8269d0
commit e1aec8de28
4 changed files with 251 additions and 107 deletions

View File

@ -1,28 +1,42 @@
use std::{error::Error, fmt};
#[derive(Debug)]
enum ErrorType {
pub enum ErrorType {
FieldIDInvalid(String),
TableAddFieldDuplicate(String),
TableRecordInvalidFieldName(String),
}
impl fmt::Display for ErrorType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorType::FieldIDInvalid(data) => write!(f, "'{}' is not a valid uuid", data),
ErrorType::TableAddFieldDuplicate(data) => write!(f, "field '{}' already exists", data),
ErrorType::TableRecordInvalidFieldName(data) => {
write!(f, "invalid field name '{}'", data)
}
}
}
}
#[derive(Debug)]
struct MTTError {
pub struct MTTError {
err: ErrorType,
}
impl From<ErrorType> for MTTError {
fn from(value: ErrorType) -> Self {
MTTError::new(value)
}
}
impl MTTError {
fn new(err: ErrorType) -> Self {
Self {
err: err,
}
pub fn new(err: ErrorType) -> Self {
Self { err: err }
}
pub fn get_code(&self) -> &ErrorType {
&self.err
}
}
@ -37,15 +51,50 @@ impl fmt::Display for MTTError {
#[cfg(test)]
mod errors {
use super::*;
use rand::{distributions::Alphanumeric, Rng};
fn rand_str() -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(5)
.map(char::from)
.collect()
}
#[test]
fn get_error() {
let err = MTTError::new(ErrorType::TableAddFieldDuplicate("tester".to_string()));
assert_eq!(err.to_string(), "field 'tester' already exists");
assert!(err.source().is_none());
let err = MTTError::new(ErrorType::TableAddFieldDuplicate("other".to_string()));
assert_eq!(err.to_string(), "field 'other' already exists");
assert!(err.source().is_none());
fn from_invalid_id() {
let data = rand_str();
let etype = ErrorType::FieldIDInvalid(data.clone());
let result = MTTError::from(etype);
match result.get_code() {
ErrorType::FieldIDInvalid(txt) => assert_eq!(txt, &data),
_ => unreachable!("should have been ErrorType::FieldIDInvalid"),
}
assert_eq!(
result.to_string(),
format!("'{}' is not a valid uuid", data)
);
}
#[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)
);
}
#[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));
}
}