diff --git a/src/collection.rs b/src/collection.rs new file mode 100644 index 0000000..83112d4 --- /dev/null +++ b/src/collection.rs @@ -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 +{ + pub entries: Vec +} + +impl Collection{ + fn get(&self) -> Option<&T>{ + self.entries.get(0) + } + fn new(&mut self, entries: Vec){ + self.entries = entries; + } +} + +impl Collection + where + T : Serialize +{ + pub fn save(&self, mut writer : W) -> Result<(), Box> + where W : Write { + for a in self.entries.iter(){ + let d = bson::to_document(a)?; + d.to_writer(&mut writer)?; + } + Ok(()) + } + +} + +impl Collection +where +T: DeserializeOwned +{ + pub fn load(&mut self, mut file: File) -> Result<(), Box>{ + 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(()) + + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..2e0a3d8 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod collection; +pub mod table; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0cad77d..5375f22 100644 --- a/src/main.rs +++ b/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::io::{BufReader, Seek, Write, Read}; -use std::error::Error; +use serde::{Deserialize, Serialize}; -struct Collection -{ - entries: Vec +mod collection; + +#[derive(Serialize, Deserialize)] +struct Potato { + bob: f64 } -impl Collection{ - fn get(&self) -> Option<&T>{ - self.entries.get(0) - } -} +fn main() { + // let mut peeps: collection::Collection = collection::Collection{entries: Vec::new()}; -impl Collection - where - T : Serialize -{ - fn save(&self, mut writer : W) -> Result<(), Box> - where W : Write { - for a in self.entries.iter(){ - let d = bson::to_document(a)?; - d.to_writer(&mut writer)?; - } - Ok(()) - } + // 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 = collection::Collection { entries: Vec::new() }; -impl Collection -where -T: DeserializeOwned -{ - fn load(&mut self, mut file: File) -> Result<(), Box>{ - 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(()) + let mut file = File::open("test.bson").unwrap(); + pops.load(file).unwrap(); - } + println!("blah"); } \ No newline at end of file diff --git a/src/table.rs b/src/table.rs new file mode 100644 index 0000000..2be1873 --- /dev/null +++ b/src/table.rs @@ -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(&mut self, id : u32, name: String, entry: Collection) -> Result<(), Box>{ + 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(()) + } +} \ No newline at end of file