From cfec3092173d1015fe90515376a8a8467c377d70 Mon Sep 17 00:00:00 2001 From: Liam Fitzpatrick Date: Thu, 11 Apr 2024 22:01:18 -0400 Subject: [PATCH] working table --- .vscode/launch.json | 7 ++++ Cargo.lock | 2 ++ Cargo.toml | 2 ++ src/collection.rs | 14 ++++---- src/main.rs | 18 +++++----- src/table.rs | 80 +++++++++++++++++++++++++++++++++++---------- 6 files changed, 92 insertions(+), 31 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5c7247b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 3c396d8..81c8302 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -153,6 +153,8 @@ version = "0.1.0" dependencies = [ "bson", "serde", + "serde_json", + "uuid", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c120343..7f752a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,5 @@ edition = "2021" [dependencies] bson = "2.10.0" serde = "1.0.197" +serde_json = "1.0.115" +uuid = "1.8.0" diff --git a/src/collection.rs b/src/collection.rs index 83112d4..91e63f2 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -15,8 +15,10 @@ impl Collection{ fn get(&self) -> Option<&T>{ self.entries.get(0) } - fn new(&mut self, entries: Vec){ - self.entries = entries; + fn new( entries: Vec) -> Collection{ + Collection{ + entries:entries + } } } @@ -39,17 +41,17 @@ impl Collection where T: DeserializeOwned { - pub fn load(&mut self, mut file: File) -> Result<(), Box>{ + pub fn load(mut file: File) -> Result, Box>{ let n = file.metadata()?.len(); let mut reader = BufReader::new(file); let mut current_position = reader.stream_position()?; + let mut entries = Vec::new(); while current_position < n{ let d = Document::from_reader(&mut reader)?; let h: T = bson::from_document(d)?; - self.entries.push(h); + entries.push(h); current_position = reader.stream_position()?; } - Ok(()) - + Ok(Collection::new( entries)) } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 5375f22..5c57294 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ use std::fs::File; +use metalize::table::Table; use serde::{Deserialize, Serialize}; - -mod collection; +use metalize::collection::Collection; #[derive(Serialize, Deserialize)] struct Potato { @@ -10,17 +10,19 @@ struct Potato { } fn main() { - // let mut peeps: collection::Collection = collection::Collection{entries: Vec::new()}; + // let mut peeps: Collection = Collection{entries: Vec::new()}; // peeps.entries.push(Potato{bob:12.0}); // peeps.entries.push(Potato{bob:42.0}); - // let mut file = File::create("test.bson").unwrap(); - // peeps.save(file).unwrap(); + - let mut pops: collection::Collection = collection::Collection { entries: Vec::new() }; + // let mut table = Table::init().unwrap(); - let mut file = File::open("test.bson").unwrap(); - pops.load(file).unwrap(); + // table.insert(peeps).unwrap(); + + let mut table = Table::init().unwrap(); + + let mut peeps: Collection = table.get(1).unwrap(); println!("blah"); } \ No newline at end of file diff --git a/src/table.rs b/src/table.rs index cc943be..cf3a553 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1,35 +1,81 @@ -use std::fs::File; -use serde::{Serialize}; +use std::{fs::File, io::{Seek, BufReader}}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::error::Error; +use uuid::Uuid; +use std::path::Path; +use serde_json; + use crate::collection::Collection; +#[derive(Serialize, Deserialize)] struct TableRow { - id: u32, - name: String, - num_entries: u32, + id: usize, + name: Uuid, + num_entries: usize, path: String } impl TableRow { - fn new(&mut self, id : u32, name: String, entry: Collection) -> Result<(), Box>{ - self.id = id; - self.name = name.clone(); - self.num_entries = u32::try_from(entry.entries.len()).unwrap(); - self.path = format!("{}.bson",name.clone()); - let mut file = File::create(self.path.clone()).unwrap(); + fn new(id: usize, name: Uuid, entry: Collection) -> Result>{ + let path = format!("{}.bson", name); + let mut file = File::create(&path)?; entry.save(file)?; - Table::insert(self); - Ok(()) + Ok( + TableRow { id: id, name: name, num_entries: entry.entries.len(), path: path } + ) } } -mod Table{ - use super::TableRow; +pub struct Table{ + rows: Vec +} +impl Table { - pub fn insert(row :&mut TableRow){ - + fn new() -> Table{ + Table{ rows:Vec::new()} + } + + pub fn init() -> Result>{ + let mut table = Table::new(); + + // check for existing table entries + let path = Path::new("metalize.table"); + if path.exists(){ + let file = File::open(path)?; + let n = file.metadata()?.len(); + let mut file = BufReader::new(file); + while file.stream_position()? < n { + table.rows.push(serde_json::from_reader(&mut file)?); + } + } + Ok(table) + } + + pub fn insert(&mut self, entry: Collection) -> Result<(), Box> { + let table_row = TableRow::new( + self.rows.len()+1, + Uuid::new_v4(), + entry + )?; + let path = Path::new("metalize.table"); + + let y = if path.exists() { + File::open(path)? + } else { + File::create(path)? + }; + let x = serde_json::to_value(&table_row)?; + serde_json::to_writer(&y, &x)?; + self.rows.push(table_row); + Ok(()) + } + + pub fn get(&self, id: usize)-> Result, Box>{ + let table_row = self.rows.get(id-1).expect("Could not access that id."); + let mut file = File::open(&table_row.path)?; + Collection::load(file) } } \ No newline at end of file