From 4a0ae807b8214069e2aff9e0020bdec5188e05e0 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 19 Dec 2024 20:19:36 -0500 Subject: [PATCH] Switched Universe to Global. --- src/data/mod.rs | 36 ++++++++++++++++++------------------ src/error.rs | 22 +++++++++++----------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/data/mod.rs b/src/data/mod.rs index dbd0695..290ac3a 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -7,14 +7,14 @@ use crate::{data::database::Database, error::MTTError}; use std::{collections::HashMap, fmt, ops::Deref}; #[derive(Debug, Clone)] -pub enum UniError { +pub enum GblError { DuplicateDB(String), } -impl fmt::Display for UniError { +impl fmt::Display for GblError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - UniError::DuplicateDB(data) => write!(f, "database '{}' already exists", data), + GblError::DuplicateDB(data) => write!(f, "database '{}' already exists", data), } } } @@ -26,7 +26,7 @@ mod errors { #[test] fn duplicate_database() { let name = "name"; - let err = UniError::DuplicateDB(name.to_string()); + let err = GblError::DuplicateDB(name.to_string()); assert_eq!( err.to_string(), format!("database '{}' already exists", name) @@ -34,11 +34,11 @@ mod errors { } } -struct Universe { +struct Global { databases: HashMap, } -impl Universe { +impl Global { fn new() -> Self { Self { databases: HashMap::new(), @@ -48,7 +48,7 @@ impl Universe { fn add_database(&mut self, name: &str, db: Database) -> Result<(), MTTError> { match self.databases.get(name) { Some(_) => { - let err = UniError::DuplicateDB(name.to_string()); + let err = GblError::DuplicateDB(name.to_string()); Err(err.into()) } None => { @@ -59,7 +59,7 @@ impl Universe { } } -impl Deref for Universe { +impl Deref for Global { type Target = HashMap; fn deref(&self) -> &Self::Target { @@ -68,31 +68,31 @@ impl Deref for Universe { } #[cfg(test)] -mod universes { +mod gblverses { use super::*; #[test] - fn create_universe() { - let uni = Universe::new(); - assert_eq!(uni.len(), 0); + fn create_gblverse() { + let gbl = Global::new(); + assert_eq!(gbl.len(), 0); } #[test] fn add_database() { - let mut uni = Universe::new(); + let mut gbl = Global::new(); let db = Database::new(); - uni.add_database("barney", db).unwrap(); - assert_eq!(uni.len(), 1); + gbl.add_database("barney", db).unwrap(); + assert_eq!(gbl.len(), 1); } #[test] fn no_duplicate_dbs() { - let mut uni = Universe::new(); + let mut gbl = Global::new(); let db1 = Database::new(); let db2 = Database::new(); let name = "duplicate"; - uni.add_database("barney", db2).unwrap(); - match uni.add_database("barney", db1) { + gbl.add_database("barney", db2).unwrap(); + match gbl.add_database("barney", db1) { Ok(_) => unreachable!("should have been an error"), Err(err) => {} } diff --git a/src/error.rs b/src/error.rs index c16bb79..dd39241 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use crate::data::{database::DBError, id::IDError, table::TBLError, UniError}; +use crate::data::{database::DBError, id::IDError, table::TBLError, GblError}; use std::{error::Error, fmt}; #[derive(Debug)] @@ -6,7 +6,7 @@ pub enum ErrorType { DBErr(DBError), IDErr(IDError), TBLErr(TBLError), - UniErr(UniError), + GblErr(GblError), } impl From for ErrorType { @@ -27,9 +27,9 @@ impl From for ErrorType { } } -impl From for ErrorType { - fn from(value: UniError) -> Self { - ErrorType::UniErr(value) +impl From for ErrorType { + fn from(value: GblError) -> Self { + ErrorType::GblErr(value) } } @@ -39,7 +39,7 @@ impl fmt::Display for ErrorType { ErrorType::DBErr(data) => write!(f, "database: {}", data), ErrorType::IDErr(data) => write!(f, "id: {}", data), ErrorType::TBLErr(data) => write!(f, "table: {}", data), - ErrorType::UniErr(data) => write!(f, "global: {}", data), + ErrorType::GblErr(data) => write!(f, "global: {}", data), } } } @@ -71,7 +71,7 @@ mod errortypes { #[test] fn universal_error() { - let err = UniError::DuplicateDB("bad".to_string()); + let err = GblError::DuplicateDB("bad".to_string()); let result = ErrorType::from(err.clone()); assert_eq!(result.to_string(), format!("global: {}", err)); } @@ -124,8 +124,8 @@ impl From for MTTError { } } -impl From for MTTError { - fn from(value: UniError) -> Self { +impl From for MTTError { + fn from(value: GblError) -> Self { Self { err: value.into() } } } @@ -166,8 +166,8 @@ mod errors { #[test] fn from_global_error() { - let error = UniError::DuplicateDB(rand_str()); + let error = GblError::DuplicateDB(rand_str()); let err = MTTError::from(error.clone()); - assert_eq!(err.to_string(), ErrorType::UniErr(error).to_string()); + assert_eq!(err.to_string(), ErrorType::GblErr(error).to_string()); } }