From f3824b79256133374aacd910f34ed2764640b1b7 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Thu, 6 Sep 2018 20:23:27 +0200 Subject: Various smol changes around the new Generator API --- lockchain-core/src/init.rs | 79 ++++++++++++++++++++++++++++++++++++++ lockchain-core/src/initialise.rs | 55 -------------------------- lockchain-core/src/lib.rs | 4 +- lockchain-files/examples/create.rs | 39 +++++++++---------- lockchain-files/src/lib.rs | 3 +- 5 files changed, 102 insertions(+), 78 deletions(-) create mode 100644 lockchain-core/src/init.rs delete mode 100644 lockchain-core/src/initialise.rs diff --git a/lockchain-core/src/init.rs b/lockchain-core/src/init.rs new file mode 100644 index 0000000..f7f2d5d --- /dev/null +++ b/lockchain-core/src/init.rs @@ -0,0 +1,79 @@ +use traits::{Body, Vault}; + +/// Describes the internal permission layout of a vault +pub enum VaultType { + /// Create an all-powerful root user which can access everything + Administrated { + /// Set a root password + secret: String, + }, + /// Similar to `Administrated` + /// but only allows a single-user for a vault + SoloUser { username: String, secret: String }, +} + +/// A shared initialisation generator for vaults +/// +/// All vaults, regardless of backends +/// or persistence layer +/// share the same common principles +/// of users and permissions. +/// +/// This means that intiailisation is shared, +/// regardless of what backend implements it. +/// +/// A `VaultGenerator` takes arguments +/// for a generic backend, +/// calls functions provided by said backend +/// and then returns the actual backend. +pub struct Generator { + #[doc(hidden)] + pub name: Option, + #[doc(hidden)] + pub location: Option, + #[doc(hidden)] + pub user_type: Option, +} + +impl Generator { + /// Start a new generator for a generic type + pub fn new() -> Self { + Self { + name: None, + location: None, + user_type: None, + } + } + + pub fn path(self, name: N, location: L) -> Self + where + N: Into, + L: Into, + { + Self { + name: Some(name.into()), + location: Some(location.into()), + ..self + } + } + + /// Specify the internal user permission structure for this vault + /// + /// If you don't know what this means, please consult + /// the `VaultType` enum documentation + pub fn user_type(self, t: VaultType) -> Self { + Self { + user_type: Some(t), + ..self + } + } + + /// Finally call this function to construct the vault + pub fn finalise(self) -> V + where + V: Vault, + B: Body, + { + V::new(self) + } +} diff --git a/lockchain-core/src/initialise.rs b/lockchain-core/src/initialise.rs deleted file mode 100644 index 6ae48a9..0000000 --- a/lockchain-core/src/initialise.rs +++ /dev/null @@ -1,55 +0,0 @@ -//! A shared initialisation block for vaults -//! -//! All vaults, regardless of backends -//! or persistence layer -//! share the same common principles -//! of users and permissions. -//! -//! This means that intiailisation is shared, -//! regardless of what backend implements it. -//! -//! A `VaultGenerator` takes arguments -//! for a generic backend, -//! calls functions provided by said backend -//! and then returns the actual backend. - -use traits::{Body, Vault}; - -/// A generator is initialised with a generic backend -/// which can then chain-call functions to setup the -/// base functionality of a Vault, and then yield -/// a working and initialised instance of the -/// generic vault backend. -pub struct Generator { - #[doc(hidden)] - pub name: Option, - #[doc(hidden)] - pub location: Option, -} - -impl Generator { - /// Start a new generator for a generic type - pub fn new() -> Self { - Self { - name: None, - location: None, - } - } - - pub fn path>(self, name: S, location: S) -> Self { - Self { - name: Some(name.into()), - location: Some(location.into()), - ..self - } - } - - /// Finally call this function to construct the vault - pub fn finalise(self) -> V - where - V: Vault, - B: Body, - { - V::new(self) - } -} diff --git a/lockchain-core/src/lib.rs b/lockchain-core/src/lib.rs index b3d0931..52139bb 100644 --- a/lockchain-core/src/lib.rs +++ b/lockchain-core/src/lib.rs @@ -76,15 +76,17 @@ mod record; pub mod traits; pub mod users; -pub mod initialise; +mod init; pub use self::crypto::PackedData; pub use self::meta::{MetaDomain, VaultMetadata}; pub use self::record::{EncryptedBody, Header, Payload, Record}; +pub use self::init::Generator; /// Export commonly used types via the prelude pub mod prelude { pub use super::crypto::PackedData; pub use super::meta::{MetaDomain, VaultMetadata}; pub use super::record::{EncryptedBody, Header, Payload, Record}; + pub use super::init::Generator; } diff --git a/lockchain-files/examples/create.rs b/lockchain-files/examples/create.rs index b152cac..b1c6a94 100644 --- a/lockchain-files/examples/create.rs +++ b/lockchain-files/examples/create.rs @@ -3,8 +3,8 @@ extern crate lockchain_files as files; use files::DataVault; use lcc::traits::Vault; -use lcc::users::{User, UserStore}; -use lcc::{EncryptedBody, Payload, Record}; +use lcc::users::User; +use lcc::{Generator, EncryptedBody, Payload, Record}; use std::env; fn main() { @@ -12,29 +12,28 @@ fn main() { let path = env::args().nth(1).unwrap(); let name = env::args().nth(2).unwrap(); - let vault: DataVault = DataVault::new(&name, &path); + let mut vault: DataVault = Generator::new().path(name, path).finalise(); + vault.sync(); - + // let vault: DataVault = DataVault::new(&name, &path); + // let mut store = match ( + // vault.meta_pull_domain("userstore"), + // vault.meta_pull_domain("registry"), + // ) { + // (Some(users), Some(registry)) => (users.clone(), registry.clone()).into(), + // _ => UserStore::default(), + // }; + // /* Some users of our vault have the same password :S */ + // store.add(User::register("alice", "password")); + // let token = store.get_token(vec!()); - // let mut store = match ( - // vault.meta_pull_domain("userstore"), - // vault.meta_pull_domain("registry"), - // ) { - // (Some(users), Some(registry)) => (users.clone(), registry.clone()).into(), - // _ => UserStore::default(), - // }; + // let (users, registry) = store.into(); - // /* Some users of our vault have the same password :S */ - // store.add(User::register("alice", "password")); - // let token = store.get_token(vec!()); - - // let (users, registry) = store.into(); - - // vault.meta_push_domain(users); - // vault.meta_push_domain(registry); - // vault.sync(); + // vault.meta_push_domain(users); + // vault.meta_push_domain(registry); + // vault.sync(); } else { eprintln!("Usage: create [FLAGS] (there are no flags)") } diff --git a/lockchain-files/src/lib.rs b/lockchain-files/src/lib.rs index 97912be..5926355 100644 --- a/lockchain-files/src/lib.rs +++ b/lockchain-files/src/lib.rs @@ -56,9 +56,8 @@ extern crate serde; use lcc::traits::{Body, LoadRecord, Vault}; use lcc::{ - initialise::Generator, users::{Access, Token}, - MetaDomain, Payload, Record, VaultMetadata, + Generator, MetaDomain, Payload, Record, VaultMetadata, }; use std::collections::HashMap; -- cgit v1.2.3