created a tablerow struct
This commit is contained in:
parent
3897781f9c
commit
1344f1580d
55
src/collection.rs
Normal file
55
src/collection.rs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde::de::DeserializeOwned;
|
||||||
|
use bson::{bson, Bson, doc, Document};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufReader, Seek, Write, Read};
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
|
||||||
|
pub struct Collection<T>
|
||||||
|
{
|
||||||
|
pub entries: Vec<T>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Collection<T>{
|
||||||
|
fn get(&self) -> Option<&T>{
|
||||||
|
self.entries.get(0)
|
||||||
|
}
|
||||||
|
fn new(&mut self, entries: Vec<T>){
|
||||||
|
self.entries = entries;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Collection<T>
|
||||||
|
where
|
||||||
|
T : Serialize
|
||||||
|
{
|
||||||
|
pub fn save<W>(&self, mut writer : W) -> Result<(), Box<dyn Error>>
|
||||||
|
where W : Write {
|
||||||
|
for a in self.entries.iter(){
|
||||||
|
let d = bson::to_document(a)?;
|
||||||
|
d.to_writer(&mut writer)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Collection<T>
|
||||||
|
where
|
||||||
|
T: DeserializeOwned
|
||||||
|
{
|
||||||
|
pub fn load(&mut self, mut file: File) -> Result<(), Box<dyn Error>>{
|
||||||
|
let n = file.metadata()?.len();
|
||||||
|
let mut reader = BufReader::new(file);
|
||||||
|
let mut current_position = reader.stream_position()?;
|
||||||
|
while current_position < n{
|
||||||
|
let d = Document::from_reader(&mut reader)?;
|
||||||
|
let h: T = bson::from_document(d)?;
|
||||||
|
self.entries.push(h);
|
||||||
|
current_position = reader.stream_position()?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
2
src/lib.rs
Normal file
2
src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod collection;
|
||||||
|
pub mod table;
|
||||||
58
src/main.rs
58
src/main.rs
@ -1,52 +1,26 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use serde::de::DeserializeOwned;
|
|
||||||
use bson::{bson, Bson, doc, Document};
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufReader, Seek, Write, Read};
|
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
struct Collection<T>
|
mod collection;
|
||||||
{
|
|
||||||
entries: Vec<T>
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct Potato {
|
||||||
|
bob: f64
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Collection<T>{
|
fn main() {
|
||||||
fn get(&self) -> Option<&T>{
|
// let mut peeps: collection::Collection<Potato> = collection::Collection{entries: Vec::new()};
|
||||||
self.entries.get(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Collection<T>
|
// peeps.entries.push(Potato{bob:12.0});
|
||||||
where
|
// peeps.entries.push(Potato{bob:42.0});
|
||||||
T : Serialize
|
// let mut file = File::create("test.bson").unwrap();
|
||||||
{
|
// peeps.save(file).unwrap();
|
||||||
fn save<W>(&self, mut writer : W) -> Result<(), Box<dyn Error>>
|
|
||||||
where W : Write {
|
|
||||||
for a in self.entries.iter(){
|
|
||||||
let d = bson::to_document(a)?;
|
|
||||||
d.to_writer(&mut writer)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
let mut pops: collection::Collection<Potato> = collection::Collection { entries: Vec::new() };
|
||||||
|
|
||||||
impl<T> Collection<T>
|
let mut file = File::open("test.bson").unwrap();
|
||||||
where
|
pops.load(file).unwrap();
|
||||||
T: DeserializeOwned
|
|
||||||
{
|
|
||||||
fn load(&mut self, mut file: File) -> Result<(), Box<dyn Error>>{
|
|
||||||
let n = file.metadata()?.len();
|
|
||||||
let mut reader = BufReader::new(file);
|
|
||||||
let mut current_position = reader.stream_position()?;
|
|
||||||
while current_position < n{
|
|
||||||
let d = Document::from_reader(&mut reader)?;
|
|
||||||
let h: T = bson::from_document(d)?;
|
|
||||||
self.entries.push(h);
|
|
||||||
current_position = reader.stream_position()?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
|
|
||||||
}
|
println!("blah");
|
||||||
}
|
}
|
||||||
25
src/table.rs
Normal file
25
src/table.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use serde::{Serialize};
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use crate::collection::Collection;
|
||||||
|
|
||||||
|
struct TableRow {
|
||||||
|
id: u32,
|
||||||
|
name: String,
|
||||||
|
num_entries: u32,
|
||||||
|
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();
|
||||||
|
entry.save(file)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user