diff options
author | Katharina Fey <kookie@spacekookie.de> | 2018-06-30 16:08:05 +0200 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2018-06-30 16:08:05 +0200 |
commit | e20809885cb6c701e54bb67c21f31c9046102386 (patch) | |
tree | cb1e15b45be2f4d042981cf4cf565a823fa68a5d | |
parent | 21fc2e1f8f1add8ed33a10ffa658acfa53d2fb61 (diff) |
Adding more utility functions to a bunch of places
Integrating the userstore into the http crate
-rw-r--r-- | lockchain-core/src/record.rs | 2 | ||||
-rw-r--r-- | lockchain-core/src/users.rs | 12 | ||||
-rw-r--r-- | lockchain-files/src/lib.rs | 6 | ||||
-rw-r--r-- | lockchain-http/src/state.rs | 23 |
4 files changed, 36 insertions, 7 deletions
diff --git a/lockchain-core/src/record.rs b/lockchain-core/src/record.rs index ed66f36..ac913b4 100644 --- a/lockchain-core/src/record.rs +++ b/lockchain-core/src/record.rs @@ -90,7 +90,7 @@ impl<T: Body> Record<T> { /// Attempt to set a key to a certain value pub fn add_data(&mut self, key: &str, value: Payload) -> Option<()> { - (self.body.as_mut()?).set_field(key, value)?; + (self.body.as_mut()?).set_field(key, value); Some(()) } diff --git a/lockchain-core/src/users.rs b/lockchain-core/src/users.rs index 581bcce..0d5973c 100644 --- a/lockchain-core/src/users.rs +++ b/lockchain-core/src/users.rs @@ -88,6 +88,18 @@ impl UserStore { pub fn get_user(&self, name: &str) -> Option<&User> { self.users.get(name) } + + pub fn get_all(&self) -> &HashMap<String, User> { + &self.users + } +} + +impl Default for UserStore { + fn default() -> Self { + Self { + users: HashMap::new(), + } + } } impl AutoEncoder for UserStore {} diff --git a/lockchain-files/src/lib.rs b/lockchain-files/src/lib.rs index 48da2b3..b94c5b1 100644 --- a/lockchain-files/src/lib.rs +++ b/lockchain-files/src/lib.rs @@ -128,8 +128,10 @@ impl<T: Body> Vault<T> for DataVault<T> { } fn meta_push_domain(&mut self, domain: MetaDomain) -> Option<()> { - self.metadata.insert(domain.name().into(), domain); - Some(()) + self.metadata + .insert(domain.name().into(), domain) + .map_or((), |_| ()) // We don't care about `None` + .into() } fn meta_set(&mut self, domain: &str, name: &str, data: Payload) -> Option<()> { diff --git a/lockchain-http/src/state.rs b/lockchain-http/src/state.rs index 4677632..78daf81 100644 --- a/lockchain-http/src/state.rs +++ b/lockchain-http/src/state.rs @@ -1,4 +1,6 @@ use lockchain::traits::{AutoEncoder, Body, Vault}; +use lockchain::users::{User, UserStore}; + use std::collections::HashMap; use std::marker::PhantomData; use std::path::PathBuf; @@ -32,6 +34,8 @@ where #[doc(hidden)] pub vaults: HashMap<String, Option<V>>, #[doc(hidden)] + pub users: UserStore, + #[doc(hidden)] pub _phantom: PhantomData<B>, /// Signal if the API handlers are allowed outside their working dir @@ -45,6 +49,16 @@ where B: Body, V: Vault<B>, { + /// Load an existing API state from an encoded string + pub fn load(encoded: &str) -> Option<Self> { + SerializedState::decode(encoded).ok().map(|s| s.into()) + } + + /// Store an in-memory API state to an encoded string + pub fn store(&self) -> String { + SerializedState::from(self).encode().ok().unwrap() + } + /// Return a list of string slices for each vault in scope pub fn vaults(&self) -> Vec<&str> { self.vaults.iter().map(|(k, _)| k.as_str()).collect() @@ -70,10 +84,9 @@ where { fn default() -> Self { Self { - vaults: Default::default(), _phantom: PhantomData, bound_scope: true, - working_dir: Default::default(), + ..Default::default() } } } @@ -81,17 +94,18 @@ where #[derive(Serialize, Deserialize)] struct SerializedState { vaults: Vec<String>, + users: Vec<User>, } impl AutoEncoder for SerializedState {} /// Implements the transform from in-memory to on-disk -impl<B, V> From<ApiState<B, V>> for SerializedState +impl<'state, B, V> From<&'state ApiState<B, V>> for SerializedState where B: Body, V: Vault<B>, { - fn from(me: ApiState<B, V>) -> Self { + fn from(me: &'state ApiState<B, V>) -> Self { Self { vaults: me .vaults @@ -100,6 +114,7 @@ where acc.push(k); acc }), + users: me.users.get_all().iter().map(|(_, v)| v).collect(), } } } |