working table

This commit is contained in:
Liam Fitzpatrick 2024-04-11 22:01:18 -04:00
parent fee17bb4ad
commit cfec309217
6 changed files with 92 additions and 31 deletions

7
.vscode/launch.json vendored Normal file
View File

@ -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": []
}

2
Cargo.lock generated
View File

@ -153,6 +153,8 @@ version = "0.1.0"
dependencies = [
"bson",
"serde",
"serde_json",
"uuid",
]
[[package]]

View File

@ -8,3 +8,5 @@ edition = "2021"
[dependencies]
bson = "2.10.0"
serde = "1.0.197"
serde_json = "1.0.115"
uuid = "1.8.0"

View File

@ -15,8 +15,10 @@ impl<T> Collection<T>{
fn get(&self) -> Option<&T>{
self.entries.get(0)
}
fn new(&mut self, entries: Vec<T>){
self.entries = entries;
fn new( entries: Vec<T>) -> Collection<T>{
Collection{
entries:entries
}
}
}
@ -39,17 +41,17 @@ impl<T> Collection<T>
where
T: DeserializeOwned
{
pub fn load(&mut self, mut file: File) -> Result<(), Box<dyn Error>>{
pub fn load(mut file: File) -> Result<Collection<T>, Box<dyn Error>>{
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))
}
}

View File

@ -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<Potato> = collection::Collection{entries: Vec::new()};
// let mut peeps: Collection<Potato> = 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<Potato> = collection::Collection { entries: Vec::new() };
let mut file = File::open("test.bson").unwrap();
pops.load(file).unwrap();
// let mut table = Table::init().unwrap();
// table.insert(peeps).unwrap();
let mut table = Table::init().unwrap();
let mut peeps: Collection<Potato> = table.get(1).unwrap();
println!("blah");
}

View File

@ -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<T:Serialize>(&mut self, id : u32, name: String, entry: Collection<T>) -> Result<(), Box<dyn Error>>{
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<T:Serialize>(id: usize, name: Uuid, entry: Collection<T>) -> Result<TableRow, Box<dyn Error>>{
let path = format!("{}.bson", name);
let mut file = File::create(&path)?;
entry.save(file)?;
Table::insert(self);
Ok(
TableRow { id: id, name: name, num_entries: entry.entries.len(), path: path }
)
}
}
pub struct Table{
rows: Vec<TableRow>
}
impl Table {
fn new() -> Table{
Table{ rows:Vec::new()}
}
pub fn init() -> Result<Table, Box<dyn Error>>{
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<T:Serialize>(&mut self, entry: Collection<T>) -> Result<(), Box<dyn Error>> {
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(())
}
}
mod Table{
use super::TableRow;
pub fn insert(row :&mut TableRow){
pub fn get<T:DeserializeOwned>(&self, id: usize)-> Result<Collection<T>, Box<dyn Error>>{
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)
}
}