created fn for add and update rows in table
This commit is contained in:
parent
cfec309217
commit
e760397494
@ -22,7 +22,7 @@ fn main() {
|
||||
|
||||
let mut table = Table::init().unwrap();
|
||||
|
||||
let mut peeps: Collection<Potato> = table.get(1).unwrap();
|
||||
table.addToCollection(1, Potato{bob: 66.0}).unwrap();
|
||||
|
||||
println!("blah");
|
||||
}
|
||||
59
src/table.rs
59
src/table.rs
@ -1,29 +1,30 @@
|
||||
use std::{fs::File, io::{Seek, BufReader}};
|
||||
use core::fmt;
|
||||
use std::{fs::{File,remove_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)]
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
struct TableRow {
|
||||
id: usize,
|
||||
name: Uuid,
|
||||
num_entries: usize,
|
||||
path: String
|
||||
path: String,
|
||||
file_pos: u64
|
||||
}
|
||||
|
||||
impl TableRow
|
||||
{
|
||||
fn new<T:Serialize>(id: usize, name: Uuid, entry: Collection<T>) -> Result<TableRow, Box<dyn Error>>{
|
||||
fn new<T:Serialize>(id: usize, name: Uuid, entry: Collection<T>, file_pos: u64) -> Result<TableRow, Box<dyn Error>>{
|
||||
let path = format!("{}.bson", name);
|
||||
let mut file = File::create(&path)?;
|
||||
entry.save(file)?;
|
||||
Ok(
|
||||
TableRow { id: id, name: name, num_entries: entry.entries.len(), path: path }
|
||||
TableRow { id: id, name: name, num_entries: entry.entries.len(), path: path, file_pos: file_pos }
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -55,26 +56,54 @@ impl 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 new_pos = y.metadata()?.len();
|
||||
let table_row = TableRow::new(
|
||||
self.rows.len()+1,
|
||||
Uuid::new_v4(),
|
||||
entry,
|
||||
new_pos
|
||||
)?;
|
||||
let x = serde_json::to_value(&table_row)?;
|
||||
serde_json::to_writer(&y, &x)?;
|
||||
self.rows.push(table_row);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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.");
|
||||
pub fn addToCollection<T:Serialize + DeserializeOwned>(&mut self, collection_id : usize, object: T) -> Result<(), Box<dyn Error>>{
|
||||
// TODO: this currently wipes out files and rewrites the entirety of them. Update to only change the sections that need to change.
|
||||
let selected_row = self.getRow(collection_id);
|
||||
let mut selected_collection: Collection<T> = Collection::load(File::open(&selected_row.path)?)?;
|
||||
remove_file(&selected_row.path)?;
|
||||
selected_collection.entries.push(object);
|
||||
selected_row.num_entries = selected_collection.entries.len();
|
||||
selected_collection.save(File::create(&selected_row.path)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update<T:Serialize>(&mut self, id: usize, entry: Collection<T>)-> Result<(), Box<dyn Error>>{
|
||||
// TODO: this currently wipes out files and rewrites the entirety of them. Update to only change the sections that need to change.
|
||||
let selected_row = self.getRow(id);
|
||||
remove_file(&selected_row.path)?;
|
||||
selected_row.num_entries = entry.entries.len();
|
||||
entry.save(File::create(&selected_row.path)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn getRow(&mut self, id: usize) -> &mut TableRow{
|
||||
let row_index = self.rows.iter().position(move |row |{
|
||||
row.id == id
|
||||
}).expect("Failed to find that row in table.");
|
||||
&mut self.rows[row_index]
|
||||
}
|
||||
|
||||
pub fn get<T:DeserializeOwned>(&mut self, id: usize)-> Result<Collection<T>, Box<dyn Error>>{
|
||||
let table_row = self.getRow(id);
|
||||
let mut file = File::open(&table_row.path)?;
|
||||
Collection::load(file)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user