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(()) } }