diff options
author | Katharina Fey <kookie@spacekookie.de> | 2018-09-16 11:03:38 +0100 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2018-09-16 11:03:38 +0100 |
commit | 39a9b4ab3f1ab8a4c79962e1de78204faaf10abe (patch) | |
tree | 9cec5f02cc8d227eeb0a2c81b3d6761ffd6aed65 | |
parent | b257bdaefe633ebb72590aac2b7f504af51ba23e (diff) |
Only relying on the `UserStore` `AutoEncoder` impl for serialisation
-rw-r--r-- | lockchain-core/src/errors/data.rs | 5 | ||||
-rw-r--r-- | lockchain-core/src/meta.rs | 5 | ||||
-rw-r--r-- | lockchain-core/src/users/userstore.rs | 8 | ||||
-rw-r--r-- | lockchain-files/src/fs/mod.rs | 4 | ||||
-rw-r--r-- | lockchain-files/src/fs/primitive.rs | 2 | ||||
-rw-r--r-- | lockchain-files/src/userstore.rs | 16 |
6 files changed, 36 insertions, 4 deletions
diff --git a/lockchain-core/src/errors/data.rs b/lockchain-core/src/errors/data.rs index b854d37..519ae88 100644 --- a/lockchain-core/src/errors/data.rs +++ b/lockchain-core/src/errors/data.rs @@ -5,7 +5,10 @@ use std::fmt::{Display, Formatter, Result}; #[derive(Debug, Serialize, Deserialize)] pub enum Error { - + FailedRead, + FailedWrite, + FailedEncode, + FailedDecode, } impl StdError for Error {} diff --git a/lockchain-core/src/meta.rs b/lockchain-core/src/meta.rs index 317e821..74b998d 100644 --- a/lockchain-core/src/meta.rs +++ b/lockchain-core/src/meta.rs @@ -59,6 +59,11 @@ impl MetaDomain { } } + /// Insert a single value into the body + pub fn insert<S: Into<String>>(&mut self, key: S, value: Payload) -> &mut Self { + unimplemented!() + } + /// Return a read-only reference to the entire body pub fn all(&self) -> &HashMap<String, Payload> { &self.body diff --git a/lockchain-core/src/users/userstore.rs b/lockchain-core/src/users/userstore.rs index 483c4ee..5355cdb 100644 --- a/lockchain-core/src/users/userstore.rs +++ b/lockchain-core/src/users/userstore.rs @@ -3,20 +3,26 @@ use super::rights::Access; use crypto::Key; use std::collections::HashMap; +use traits::AutoEncoder; /// A thin user UserStore /// /// It's implementation can manage multiple keys per user, of various /// types and constrained for limited access rights. +#[derive(Serialize, Deserialize)] pub struct UserStore { store: HashMap<String, StoreUser>, } -struct StoreUser { +/// Internal store user structure +#[derive(Serialize, Deserialize)] +pub struct StoreUser { name: String, keys: HashMap<Access, Key>, } +impl AutoEncoder for UserStore {} + impl UserStore { /// Create a new, empty UserStore /// diff --git a/lockchain-files/src/fs/mod.rs b/lockchain-files/src/fs/mod.rs index eed860d..7820925 100644 --- a/lockchain-files/src/fs/mod.rs +++ b/lockchain-files/src/fs/mod.rs @@ -88,11 +88,11 @@ impl Filesystem { pub fn sync_vault<T: Body>(&self, vault: &FileVault<T>) -> Result<(), io::Error> { vault.config.save(&self.root)?; - unimplemented!() } /// Respond to a sync request + #[deprecated] pub fn sync<T>(&self, data: &HashMap<String, T>, types: FileType) -> Result<(), Box<Error>> where T: AutoEncoder, @@ -122,4 +122,6 @@ impl Filesystem { // Ok(()) } + + /************* Private utility functions*************/ } diff --git a/lockchain-files/src/fs/primitive.rs b/lockchain-files/src/fs/primitive.rs index c5e13a3..d567a28 100644 --- a/lockchain-files/src/fs/primitive.rs +++ b/lockchain-files/src/fs/primitive.rs @@ -47,7 +47,7 @@ fn type_path(tt: FileType, root: &PathBuf) -> PathBuf { path } -pub fn write_file(tt: FileType, root: PathBuf, name: &str, contents: Vec<u8>) -> Result<()> { +pub(crate) fn write_file(tt: FileType, root: PathBuf, name: &str, contents: Vec<u8>) -> Result<()> { let file_name = format!("{}.{}", name, file_ending!(&tt)); let mut path = type_path(tt, &root); path.push(file_name); diff --git a/lockchain-files/src/userstore.rs b/lockchain-files/src/userstore.rs index f7bfccd..4c4ff43 100644 --- a/lockchain-files/src/userstore.rs +++ b/lockchain-files/src/userstore.rs @@ -1,4 +1,20 @@ //! Implements serialization, desrialization for UserStore +use lcc::{errors::DataError, traits::AutoEncoder, users::UserStore}; +pub trait DiskMirror { + fn to_disk(&self) -> Vec<u8>; + fn from_disk(Vec<u8>) -> Result<Box<Self>, DataError>; +} +impl DiskMirror for UserStore { + fn to_disk(&self) -> Vec<u8> { + self.encode().unwrap().into_bytes() + } + + fn from_disk(vec: Vec<u8>) -> Result<Box<Self>, DataError> { + Self::decode(::std::str::from_utf8(vec.as_slice()).map_err(|_| DataError::FailedDecode)?) + .map(|s| Box::new(s)) + .map_err(|_| DataError::FailedDecode) + } +} |