morethantext/src/data/mod.rs

100 lines
2.1 KiB
Rust
Raw Normal View History

2024-11-28 10:43:56 -05:00
pub mod id;
2024-11-12 10:26:21 -05:00
mod record;
2024-11-28 10:43:56 -05:00
mod table;
2024-11-09 07:56:49 -05:00
2024-11-25 09:12:31 -05:00
use crate::{
2024-11-28 10:43:56 -05:00
data::table::Table,
2024-11-25 09:12:31 -05:00
error::{ErrorType, MTTError},
};
2024-11-28 10:43:56 -05:00
use std::{collections::HashMap, fmt, ops::Deref};
2024-11-09 07:56:49 -05:00
2024-11-28 10:43:56 -05:00
#[derive(Debug, Clone)]
pub enum DBError {
DuplicateTable(String),
2024-11-25 09:12:31 -05:00
}
2024-11-28 10:43:56 -05:00
impl fmt::Display for DBError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DBError::DuplicateTable(data) => write!(f, "'{}' already exists", data),
}
2024-11-09 07:56:49 -05:00
}
2024-11-28 10:43:56 -05:00
}
#[cfg(test)]
mod errora {
use super::*;
2024-11-25 09:12:31 -05:00
2024-11-28 10:43:56 -05:00
#[test]
fn duplicate_table() {
let name = "fred";
let err = DBError::DuplicateTable(name.to_string());
assert_eq!(err.to_string(), format!("'{}' already exists", name));
2024-11-25 09:12:31 -05:00
}
2024-11-09 07:56:49 -05:00
}
2024-11-28 10:43:56 -05:00
struct Database {
tables: HashMap<String, Table>,
2024-11-12 10:26:21 -05:00
}
2024-11-28 10:43:56 -05:00
impl Database {
2024-11-12 10:26:21 -05:00
fn new() -> Self {
Self {
2024-11-28 10:43:56 -05:00
tables: HashMap::new(),
2024-11-12 10:26:21 -05:00
}
}
2024-11-28 10:43:56 -05:00
fn add_table(&mut self, name: &str, table: Table) -> Result<(), MTTError> {
match self.tables.get(name) {
2024-11-25 09:12:31 -05:00
Some(_) => {
2024-11-28 10:43:56 -05:00
let error = ErrorType::TableRecordInvalidFieldName(name.to_string());
return Err(MTTError::new(error));
2024-11-15 13:37:11 -05:00
}
2024-11-28 10:43:56 -05:00
None => {}
2024-11-12 10:26:21 -05:00
}
2024-11-28 10:43:56 -05:00
self.tables.insert(name.to_string(), table);
Ok(())
2024-11-12 10:26:21 -05:00
}
2024-11-28 10:43:56 -05:00
}
2024-11-25 09:12:31 -05:00
2024-11-28 10:43:56 -05:00
impl Deref for Database {
type Target = HashMap<String, Table>;
fn deref(&self) -> &Self::Target {
&self.tables
2024-11-25 09:12:31 -05:00
}
2024-11-12 10:26:21 -05:00
}
2024-11-09 07:56:49 -05:00
#[cfg(test)]
2024-11-28 10:43:56 -05:00
mod databases {
2024-11-09 07:56:49 -05:00
use super::*;
#[test]
2024-11-28 10:43:56 -05:00
fn create_new_database() {
let db = Database::new();
assert_eq!(db.len(), 0);
2024-11-25 09:12:31 -05:00
}
#[test]
2024-11-28 10:43:56 -05:00
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);
2024-11-25 09:12:31 -05:00
}
#[test]
2024-11-28 10:43:56 -05:00
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) => {}
2024-11-12 10:26:21 -05:00
}
2024-11-09 07:56:49 -05:00
}
}