Switched Universe to Global.
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit
This commit is contained in:
parent
3868ab44dd
commit
4a0ae807b8
@ -7,14 +7,14 @@ use crate::{data::database::Database, error::MTTError};
|
|||||||
use std::{collections::HashMap, fmt, ops::Deref};
|
use std::{collections::HashMap, fmt, ops::Deref};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum UniError {
|
pub enum GblError {
|
||||||
DuplicateDB(String),
|
DuplicateDB(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for UniError {
|
impl fmt::Display for GblError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
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]
|
#[test]
|
||||||
fn duplicate_database() {
|
fn duplicate_database() {
|
||||||
let name = "name";
|
let name = "name";
|
||||||
let err = UniError::DuplicateDB(name.to_string());
|
let err = GblError::DuplicateDB(name.to_string());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
err.to_string(),
|
err.to_string(),
|
||||||
format!("database '{}' already exists", name)
|
format!("database '{}' already exists", name)
|
||||||
@ -34,11 +34,11 @@ mod errors {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Universe {
|
struct Global {
|
||||||
databases: HashMap<String, Database>,
|
databases: HashMap<String, Database>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Universe {
|
impl Global {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
databases: HashMap::new(),
|
databases: HashMap::new(),
|
||||||
@ -48,7 +48,7 @@ impl Universe {
|
|||||||
fn add_database(&mut self, name: &str, db: Database) -> Result<(), MTTError> {
|
fn add_database(&mut self, name: &str, db: Database) -> Result<(), MTTError> {
|
||||||
match self.databases.get(name) {
|
match self.databases.get(name) {
|
||||||
Some(_) => {
|
Some(_) => {
|
||||||
let err = UniError::DuplicateDB(name.to_string());
|
let err = GblError::DuplicateDB(name.to_string());
|
||||||
Err(err.into())
|
Err(err.into())
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
@ -59,7 +59,7 @@ impl Universe {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for Universe {
|
impl Deref for Global {
|
||||||
type Target = HashMap<String, Database>;
|
type Target = HashMap<String, Database>;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
@ -68,31 +68,31 @@ impl Deref for Universe {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod universes {
|
mod gblverses {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_universe() {
|
fn create_gblverse() {
|
||||||
let uni = Universe::new();
|
let gbl = Global::new();
|
||||||
assert_eq!(uni.len(), 0);
|
assert_eq!(gbl.len(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add_database() {
|
fn add_database() {
|
||||||
let mut uni = Universe::new();
|
let mut gbl = Global::new();
|
||||||
let db = Database::new();
|
let db = Database::new();
|
||||||
uni.add_database("barney", db).unwrap();
|
gbl.add_database("barney", db).unwrap();
|
||||||
assert_eq!(uni.len(), 1);
|
assert_eq!(gbl.len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_duplicate_dbs() {
|
fn no_duplicate_dbs() {
|
||||||
let mut uni = Universe::new();
|
let mut gbl = Global::new();
|
||||||
let db1 = Database::new();
|
let db1 = Database::new();
|
||||||
let db2 = Database::new();
|
let db2 = Database::new();
|
||||||
let name = "duplicate";
|
let name = "duplicate";
|
||||||
uni.add_database("barney", db2).unwrap();
|
gbl.add_database("barney", db2).unwrap();
|
||||||
match uni.add_database("barney", db1) {
|
match gbl.add_database("barney", db1) {
|
||||||
Ok(_) => unreachable!("should have been an error"),
|
Ok(_) => unreachable!("should have been an error"),
|
||||||
Err(err) => {}
|
Err(err) => {}
|
||||||
}
|
}
|
||||||
|
22
src/error.rs
22
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};
|
use std::{error::Error, fmt};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -6,7 +6,7 @@ pub enum ErrorType {
|
|||||||
DBErr(DBError),
|
DBErr(DBError),
|
||||||
IDErr(IDError),
|
IDErr(IDError),
|
||||||
TBLErr(TBLError),
|
TBLErr(TBLError),
|
||||||
UniErr(UniError),
|
GblErr(GblError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<DBError> for ErrorType {
|
impl From<DBError> for ErrorType {
|
||||||
@ -27,9 +27,9 @@ impl From<TBLError> for ErrorType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<UniError> for ErrorType {
|
impl From<GblError> for ErrorType {
|
||||||
fn from(value: UniError) -> Self {
|
fn from(value: GblError) -> Self {
|
||||||
ErrorType::UniErr(value)
|
ErrorType::GblErr(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ impl fmt::Display for ErrorType {
|
|||||||
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::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]
|
#[test]
|
||||||
fn universal_error() {
|
fn universal_error() {
|
||||||
let err = UniError::DuplicateDB("bad".to_string());
|
let err = GblError::DuplicateDB("bad".to_string());
|
||||||
let result = ErrorType::from(err.clone());
|
let result = ErrorType::from(err.clone());
|
||||||
assert_eq!(result.to_string(), format!("global: {}", err));
|
assert_eq!(result.to_string(), format!("global: {}", err));
|
||||||
}
|
}
|
||||||
@ -124,8 +124,8 @@ impl From<TBLError> for MTTError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<UniError> for MTTError {
|
impl From<GblError> for MTTError {
|
||||||
fn from(value: UniError) -> Self {
|
fn from(value: GblError) -> Self {
|
||||||
Self { err: value.into() }
|
Self { err: value.into() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -166,8 +166,8 @@ mod errors {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_global_error() {
|
fn from_global_error() {
|
||||||
let error = UniError::DuplicateDB(rand_str());
|
let error = GblError::DuplicateDB(rand_str());
|
||||||
let err = MTTError::from(error.clone());
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user