working table
This commit is contained in:
parent
fee17bb4ad
commit
cfec309217
7
.vscode/launch.json
vendored
Normal file
7
.vscode/launch.json
vendored
Normal 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
2
Cargo.lock
generated
@ -153,6 +153,8 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bson",
|
"bson",
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@ -8,3 +8,5 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
bson = "2.10.0"
|
bson = "2.10.0"
|
||||||
serde = "1.0.197"
|
serde = "1.0.197"
|
||||||
|
serde_json = "1.0.115"
|
||||||
|
uuid = "1.8.0"
|
||||||
|
|||||||
@ -15,8 +15,10 @@ impl<T> Collection<T>{
|
|||||||
fn get(&self) -> Option<&T>{
|
fn get(&self) -> Option<&T>{
|
||||||
self.entries.get(0)
|
self.entries.get(0)
|
||||||
}
|
}
|
||||||
fn new(&mut self, entries: Vec<T>){
|
fn new( entries: Vec<T>) -> Collection<T>{
|
||||||
self.entries = entries;
|
Collection{
|
||||||
|
entries:entries
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,17 +41,17 @@ impl<T> Collection<T>
|
|||||||
where
|
where
|
||||||
T: DeserializeOwned
|
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 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()?;
|
||||||
|
let mut entries = Vec::new();
|
||||||
while current_position < n{
|
while current_position < n{
|
||||||
let d = Document::from_reader(&mut reader)?;
|
let d = Document::from_reader(&mut reader)?;
|
||||||
let h: T = bson::from_document(d)?;
|
let h: T = bson::from_document(d)?;
|
||||||
self.entries.push(h);
|
entries.push(h);
|
||||||
current_position = reader.stream_position()?;
|
current_position = reader.stream_position()?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(Collection::new( entries))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
18
src/main.rs
18
src/main.rs
@ -1,8 +1,8 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
|
use metalize::table::Table;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use metalize::collection::Collection;
|
||||||
mod collection;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Potato {
|
struct Potato {
|
||||||
@ -10,17 +10,19 @@ struct Potato {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
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:12.0});
|
||||||
// peeps.entries.push(Potato{bob:42.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();
|
// let mut table = Table::init().unwrap();
|
||||||
pops.load(file).unwrap();
|
|
||||||
|
// table.insert(peeps).unwrap();
|
||||||
|
|
||||||
|
let mut table = Table::init().unwrap();
|
||||||
|
|
||||||
|
let mut peeps: Collection<Potato> = table.get(1).unwrap();
|
||||||
|
|
||||||
println!("blah");
|
println!("blah");
|
||||||
}
|
}
|
||||||
84
src/table.rs
84
src/table.rs
@ -1,35 +1,81 @@
|
|||||||
use std::fs::File;
|
use std::{fs::File, io::{Seek, BufReader}};
|
||||||
use serde::{Serialize};
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use uuid::Uuid;
|
||||||
|
use std::path::Path;
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
|
|
||||||
use crate::collection::Collection;
|
use crate::collection::Collection;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
struct TableRow {
|
struct TableRow {
|
||||||
id: u32,
|
id: usize,
|
||||||
name: String,
|
name: Uuid,
|
||||||
num_entries: u32,
|
num_entries: usize,
|
||||||
path: String
|
path: String
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TableRow
|
impl TableRow
|
||||||
{
|
{
|
||||||
fn new<T:Serialize>(&mut self, id : u32, name: String, entry: Collection<T>) -> Result<(), Box<dyn Error>>{
|
fn new<T:Serialize>(id: usize, name: Uuid, entry: Collection<T>) -> Result<TableRow, Box<dyn Error>>{
|
||||||
self.id = id;
|
let path = format!("{}.bson", name);
|
||||||
self.name = name.clone();
|
let mut file = File::create(&path)?;
|
||||||
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();
|
|
||||||
entry.save(file)?;
|
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(())
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user