diff options
author | Katharina Sabel <katharina.sabel@asquera.de> | 2018-05-13 21:14:35 +0200 |
---|---|---|
committer | Katharina Sabel <katharina.sabel@asquera.de> | 2018-05-13 21:14:35 +0200 |
commit | 1f63146d9238dda456f6b3ff8c37cc26f765d4a1 (patch) | |
tree | aac05ae263b76b4e4914ced675e0f1153143df5a | |
parent | 0e1d57d3c6bd4a54be4eeca9502d6b7596549b43 (diff) |
Starting work on http crate
-rw-r--r-- | lockchain-core/src/lib.rs | 2 | ||||
-rw-r--r-- | lockchain-files/src/fs.rs | 57 | ||||
-rw-r--r-- | lockchain-files/src/lib.rs | 4 | ||||
-rw-r--r-- | lockchain-http/src/impl.rs | 0 | ||||
-rw-r--r-- | lockchain-http/src/impl/base.rs | 0 | ||||
-rw-r--r-- | lockchain-http/src/lib.rs | 4 |
6 files changed, 57 insertions, 10 deletions
diff --git a/lockchain-core/src/lib.rs b/lockchain-core/src/lib.rs index 02e5a88..8661894 100644 --- a/lockchain-core/src/lib.rs +++ b/lockchain-core/src/lib.rs @@ -21,4 +21,4 @@ mod record; pub use self::crypto::PackedData; pub use self::record::{Header, Payload, Record, EncryptedBody}; -pub use self::users::User;
\ No newline at end of file +pub use self::users::User; diff --git a/lockchain-files/src/fs.rs b/lockchain-files/src/fs.rs index dfa08d1..a0c288a 100644 --- a/lockchain-files/src/fs.rs +++ b/lockchain-files/src/fs.rs @@ -1,9 +1,13 @@ //! Utility module which handles filesystem writes use lcc::traits::AutoEncoder; -use std::fs::{self, File}; -use std::io::{self, Read}; -use std::path::PathBuf; + +use std::collections::HashMap; +use std::error::Error; +use std::io::{self, Read, Write}; +use std::{ + fs::{self, File, OpenOptions as OO}, path::PathBuf, +}; pub struct Filesystem { name: String, @@ -12,6 +16,7 @@ pub struct Filesystem { } /// A switching enum to determine what type of file to load + pub enum FileType { /// A data record file Record, @@ -44,7 +49,7 @@ impl Filesystem { } /// Load all files of a certain type into a Vec<String> - pub fn fetch<T: AutoEncoder>(&self, types: FileType) -> Result<Vec<T>, io::Error> { + pub fn fetch<T: AutoEncoder>(&self, types: FileType) -> Result<Vec<T>, Box<Error>> { Ok(fs::read_dir(match types { FileType::Record => self.root.join("records"), _ => self.root.join("."), @@ -62,12 +67,48 @@ impl Filesystem { .collect()) } - pub fn pull<T: AutoEncoder>(&self, types: FileType, id: &str) -> Result<T, io::Error> { - unimplemented!() + pub fn pull<T: AutoEncoder>(&self, types: FileType, id: &str) -> Result<T, Box<Error>> { + Ok(T::decode(&File::open(self.root.join(&format!( + "{}.{}", + id, + match types { + FileType::Record => "record", + _ => "dat", + } + )))?.get_string()?)?) } - pub fn sync<T: AutoEncoder>(&self, types: FileType) -> Result<(), io::Error> { - unimplemented!() + pub fn sync<T: AutoEncoder>( + &self, + data: &HashMap<String, T>, + types: FileType, + ) -> Result<(), Box<Error>> { + data.into_iter() + .map(|(k, v)| (k, v.encode().ok())) + .map(|(k, v)| { + ( + self.root.join(format!( + "{}.{}", + k, + match types { + FileType::Record => "record", + _ => "dat", + } + )), + v, + ) + }) + .filter(|(_, v)| v.is_some()) + .map(|(k, v)| (k, v.unwrap())) + .map(|(path, data): (PathBuf, String)| (OO::new().write(true).open(path), data)) + .filter(|(path, _)| path.is_ok()) + .map(|(file, data)| (file.unwrap(), data)) + .for_each(|(mut file, data)| { + file.write_all(data.as_bytes()) + .expect("Failed to write file!") + }); + + Ok(()) } } diff --git a/lockchain-files/src/lib.rs b/lockchain-files/src/lib.rs index 2bf7422..fbedc34 100644 --- a/lockchain-files/src/lib.rs +++ b/lockchain-files/src/lib.rs @@ -57,7 +57,9 @@ impl<T: Body> Vault<T> for DataVault<T> { } fn sync(&mut self) { - self.fs.sync::<Record<T>>(FileType::Record).unwrap(); + self.fs + .sync::<Record<T>>(&self.records, FileType::Record) + .unwrap(); } fn get_record(&self, name: &str) -> Option<&Record<T>> { diff --git a/lockchain-http/src/impl.rs b/lockchain-http/src/impl.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lockchain-http/src/impl.rs diff --git a/lockchain-http/src/impl/base.rs b/lockchain-http/src/impl/base.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lockchain-http/src/impl/base.rs diff --git a/lockchain-http/src/lib.rs b/lockchain-http/src/lib.rs index b159345..2c69ab4 100644 --- a/lockchain-http/src/lib.rs +++ b/lockchain-http/src/lib.rs @@ -1,4 +1,8 @@ //! A plug and play http interface layer for various lockchain components +#![feature(external_doc)] +#![doc(include = "../README.md")] +#![feature(non_modrs_mods)] + extern crate gotham_serde_json_body_parser as goth_json; extern crate gotham; |