From 656aa69bfdcbc1cbf18d5e2968c367c0ea070b46 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 00:29:56 +0100 Subject: [PATCH] Implement Store::persist() --- src/storage/mod.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index b35fa41f..998bba95 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -1,6 +1,9 @@ use std::rc::Rc; use std::cell::RefCell; use std::collections::HashMap; +use std::fs::File as FSFile; +use std::ops::Deref; +use std::io::Write; pub mod path; pub mod file; @@ -8,6 +11,7 @@ pub mod parser; pub mod json; use module::Module; +use runtime::Runtime; use storage::file::File; use storage::file::id::FileID; use storage::file::id_type::FileIDType; @@ -110,8 +114,29 @@ impl Store { self.put_in_cache(f) } - pub fn persist(&self, file: &File) -> bool { - unimplemented!() + pub fn persist(&self, + storepath: String, + p: &Parser, + f: Rc>) -> bool + where HP: FileHeaderParser + { + let file = f.deref().borrow(); + let text = p.write(file.contents()); + if text.is_err() { + error!("Error: {}", text.err().unwrap()); + return false; + } + + let path = { + let ids : String = file.id().clone().into(); + format!("{}/{}-{}.imag", storepath, file.owning_module_name, ids) + }; + + FSFile::create(&path).map(|mut fsfile| { + fsfile.write_all(&text.unwrap().clone().into_bytes()[..]) + }).map_err(|writeerr| { + debug!("Could not create file at '{}'", path); + }).and(Ok(true)).unwrap() } pub fn load(&self, id: &FileID) -> Option>> {