From 4c252577f20a2f1d9d231522d1d49db59a3acd49 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:02:06 +0100 Subject: [PATCH 01/15] Derive: Hash for FileHash --- src/storage/file/hash.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/storage/file/hash.rs b/src/storage/file/hash.rs index 68b91b7d..a2f1f96a 100644 --- a/src/storage/file/hash.rs +++ b/src/storage/file/hash.rs @@ -2,12 +2,14 @@ use std::convert::{From, Into}; use std::error::Error; use std::fmt::{Debug, Display, Formatter}; use std::fmt; +use std::hash::Hash; use uuid::Uuid; #[derive(Clone)] #[derive(Debug)] #[derive(PartialEq)] #[derive(Eq)] +#[derive(Hash)] pub struct FileHash { hash: String, } From f60c540ee56f9009d51932de0be9ca8490eacd01 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:02:20 +0100 Subject: [PATCH 02/15] Derive: Hash for FileID --- src/storage/file/id.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/storage/file/id.rs b/src/storage/file/id.rs index 36f40cdb..457e80dd 100644 --- a/src/storage/file/id.rs +++ b/src/storage/file/id.rs @@ -2,6 +2,7 @@ use std::convert::{From, Into}; use std::error::Error; use std::fmt::{Debug, Display, Formatter}; use std::fmt; +use std::hash::Hash; use std::path::PathBuf; use std::result::Result; use std::str::FromStr; @@ -12,6 +13,7 @@ use storage::file::id_type::FileIDType; use storage::file::hash::FileHash; #[derive(Clone)] +#[derive(Hash)] pub struct FileID { id: FileHash, id_type: FileIDType, From 6676611f4bb4ccfc481bec7692d769e7299c56d5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:02:34 +0100 Subject: [PATCH 03/15] Derive Hash for FileIDType --- src/storage/file/id_type.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/storage/file/id_type.rs b/src/storage/file/id_type.rs index d13c8bab..474e12a5 100644 --- a/src/storage/file/id_type.rs +++ b/src/storage/file/id_type.rs @@ -1,12 +1,14 @@ use std::convert::{From, Into}; use std::error::Error; use std::str::FromStr; +use std::hash::Hash; #[derive(Debug)] #[derive(Clone)] #[derive(PartialEq)] #[derive(Eq)] // #[derive(Display)] +#[derive(Hash)] pub enum FileIDType { UUID, } From 736c3784a960d9cb77e2db998581bc6f3afab7c8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:13:54 +0100 Subject: [PATCH 04/15] Derive: Eq, PartialEq for FileID --- src/storage/file/id.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/storage/file/id.rs b/src/storage/file/id.rs index 457e80dd..bdfd9db4 100644 --- a/src/storage/file/id.rs +++ b/src/storage/file/id.rs @@ -14,6 +14,8 @@ use storage::file::hash::FileHash; #[derive(Clone)] #[derive(Hash)] +#[derive(Eq)] +#[derive(PartialEq)] pub struct FileID { id: FileHash, id_type: FileIDType, From 73c7281259a660acee436542931ffbbbed84f927 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:17:29 +0100 Subject: [PATCH 05/15] Make File members pub --- src/storage/file/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/storage/file/mod.rs b/src/storage/file/mod.rs index 59a2638c..0be95e62 100644 --- a/src/storage/file/mod.rs +++ b/src/storage/file/mod.rs @@ -24,10 +24,10 @@ use self::header::data::*; * internally until it is written to disk. */ pub struct File<'a> { - owning_module : &'a Module<'a>, - header : FileHeaderData, - data : String, - id : FileID, + pub owning_module : &'a Module<'a>, + pub header : FileHeaderData, + pub data : String, + pub id : FileID, } impl<'a> File<'a> { From cb2c512440f81fc1339acf836581a96400d6539b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:03:57 +0100 Subject: [PATCH 06/15] Add Store type --- src/storage/mod.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index e51307df..16fbe4f6 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -1,4 +1,26 @@ +use std::cell::RefCell; +use std::collections::HashMap; + pub mod path; pub mod file; pub mod parser; pub mod json; + +use storage::file::File; +use storage::file::id::FileID; + +type Cache<'a> = HashMap>>; + +pub struct Store<'a> { + cache : RefCell>, +} + +impl<'a> Store<'a> { + + pub fn new() -> Store<'a> { + Store { + cache: RefCell::new(HashMap::new()), + } + } + +} From c595f5a1e84a92f751a43f74c4c093b0395f2948 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:10:36 +0100 Subject: [PATCH 07/15] Move File-creation code to Store --- src/storage/file/mod.rs | 88 ----------------------------------------- src/storage/mod.rs | 70 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 88 deletions(-) diff --git a/src/storage/file/mod.rs b/src/storage/file/mod.rs index 0be95e62..21d5e92e 100644 --- a/src/storage/file/mod.rs +++ b/src/storage/file/mod.rs @@ -32,89 +32,6 @@ pub struct File<'a> { impl<'a> File<'a> { - pub fn new(module: &'a Module<'a>) -> File<'a> { - let f = File { - owning_module: module, - header: FileHeaderData::Null, - data: String::from(""), - id: File::get_new_file_id(), - }; - debug!("Create new File object: {:?}", f); - f - } - - pub fn from_parser_result(module: &'a Module<'a>, id: FileID, header: FileHeaderData, data: String) -> File<'a> { - let f = File { - owning_module: module, - header: header, - data: data, - id: id, - }; - debug!("Create new File object from parser result: {:?}", f); - f - } - - pub fn new_with_header(module: &'a Module<'a>, h: FileHeaderData) -> File<'a> { - let f = File { - owning_module: module, - header: h, - data: String::from(""), - id: File::get_new_file_id(), - }; - debug!("Create new File object with header: {:?}", f); - f - } - - pub fn new_with_data(module: &'a Module<'a>, d: String) -> File<'a> { - let f = File { - owning_module: module, - header: FileHeaderData::Null, - data: d, - id: File::get_new_file_id(), - }; - debug!("Create new File object with data: {:?}", f); - f - } - - pub fn new_with_content(module: &'a Module<'a>, h: FileHeaderData, d: String) -> File<'a> { - let f = File { - owning_module: module, - header: h, - data: d, - id: File::get_new_file_id(), - }; - debug!("Create new File object with content: {:?}", f); - f - } - - /** - * Call editor on the file - * - * Return true if exit code from editor was good (and content was changed), false otherwise. - */ - pub fn edit(rt: &Runtime) { - unimplemented!() - } - - /** - * Call editor on the file contents, but don't include the header inside the editor call. - * - * Return true if exit code from editor was good (and content was changed), false otherwise. - */ - pub fn edit_contents(rt: &Runtime) -> bool { - } - - /** - * Make file persistent. - * - * Retreive store path from the runtime, file id is already available in self. - * - * Return true if action succeeded, false otherwise. - */ - pub fn persist(rt: &Runtime) -> bool { - unimplemented!() - } - pub fn header(&self) -> FileHeaderData { self.header.clone() } @@ -139,11 +56,6 @@ impl<'a> File<'a> { r.is_match(&self.data[..]) || self.header.matches_with(r) } - fn get_new_file_id() -> FileID { - use uuid::Uuid; - let hash = FileHash::from(Uuid::new_v4().to_hyphenated_string()); - FileID::new(FileIDType::UUID, hash) - } } impl<'a> Display for File<'a> { diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 16fbe4f6..de86fe99 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -6,8 +6,13 @@ pub mod file; pub mod parser; pub mod json; +use module::Module; use storage::file::File; use storage::file::id::FileID; +use storage::file::id_type::FileIDType; +use storage::file::hash::FileHash; +use storage::parser::{FileHeaderParser, Parser, ParserError}; +use storage::file::header::data::FileHeaderData; type Cache<'a> = HashMap>>; @@ -23,4 +28,69 @@ impl<'a> Store<'a> { } } + pub fn new_file(&self, module: &'a Module<'a>) -> File<'a> { + let f = File { + owning_module: module, + header: FileHeaderData::Null, + data: String::from(""), + id: self.get_new_file_id(), + }; + debug!("Create new File object: {:?}", f); + f + } + + pub fn new_file_from_parser_result(&self, module: &'a Module<'a>, id: FileID, header: FileHeaderData, data: String) -> File<'a> { + let f = File { + owning_module: module, + header: header, + data: data, + id: id, + }; + debug!("Create new File object from parser result: {:?}", f); + f + } + + pub fn new_file_with_header(&self, module: &'a Module<'a>, h: FileHeaderData) -> File<'a> { + let f = File { + owning_module: module, + header: h, + data: String::from(""), + id: self.get_new_file_id(), + }; + debug!("Create new File object with header: {:?}", f); + f + } + + pub fn new_file_with_data(&self, module: &'a Module<'a>, d: String) -> File<'a> { + let f = File { + owning_module: module, + header: FileHeaderData::Null, + data: d, + id: self.get_new_file_id(), + }; + debug!("Create new File object with data: {:?}", f); + f + } + + pub fn new_file_with_content(&self, module: &'a Module<'a>, h: FileHeaderData, d: String) -> File<'a> { + let f = File { + owning_module: module, + header: h, + data: d, + id: self.get_new_file_id(), + }; + debug!("Create new File object with content: {:?}", f); + f + } + + pub fn persist(&self, file: &File) -> bool { + unimplemented!() + } + + fn get_new_file_id(&self) -> FileID { + use uuid::Uuid; + let hash = FileHash::from(Uuid::new_v4().to_hyphenated_string()); + FileID::new(FileIDType::UUID, hash) + } + } From 030b5fc8880ed1dcf548e5d37124733f0d552f0e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:18:55 +0100 Subject: [PATCH 08/15] Fixup Store --- src/storage/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index de86fe99..c42d2da5 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -87,6 +87,10 @@ impl<'a> Store<'a> { unimplemented!() } + pub fn load(&self, id: FileID) -> File { + unimplemented!() + } + fn get_new_file_id(&self) -> FileID { use uuid::Uuid; let hash = FileHash::from(Uuid::new_v4().to_hyphenated_string()); From f774effabb244c2afc20ec3c7aa689debe88a06e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:26:08 +0100 Subject: [PATCH 09/15] File: Return inner contents as references, not cloned --- src/storage/file/mod.rs | 14 +++++++------- src/ui/file.rs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/storage/file/mod.rs b/src/storage/file/mod.rs index 21d5e92e..165c0276 100644 --- a/src/storage/file/mod.rs +++ b/src/storage/file/mod.rs @@ -32,20 +32,20 @@ pub struct File<'a> { impl<'a> File<'a> { - pub fn header(&self) -> FileHeaderData { - self.header.clone() + pub fn header(&self) -> &FileHeaderData { + &self.header } - pub fn data(&self) -> String { - self.data.clone() + pub fn data(&self) -> &String { + &self.data } - pub fn contents(&self) -> (FileHeaderData, String) { + pub fn contents(&self) -> (&FileHeaderData, &String) { (self.header(), self.data()) } - pub fn id(&self) -> FileID { - self.id.clone() + pub fn id(&self) -> &FileID { + &self.id } pub fn owner(&self) -> &'a Module<'a> { diff --git a/src/ui/file.rs b/src/ui/file.rs index 625eb43e..85168421 100644 --- a/src/ui/file.rs +++ b/src/ui/file.rs @@ -105,7 +105,7 @@ impl FilePrinter for TablePrinter { let cell_i = Cell::new(&format!("{}", i)[..]); let cell_o = Cell::new(&format!("{}", file.owner().name())[..]); - let id : String = file.id().into(); + let id : String = file.id().clone().into(); let cell_id = Cell::new(&id[..]); let row = Row::new(vec![cell_i, cell_o, cell_id]); tab.add_row(row); From 470f1b1378915193ee389d7d722960d05baeb794 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:46:08 +0100 Subject: [PATCH 10/15] Save only module name in File, not whole module --- src/storage/file/mod.rs | 28 ++++++++++++++-------------- src/storage/mod.rs | 37 ++++++++++++++++++++++--------------- src/ui/file.rs | 6 +++--- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/storage/file/mod.rs b/src/storage/file/mod.rs index 165c0276..c22b0e5a 100644 --- a/src/storage/file/mod.rs +++ b/src/storage/file/mod.rs @@ -23,14 +23,18 @@ use self::header::data::*; * Internal abstract view on a file. Does not exist on the FS and is just kept * internally until it is written to disk. */ -pub struct File<'a> { - pub owning_module : &'a Module<'a>, - pub header : FileHeaderData, - pub data : String, - pub id : FileID, +pub struct File { + pub owning_module_name : &'static str, + pub header : FileHeaderData, + pub data : String, + pub id : FileID, } -impl<'a> File<'a> { +impl File { + + pub fn owner_name(&self) -> &'static str { + self.owning_module_name + } pub fn header(&self) -> &FileHeaderData { &self.header @@ -48,17 +52,13 @@ impl<'a> File<'a> { &self.id } - pub fn owner(&self) -> &'a Module<'a> { - self.owning_module - } - pub fn matches_with(&self, r: &Regex) -> bool { r.is_match(&self.data[..]) || self.header.matches_with(r) } } -impl<'a> Display for File<'a> { +impl Display for File { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { write!(fmt, @@ -66,7 +66,7 @@ impl<'a> Display for File<'a> { FileID: '{:?}' Header: '{:?}' Data : '{:?}'", - self.owning_module, + self.owning_module_name, self.header, self.data, self.id); @@ -75,7 +75,7 @@ impl<'a> Display for File<'a> { } -impl<'a> Debug for File<'a> { +impl Debug for File { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { write!(fmt, @@ -83,7 +83,7 @@ impl<'a> Debug for File<'a> { FileID: '{:?}' Header: '{:?}' Data : '{:?}'", - self.owning_module, + self.owning_module_name, self.header, self.data, self.id); diff --git a/src/storage/mod.rs b/src/storage/mod.rs index c42d2da5..c278e6f6 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -14,23 +14,25 @@ use storage::file::hash::FileHash; use storage::parser::{FileHeaderParser, Parser, ParserError}; use storage::file::header::data::FileHeaderData; -type Cache<'a> = HashMap>>; +type Cache = HashMap>; -pub struct Store<'a> { - cache : RefCell>, +pub struct Store { + cache : RefCell, } -impl<'a> Store<'a> { +impl Store { - pub fn new() -> Store<'a> { + pub fn new() -> Store { Store { cache: RefCell::new(HashMap::new()), } } - pub fn new_file(&self, module: &'a Module<'a>) -> File<'a> { + pub fn new_file(&self, module: &Module) + -> &RefCell + { let f = File { - owning_module: module, + owning_module_name: module.name(), header: FileHeaderData::Null, data: String::from(""), id: self.get_new_file_id(), @@ -39,9 +41,14 @@ impl<'a> Store<'a> { f } - pub fn new_file_from_parser_result(&self, module: &'a Module<'a>, id: FileID, header: FileHeaderData, data: String) -> File<'a> { + pub fn new_file_from_parser_result(&self, + module: &Module, + id: FileID, + header: FileHeaderData, + data: String) -> File + { let f = File { - owning_module: module, + owning_module_name: module.name(), header: header, data: data, id: id, @@ -50,9 +57,9 @@ impl<'a> Store<'a> { f } - pub fn new_file_with_header(&self, module: &'a Module<'a>, h: FileHeaderData) -> File<'a> { + pub fn new_file_with_header(&self, module: &Module, h: FileHeaderData) -> File { let f = File { - owning_module: module, + owning_module_name: module.name(), header: h, data: String::from(""), id: self.get_new_file_id(), @@ -61,9 +68,9 @@ impl<'a> Store<'a> { f } - pub fn new_file_with_data(&self, module: &'a Module<'a>, d: String) -> File<'a> { + pub fn new_file_with_data(&self, module: &Module, d: String) -> File { let f = File { - owning_module: module, + owning_module_name: module.name(), header: FileHeaderData::Null, data: d, id: self.get_new_file_id(), @@ -72,9 +79,9 @@ impl<'a> Store<'a> { f } - pub fn new_file_with_content(&self, module: &'a Module<'a>, h: FileHeaderData, d: String) -> File<'a> { + pub fn new_file_with_content(&self, module: &Module, h: FileHeaderData, d: String) -> File { let f = File { - owning_module: module, + owning_module_name: module.name(), header: h, data: d, id: self.get_new_file_id(), diff --git a/src/ui/file.rs b/src/ui/file.rs index 85168421..114e5c83 100644 --- a/src/ui/file.rs +++ b/src/ui/file.rs @@ -14,7 +14,7 @@ pub trait FilePrinter { /* * Print a list of files */ - fn print_files<'a, I: Iterator>>(&self, files: I) { + fn print_files>(&self, files: I) { for file in files { self.print_file(&file); } @@ -88,7 +88,7 @@ impl FilePrinter for TablePrinter { self.sp.print_file(f); } - fn print_files<'a, I: Iterator>>(&self, files: I) { + fn print_files>(&self, files: I) { use prettytable::Table; use prettytable::row::Row; use prettytable::cell::Cell; @@ -103,7 +103,7 @@ impl FilePrinter for TablePrinter { debug!("Printing file: {:?}", file); i += 1; let cell_i = Cell::new(&format!("{}", i)[..]); - let cell_o = Cell::new(&format!("{}", file.owner().name())[..]); + let cell_o = Cell::new(&format!("{}", file.owner_name())[..]); let id : String = file.id().clone().into(); let cell_id = Cell::new(&id[..]); From f29bcc7430e5591ccc784f68c98c68b1b08a5aad Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Dec 2015 23:51:44 +0100 Subject: [PATCH 11/15] Store: Put file into cache, return FileID --- src/storage/mod.rs | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index c278e6f6..290ede64 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -28,8 +28,14 @@ impl Store { } } + fn put_in_cache(&self, f: File) -> FileID { + let res = f.id().clone(); + self.cache.borrow_mut().insert(f.id().clone(), RefCell::new(f)); + res + } + pub fn new_file(&self, module: &Module) - -> &RefCell + -> FileID { let f = File { owning_module_name: module.name(), @@ -37,15 +43,17 @@ impl Store { data: String::from(""), id: self.get_new_file_id(), }; - debug!("Create new File object: {:?}", f); - f + + debug!("Create new File object: {:?}", &f); + self.put_in_cache(f) } pub fn new_file_from_parser_result(&self, module: &Module, id: FileID, header: FileHeaderData, - data: String) -> File + data: String) + -> FileID { let f = File { owning_module_name: module.name(), @@ -54,10 +62,14 @@ impl Store { id: id, }; debug!("Create new File object from parser result: {:?}", f); - f + self.put_in_cache(f) } - pub fn new_file_with_header(&self, module: &Module, h: FileHeaderData) -> File { + pub fn new_file_with_header(&self, + module: &Module, + h: FileHeaderData) + -> FileID + { let f = File { owning_module_name: module.name(), header: h, @@ -65,10 +77,12 @@ impl Store { id: self.get_new_file_id(), }; debug!("Create new File object with header: {:?}", f); - f + self.put_in_cache(f) } - pub fn new_file_with_data(&self, module: &Module, d: String) -> File { + pub fn new_file_with_data(&self, module: &Module, d: String) + -> FileID + { let f = File { owning_module_name: module.name(), header: FileHeaderData::Null, @@ -76,10 +90,15 @@ impl Store { id: self.get_new_file_id(), }; debug!("Create new File object with data: {:?}", f); - f + self.put_in_cache(f) } - pub fn new_file_with_content(&self, module: &Module, h: FileHeaderData, d: String) -> File { + pub fn new_file_with_content(&self, + module: &Module, + h: FileHeaderData, + d: String) + -> FileID + { let f = File { owning_module_name: module.name(), header: h, @@ -87,7 +106,7 @@ impl Store { id: self.get_new_file_id(), }; debug!("Create new File object with content: {:?}", f); - f + self.put_in_cache(f) } pub fn persist(&self, file: &File) -> bool { From 23e4152aaa4c08405a150b8b1b8ca1624a07dfc7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 00:04:54 +0100 Subject: [PATCH 12/15] Implement Store::load() --- src/storage/mod.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 290ede64..b35fa41f 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -1,3 +1,4 @@ +use std::rc::Rc; use std::cell::RefCell; use std::collections::HashMap; @@ -14,7 +15,7 @@ use storage::file::hash::FileHash; use storage::parser::{FileHeaderParser, Parser, ParserError}; use storage::file::header::data::FileHeaderData; -type Cache = HashMap>; +type Cache = HashMap>>; pub struct Store { cache : RefCell, @@ -30,7 +31,7 @@ impl Store { fn put_in_cache(&self, f: File) -> FileID { let res = f.id().clone(); - self.cache.borrow_mut().insert(f.id().clone(), RefCell::new(f)); + self.cache.borrow_mut().insert(f.id().clone(), Rc::new(RefCell::new(f))); res } @@ -113,8 +114,9 @@ impl Store { unimplemented!() } - pub fn load(&self, id: FileID) -> File { - unimplemented!() + pub fn load(&self, id: &FileID) -> Option>> { + debug!("Loading '{:?}'", id); + self.cache.borrow().get(id).cloned() } fn get_new_file_id(&self) -> FileID { From 0212fb1929e751a8e82dbc2294d3e6cff38c9687 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 00:29:21 +0100 Subject: [PATCH 13/15] Fix: Add missing dash --- src/storage/file/id.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/file/id.rs b/src/storage/file/id.rs index bdfd9db4..d26b537b 100644 --- a/src/storage/file/id.rs +++ b/src/storage/file/id.rs @@ -105,7 +105,7 @@ impl Into for FileID { fn into(self) -> String { let typestr : String = self.id_type.into(); let idstr : String = self.id.into(); - typestr + &idstr[..] + typestr + "-" + &idstr[..] } } From 8145b21404cceadf99d4a25568326d24703475f1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 00:29:45 +0100 Subject: [PATCH 14/15] Fix: Parser::write() can get reference tuple --- src/storage/parser.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/parser.rs b/src/storage/parser.rs index 266f6a3d..a3bbf832 100644 --- a/src/storage/parser.rs +++ b/src/storage/parser.rs @@ -122,7 +122,7 @@ impl Parser { Ok((h_parseres, data.unwrap_or(String::new()))) } - pub fn write(&self, tpl : (FileHeaderData, String)) -> Result { + pub fn write(&self, tpl : (&FileHeaderData, &String)) -> Result { debug!("Parsing internal datastructure to String"); let (header, data) = tpl; let h_text = try!(self.headerp.write(&header)); From 656aa69bfdcbc1cbf18d5e2968c367c0ea070b46 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 00:29:56 +0100 Subject: [PATCH 15/15] 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>> {