fixed warnings
This commit is contained in:
parent
862d1aae2a
commit
d3600c46da
@ -41,7 +41,7 @@ impl<T> Collection<T>
|
|||||||
where
|
where
|
||||||
T: DeserializeOwned
|
T: DeserializeOwned
|
||||||
{
|
{
|
||||||
pub fn load(mut file: File) -> Result<Collection<T>, Box<dyn Error>>{
|
pub fn load(file: File) -> Result<Collection<T>, Box<dyn Error>>{
|
||||||
let n = file.metadata()?.len();
|
let n = file.metadata()?.len();
|
||||||
let mut reader = BufReader::new(file);
|
let mut reader = BufReader::new(file);
|
||||||
let mut current_position = reader.stream_position()?;
|
let mut current_position = reader.stream_position()?;
|
||||||
|
|||||||
13
src/table.rs
13
src/table.rs
@ -1,4 +1,3 @@
|
|||||||
use core::fmt;
|
|
||||||
use std::{fs::{File,remove_file}, io::{Seek, BufReader}};
|
use std::{fs::{File,remove_file}, io::{Seek, BufReader}};
|
||||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
@ -21,7 +20,7 @@ impl TableRow
|
|||||||
{
|
{
|
||||||
fn new<T:Serialize>(id: usize, name: Uuid, entry: Collection<T>, file_pos: u64) -> 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 path = format!("{}.bson", name);
|
||||||
let mut file = File::create(&path)?;
|
let file = File::create(&path)?;
|
||||||
entry.save(file)?;
|
entry.save(file)?;
|
||||||
Ok(
|
Ok(
|
||||||
TableRow { id: id, name: name, num_entries: entry.entries.len(), path: path, file_pos: file_pos }
|
TableRow { id: id, name: name, num_entries: entry.entries.len(), path: path, file_pos: file_pos }
|
||||||
@ -87,7 +86,7 @@ impl Table {
|
|||||||
|
|
||||||
pub fn add_to_collection<T:Serialize + DeserializeOwned>(&mut self, collection_id : usize, object: T) -> Result<(), Box<dyn Error>>{
|
pub fn add_to_collection<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.
|
// 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 selected_row = self.get_row(collection_id);
|
||||||
let mut selected_collection: Collection<T> = Collection::load(File::open(&selected_row.path)?)?;
|
let mut selected_collection: Collection<T> = Collection::load(File::open(&selected_row.path)?)?;
|
||||||
remove_file(&selected_row.path)?;
|
remove_file(&selected_row.path)?;
|
||||||
selected_collection.entries.push(object);
|
selected_collection.entries.push(object);
|
||||||
@ -99,7 +98,7 @@ impl Table {
|
|||||||
|
|
||||||
pub fn update<T:Serialize>(&mut self, id: usize, entry: Collection<T>)-> Result<(), Box<dyn Error>>{
|
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.
|
// 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);
|
let selected_row = self.get_row(id);
|
||||||
remove_file(&selected_row.path)?;
|
remove_file(&selected_row.path)?;
|
||||||
selected_row.num_entries = entry.entries.len();
|
selected_row.num_entries = entry.entries.len();
|
||||||
entry.save(File::create(&selected_row.path)?)?;
|
entry.save(File::create(&selected_row.path)?)?;
|
||||||
@ -107,7 +106,7 @@ impl Table {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getRow(&mut self, id: usize) -> &mut TableRow{
|
fn get_row(&mut self, id: usize) -> &mut TableRow{
|
||||||
let row_index = self.rows.iter().position(move |row |{
|
let row_index = self.rows.iter().position(move |row |{
|
||||||
row.id == id
|
row.id == id
|
||||||
}).expect("Failed to find that row in table.");
|
}).expect("Failed to find that row in table.");
|
||||||
@ -115,8 +114,8 @@ impl Table {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get<T:DeserializeOwned>(&mut self, id: usize)-> Result<Collection<T>, Box<dyn Error>>{
|
pub fn get<T:DeserializeOwned>(&mut self, id: usize)-> Result<Collection<T>, Box<dyn Error>>{
|
||||||
let table_row = self.getRow(id);
|
let table_row = self.get_row(id);
|
||||||
let mut file = File::open(&table_row.path)?;
|
let file = File::open(&table_row.path)?;
|
||||||
Collection::load(file)
|
Collection::load(file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user