aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2018-09-15 20:34:14 +0100
committerKatharina Fey <kookie@spacekookie.de>2018-09-15 20:34:14 +0100
commit87cb39050d938e01b7b26633dab3d141737330ed (patch)
tree0b31f4c8ab0da54f32c959664c46427dc9d9174d
parent306c1e56b0dc457b59c47693c62fcab668398020 (diff)
A lot of refactoring in lockchain-files
-rw-r--r--lockchain-files/examples/create.rs6
-rw-r--r--lockchain-files/src/config.rs4
-rw-r--r--lockchain-files/src/create.rs44
-rw-r--r--lockchain-files/src/fs.rs14
-rw-r--r--lockchain-files/src/lib.rs74
-rw-r--r--lockchain-files/src/load.rs34
-rw-r--r--lockchain-files/src/userstore.rs9
-rw-r--r--lockchain-http/src/lib.rs4
-rw-r--r--lockchain-server/src/main.rs4
9 files changed, 123 insertions, 70 deletions
diff --git a/lockchain-files/examples/create.rs b/lockchain-files/examples/create.rs
index 68fd980..67279a9 100644
--- a/lockchain-files/examples/create.rs
+++ b/lockchain-files/examples/create.rs
@@ -1,7 +1,7 @@
extern crate lockchain_core as lcc;
extern crate lockchain_files as files;
-use files::DataVault;
+use files::FileVault;
use lcc::traits::Vault;
use lcc::users::User;
use lcc::{EncryptedBody, Generator, Payload, Record, VaultType};
@@ -12,7 +12,7 @@ fn main() {
let path = env::args().nth(1).unwrap();
let name = env::args().nth(2).unwrap();
- let mut vault: DataVault<EncryptedBody> = Generator::new()
+ let mut vault: FileVault<EncryptedBody> = Generator::new()
.path(name, path)
.user_type(VaultType::SoloUser {
username: "spacekookie".into(),
@@ -21,7 +21,7 @@ fn main() {
.unwrap();
vault.sync();
- // let vault: DataVault<EncryptedBody> = DataVault::new(&name, &path);
+ // let vault: FileVault<EncryptedBody> = FileVault::new(&name, &path);
// let mut store = match (
// vault.meta_pull_domain("userstore"),
diff --git a/lockchain-files/src/config.rs b/lockchain-files/src/config.rs
index ffec582..c862b31 100644
--- a/lockchain-files/src/config.rs
+++ b/lockchain-files/src/config.rs
@@ -11,6 +11,8 @@ use semver::Version;
use toml;
use utils::FileToString;
+use lcc::Generator;
+
/// A set of errors around `lockchain-files` configs
#[derive(Debug)]
pub enum ConfigError {
@@ -46,7 +48,7 @@ pub struct VaultConfig {
}
impl VaultConfig {
- pub fn new() -> Self {
+ pub fn new(_: &Generator) -> Self {
Self {
version: "0.1".into(),
created_at: SystemTime::now(),
diff --git a/lockchain-files/src/create.rs b/lockchain-files/src/create.rs
new file mode 100644
index 0000000..6c5255a
--- /dev/null
+++ b/lockchain-files/src/create.rs
@@ -0,0 +1,44 @@
+//! A small submodule which handles all aspects of vault creation
+#![allow(unused_imports)]
+
+use lcc::errors::VaultError;
+use lcc::{traits::Body, Generator};
+use std::collections::HashMap;
+
+use ::FileVault;
+use ::config::{VaultConfig, ConfigError};
+use ::fs::{Filesystem, FileType};
+
+
+impl<T: Body> FileVault<T> {
+ /// A small utility to create a new file vault
+ pub(crate) fn create(gen: Generator) -> Result<Self, VaultError> {
+ let (name, location) = Self::get_path(&gen)?;
+
+ let fs = Filesystem::new(location, name);
+ fs.scaffold().map_err(|_| VaultError::FailedCreation)?;
+
+ let cfg = VaultConfig::new(&gen);
+
+ // Ok(Box::new(
+ // Self {
+ // meta_info: (
+ // gen.name.clone().unwrap().into(),
+ // gen.location.clone().unwrap().into(),
+ // ),
+ // records: HashMap::new(),
+ // config: VaultConfig::new(),
+ // metadata: HashMap::new(),
+ // fs: Filesystem::new(&gen.location.unwrap(), &gen.name.unwrap()),
+ // users: UserStoreMapper::new(),
+ // }.initialize(),
+ // ))
+ unimplemented!()
+ }
+
+ fn get_path(gen: &Generator) -> Result<(&str, &str), VaultError> {
+
+
+ Err(VaultError::IncompleteGenerator)
+ }
+}
diff --git a/lockchain-files/src/fs.rs b/lockchain-files/src/fs.rs
index 1aa57bc..af47e50 100644
--- a/lockchain-files/src/fs.rs
+++ b/lockchain-files/src/fs.rs
@@ -13,7 +13,7 @@ use lcc::traits::AutoEncoder;
use std::collections::HashMap;
use std::error::Error;
-use std::io::Write;
+use std::io::{self, Write};
use std::{
fs::{self, File, OpenOptions as OO},
path::PathBuf,
@@ -70,12 +70,12 @@ impl Filesystem {
}
/// Create required directories
- pub fn scaffold(&self) -> Option<()> {
- fs::create_dir_all(&self.root).ok()?;
- fs::create_dir(&self.root.join("records")).ok()?;
- fs::create_dir(&self.root.join("metadata")).ok()?;
- fs::create_dir(&self.root.join("checksums")).ok()?;
- Some(())
+ pub fn scaffold(&self) -> Result<(), io::Error> {
+ fs::create_dir_all(&self.root)?;
+ fs::create_dir(&self.root.join("records"))?;
+ fs::create_dir(&self.root.join("metadata"))?;
+ fs::create_dir(&self.root.join("checksums"))?;
+ Ok(())
}
/// Load all files of a certain type into a Vec<String>
diff --git a/lockchain-files/src/lib.rs b/lockchain-files/src/lib.rs
index a7941e9..cfb097a 100644
--- a/lockchain-files/src/lib.rs
+++ b/lockchain-files/src/lib.rs
@@ -58,12 +58,14 @@ use lcc::traits::{Body, LoadRecord, Vault};
use lcc::{
errors::VaultError,
users::{Access, Token, UserStore},
- Generator, MetaDomain, Payload, Record, VaultMetadata,
+ Generator, Header, MetaDomain, Payload, Record, VaultMetadata,
};
use std::collections::HashMap;
mod config;
+mod create;
mod fs;
+mod load;
mod userstore;
mod utils;
@@ -87,50 +89,30 @@ use userstore::UserStoreMapper;
///
/// The vault folder is safe to copy around –
/// all vault metadata is kept inside it.
-pub struct DataVault<T: Body> {
- meta_info: (String, String),
+pub struct FileVault<T: Body> {
+ /// A representation of the cached vault config
config: VaultConfig,
- records: HashMap<String, Record<T>>,
- metadata: HashMap<String, MetaDomain>,
+ /// Filesystem wrapper utility
fs: Filesystem,
+ /// A userstore utility derived from Metadata
users: UserStoreMapper,
+ /// A mapping of loaded records
+ records: HashMap<String, Record<T>>,
+ /// An index of all existing headers
+ headers: HashMap<String, Header>,
+ /// A map of all metadata files
+ metadata: HashMap<String, MetaDomain>,
}
-impl<T: Body> DataVault<T> {
- /// Small utility function to setup file structure
- fn initialize(self) -> Self {
- self.fs.scaffold();
- self.config.save(&self.fs.root).unwrap();
- self
- }
-
- fn load(mut self) -> Result<Box<Self>, VaultError> {
- self.config = match VaultConfig::load(&self.fs.root) {
- Ok(cfg) => cfg,
- _ => return Err(VaultError::FailedLoading),
- };
+impl<T: Body> LoadRecord<T> for FileVault<T> {}
- Ok(Box::new(self))
+impl<T: Body> Vault<T> for FileVault<T> {
+ fn new(gen: Generator) -> Result<Box<FileVault<T>>, VaultError> {
+ Self::create(gen).map(|s| Box::new(s))
}
-}
-
-impl<T: Body> LoadRecord<T> for DataVault<T> {}
-impl<T: Body> Vault<T> for DataVault<T> {
- fn new(gen: Generator) -> Result<Box<DataVault<T>>, VaultError> {
- Ok(Box::new(
- Self {
- meta_info: (
- gen.name.clone().unwrap().into(),
- gen.location.clone().unwrap().into(),
- ),
- records: HashMap::new(),
- config: VaultConfig::new(),
- metadata: HashMap::new(),
- fs: Filesystem::new(&gen.location.unwrap(), &gen.name.unwrap()),
- users: UserStoreMapper::new(UserStore::new()),
- }.initialize(),
- ))
+ fn load(name: &str, location: &str) -> Result<Box<Self>, VaultError> {
+ Self::load(name, location).map(|s| Box::new(s))
}
fn create_user(
@@ -143,22 +125,8 @@ impl<T: Body> Vault<T> for DataVault<T> {
unimplemented!()
}
- fn delete_user(&mut self, token: Token, username: &str) {}
-
- // Checking if a vault exists is basically checking it's config
- // against the compatible version of this library.
- //
- // If it's compatible we can open the vault into memory
- // (loading all required paths into the struct), then return it
- fn load(name: &str, location: &str) -> Result<Box<Self>, VaultError> {
- Self {
- meta_info: (name.into(), location.into()),
- records: HashMap::new(),
- config: VaultConfig::new(),
- metadata: HashMap::new(),
- fs: Filesystem::new(location, name),
- users: UserStoreMapper::new(UserStore::new()),
- }.load()
+ fn delete_user(&mut self, token: Token, username: &str) {
+ unimplemented!()
}
fn authenticate(&mut self, username: &str, secret: &str) -> Token {
diff --git a/lockchain-files/src/load.rs b/lockchain-files/src/load.rs
new file mode 100644
index 0000000..fd4e66e
--- /dev/null
+++ b/lockchain-files/src/load.rs
@@ -0,0 +1,34 @@
+//! A small submodule which handles all aspects of vault creation
+#![allow(unused_imports)]
+
+use lcc::errors::VaultError;
+use lcc::{traits::Body, Generator};
+use std::collections::HashMap;
+
+use ::FileVault;
+use ::config::{VaultConfig, ConfigError};
+use ::fs::{Filesystem, FileType};
+
+
+impl<T: Body> FileVault<T> {
+ /// A small utility to load an existing file vault
+ pub(crate) fn load(name: &str, location: &str) -> Result<Self, VaultError> {
+ unimplemented!()
+
+ // self.config = match VaultConfig::load(&self.fs.root) {
+ // Ok(cfg) => cfg,
+ // _ => return Err(VaultError::FailedLoading),
+ // };
+ // Ok(Box::new(self))
+
+ // Self {
+ // meta_info: (name.into(), location.into()),
+ // records: HashMap::new(),
+ // config: VaultConfig::new(Gene),
+ // metadata: HashMap::new(),
+ // fs: Filesystem::new(location, name),
+ // users: UserStoreMapper::new(),
+ // }.load()
+ }
+
+}
diff --git a/lockchain-files/src/userstore.rs b/lockchain-files/src/userstore.rs
index 1d01db6..07c57fd 100644
--- a/lockchain-files/src/userstore.rs
+++ b/lockchain-files/src/userstore.rs
@@ -3,13 +3,18 @@
use lcc::users::UserStore;
-#[derive(Debug)]
pub struct UserStoreMapper {
inner: UserStore,
}
impl UserStoreMapper {
- pub fn new(store: UserStore) -> Store {
+ pub fn new() -> Self {
+ Self {
+ inner: UserStore::new(),
+ }
+ }
+
+ pub fn load(store: UserStore) -> Self {
Self { inner: store }
}
}
diff --git a/lockchain-http/src/lib.rs b/lockchain-http/src/lib.rs
index 961ad21..005bdb6 100644
--- a/lockchain-http/src/lib.rs
+++ b/lockchain-http/src/lib.rs
@@ -50,12 +50,12 @@ pub type HttpApi<V> = server::HttpServer<App<Arc<Mutex<V>>>>;
/// ```norun
/// use lockchain_core::{traits::*, EncryptedBody};
/// use lockchain_http::create_server;
-/// use lockchain_files::DataVault;
+/// use lockchain_files::FileVault;
///
/// let server = create_server(
/// "localhost",
/// "8080",
-/// DataVault::<EncryptedBody>::new("name", "some-location"),
+/// FileVault::<EncryptedBody>::new("name", "some-location"),
/// ).run();
/// ```
pub fn create_server<B, V>(
diff --git a/lockchain-server/src/main.rs b/lockchain-server/src/main.rs
index f23cf71..d96c213 100644
--- a/lockchain-server/src/main.rs
+++ b/lockchain-server/src/main.rs
@@ -7,11 +7,11 @@ extern crate lockchain_files as files;
extern crate lockchain_http as http;
use core::EncryptedBody;
-use files::DataVault;
+use files::FileVault;
use http::{create_server, state::ApiState};
fn main() {
- let state = ApiState::<EncryptedBody, DataVault<EncryptedBody>> {
+ let state = ApiState::<EncryptedBody, FileVault<EncryptedBody>> {
bound_scope: true,
working_dir: ".".into(),