From 8ea347bc98c00432778741c0374efd21f339797a Mon Sep 17 00:00:00 2001 From: Elias Renman Date: Sun, 7 May 2023 11:19:22 +0200 Subject: [PATCH] chore: formatting --- src/database/index.rs | 14 +++---- src/database/mod.rs | 3 +- src/database/row.rs | 14 +++---- src/database/table.rs | 81 +++++++++++++++++++++-------------------- src/main.rs | 6 +-- src/tests/mod.rs | 2 +- src/tests/table_test.rs | 4 +- 7 files changed, 59 insertions(+), 65 deletions(-) diff --git a/src/database/index.rs b/src/database/index.rs index b8eb463..0d5f26f 100644 --- a/src/database/index.rs +++ b/src/database/index.rs @@ -1,11 +1,9 @@ - use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize)] pub struct Index { - pk: u64, - key: String, - asd_sort: bool, - unique: bool, - -} \ No newline at end of file + pk: u64, + key: String, + asd_sort: bool, + unique: bool, +} diff --git a/src/database/mod.rs b/src/database/mod.rs index 5ba3513..c6b7498 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -60,10 +60,9 @@ mod tests { let db = table.unwrap(); let table = db.tables.get("Cats"); if table.is_none() { - panic!("Unable to find table"); + panic!("Unable to find table"); } - let row = table.unwrap().find_by_pk(&1u64); assert_eq!(row.is_ok(), true); diff --git a/src/database/row.rs b/src/database/row.rs index 1c507c0..7dc9d15 100644 --- a/src/database/row.rs +++ b/src/database/row.rs @@ -1,14 +1,14 @@ +use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::HashMap; -use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize)] pub struct Row { - pub columns: HashMap + pub columns: HashMap, } impl Row { - pub fn new(cols: HashMap) -> Row { - Row {columns: cols} - } -} \ No newline at end of file + pub fn new(cols: HashMap) -> Row { + Row { columns: cols } + } +} diff --git a/src/database/table.rs b/src/database/table.rs index 30131f0..85f3c30 100644 --- a/src/database/table.rs +++ b/src/database/table.rs @@ -1,50 +1,51 @@ - +use super::{index::Index, row::Row}; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, fs}; -use super::{index::Index, row::Row}; - -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize)] pub struct Table { - indexes: Vec, - rows: HashMap, - name: String, - pk_key: String, + indexes: Vec, + rows: HashMap, + name: String, + pk_key: String, } impl Table { - - pub fn new(name: &str, pk_key: &str) -> Table { - Table { indexes: vec![], rows: HashMap::new(), name: name.to_string(), pk_key: pk_key.to_string() } - } - - pub fn create_index(&mut self, index: Index) { - // TODO: Add row indexes to the index itself before pushing to the table - self.indexes.push(index) - } - - pub fn find_by_pk(&self, value: &u64) -> Result<&Row, &'static str> { - let row = self.rows.get(value); - if row.is_none() { - return Err("Row not found!"); - } - Ok(row.unwrap().clone()) - } - - pub fn insert_row(&mut self, row: Row) { - let key_option = row.columns.get(&self.pk_key); - - if key_option.is_none() { - panic!("Primary key not found on row to insert!"); - } - - let key = key_option.unwrap().as_u64(); - - if key.is_none() { - panic!("Primary key is not of type u64"); + pub fn new(name: &str, pk_key: &str) -> Table { + Table { + indexes: vec![], + rows: HashMap::new(), + name: name.to_string(), + pk_key: pk_key.to_string(), + } } - self.rows.insert(key.unwrap(), row); - } + pub fn create_index(&mut self, index: Index) { + // TODO: Add row indexes to the index itself before pushing to the table + self.indexes.push(index) + } + + pub fn find_by_pk(&self, value: &u64) -> Result<&Row, &'static str> { + let row = self.rows.get(value); + if row.is_none() { + return Err("Row not found!"); + } + Ok(row.unwrap().clone()) + } + + pub fn insert_row(&mut self, row: Row) { + let key_option = row.columns.get(&self.pk_key); + + if key_option.is_none() { + panic!("Primary key not found on row to insert!"); + } + + let key = key_option.unwrap().as_u64(); + + if key.is_none() { + panic!("Primary key is not of type u64"); + } + + self.rows.insert(key.unwrap(), row); + } } - diff --git a/src/main.rs b/src/main.rs index e7a37d8..6830a81 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ - macro_rules! hashmapJson { ($( $key: expr => $val: expr ),*) => {{ let mut map: ::std::collections::HashMap = ::std::collections::HashMap::new(); @@ -17,8 +16,7 @@ macro_rules! hashmap { mod database; -fn main() { -} +fn main() {} #[cfg(test)] -mod tests; \ No newline at end of file +mod tests; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 266aff8..876ff65 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1 +1 @@ -mod table_test; \ No newline at end of file +mod table_test; diff --git a/src/tests/table_test.rs b/src/tests/table_test.rs index 8ccc571..2081886 100644 --- a/src/tests/table_test.rs +++ b/src/tests/table_test.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests { - use crate::database::{table::Table, row::Row}; + use crate::database::{row::Row, table::Table}; #[test] fn should_find_two_rows() { @@ -29,6 +29,4 @@ mod tests { let row = table.find_by_pk(&3u64); assert_eq!(row.is_err(), true); } - - }