From 78741c4ff85f9c83293d0eaeb430eb7ee8d2aad0 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 11 Dec 2024 08:05:29 -0500 Subject: [PATCH] Moved database into a separate file. --- src/data/database.rs | 95 +++++++++++++++++++++++++++++++++++++++++++ src/data/mod.rs | 97 +------------------------------------------- src/error.rs | 2 +- 3 files changed, 97 insertions(+), 97 deletions(-) create mode 100644 src/data/database.rs diff --git a/src/data/database.rs b/src/data/database.rs new file mode 100644 index 0000000..d3b4b81 --- /dev/null +++ b/src/data/database.rs @@ -0,0 +1,95 @@ +use crate::{ + data::table::Table, + error::{ErrorType, MTTError}, +}; +use std::{collections::HashMap, fmt, ops::Deref}; + +#[derive(Debug, Clone)] +pub enum DBError { + DuplicateTable(String), +} + +impl fmt::Display for DBError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + DBError::DuplicateTable(data) => write!(f, "'{}' already exists", data), + } + } +} + +#[cfg(test)] +mod errora { + use super::*; + + #[test] + fn duplicate_table() { + let name = "fred"; + let err = DBError::DuplicateTable(name.to_string()); + assert_eq!(err.to_string(), format!("'{}' already exists", name)); + } +} + +struct Database { + tables: HashMap, +} + +impl Database { + fn new() -> Self { + Self { + tables: HashMap::new(), + } + } + + fn add_table(&mut self, name: &str, table: Table) -> Result<(), MTTError> { + match self.tables.get(name) { + Some(_) => { + let err = DBError::DuplicateTable(name.to_string()); + return Err(err.into()); + } + None => {} + } + self.tables.insert(name.to_string(), table); + Ok(()) + } +} + +impl Deref for Database { + type Target = HashMap; + + fn deref(&self) -> &Self::Target { + &self.tables + } +} + +#[cfg(test)] +mod databases { + use super::*; + + #[test] + fn create_new_database() { + let db = Database::new(); + assert_eq!(db.len(), 0); + } + + #[test] + fn add_table() { + let mut db = Database::new(); + let tbl = Table::new(); + let name = "Something"; + db.add_table(name, tbl).unwrap(); + assert_eq!(db.len(), 1); + } + + #[test] + fn no_duplicate_names() { + let mut db = Database::new(); + let tbl1 = Table::new(); + let tbl2 = Table::new(); + let name = "Something"; + db.add_table(name, tbl1).unwrap(); + match db.add_table(name, tbl2) { + Ok(_) => unreachable!("Should have been an error"), + Err(err) => {} + } + } +} diff --git a/src/data/mod.rs b/src/data/mod.rs index e8578ea..45bf7b5 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -1,99 +1,4 @@ +pub mod database; pub mod id; mod record; pub mod table; - -use crate::{ - data::table::Table, - error::{ErrorType, MTTError}, -}; -use std::{collections::HashMap, fmt, ops::Deref}; - -#[derive(Debug, Clone)] -pub enum DBError { - DuplicateTable(String), -} - -impl fmt::Display for DBError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - DBError::DuplicateTable(data) => write!(f, "'{}' already exists", data), - } - } -} - -#[cfg(test)] -mod errora { - use super::*; - - #[test] - fn duplicate_table() { - let name = "fred"; - let err = DBError::DuplicateTable(name.to_string()); - assert_eq!(err.to_string(), format!("'{}' already exists", name)); - } -} - -struct Database { - tables: HashMap, -} - -impl Database { - fn new() -> Self { - Self { - tables: HashMap::new(), - } - } - - fn add_table(&mut self, name: &str, table: Table) -> Result<(), MTTError> { - match self.tables.get(name) { - Some(_) => { - let err = DBError::DuplicateTable(name.to_string()); - return Err(err.into()); - } - None => {} - } - self.tables.insert(name.to_string(), table); - Ok(()) - } -} - -impl Deref for Database { - type Target = HashMap; - - fn deref(&self) -> &Self::Target { - &self.tables - } -} - -#[cfg(test)] -mod databases { - use super::*; - - #[test] - fn create_new_database() { - let db = Database::new(); - assert_eq!(db.len(), 0); - } - - #[test] - fn add_table() { - let mut db = Database::new(); - let tbl = Table::new(); - let name = "Something"; - db.add_table(name, tbl).unwrap(); - assert_eq!(db.len(), 1); - } - - #[test] - fn no_duplicate_names() { - let mut db = Database::new(); - let tbl1 = Table::new(); - let tbl2 = Table::new(); - let name = "Something"; - db.add_table(name, tbl1).unwrap(); - match db.add_table(name, tbl2) { - Ok(_) => unreachable!("Should have been an error"), - Err(err) => {} - } - } -} diff --git a/src/error.rs b/src/error.rs index 1d6fdfe..0e7660c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use crate::data::{id::IDError, table::TBLError, DBError}; +use crate::data::{database::DBError, id::IDError, table::TBLError}; use std::{error::Error, fmt}; #[derive(Debug)]