aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock84
-rw-r--r--lockchain-core/Cargo.toml2
-rw-r--r--lockchain-core/src/crypto/utils.rs20
-rw-r--r--lockchain-core/src/traits.rs17
-rw-r--r--lockchain-crypto/Cargo.toml4
-rw-r--r--lockchain-files/Cargo.toml6
-rw-r--r--lockchain-files/src/fs.rs51
-rw-r--r--lockchain-files/src/lib.rs26
8 files changed, 134 insertions, 76 deletions
diff --git a/Cargo.lock b/Cargo.lock
index f8eef46..6549976 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -98,7 +98,7 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -178,8 +178,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"semver 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -200,7 +200,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -402,8 +402,8 @@ dependencies = [
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -428,7 +428,7 @@ dependencies = [
"gotham 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.11.26 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -469,7 +469,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
- "unicode-normalization 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-normalization 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -532,14 +532,14 @@ dependencies = [
"base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bcrypt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lockchain-core"
-version = "0.4.1"
+version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -547,65 +547,65 @@ dependencies = [
"blake2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lockchain-core"
-version = "0.5.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
+version = "0.6.0"
dependencies = [
"base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bcrypt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"blake2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lockchain-core"
-version = "0.6.0-alpha"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bcrypt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"blake2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lockchain-crypto"
-version = "0.3.0-alpha"
+version = "0.3.0"
dependencies = [
- "lockchain-core 0.6.0-alpha",
+ "lockchain-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"miscreant 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lockchain-files"
-version = "0.0.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
+version = "0.1.0"
dependencies = [
- "lockchain-core 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lockchain-core 0.6.0",
]
[[package]]
name = "lockchain-files"
version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "lockchain-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lockchain-core 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -635,8 +635,8 @@ name = "lockchain-server"
version = "0.1.0"
dependencies = [
"clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "lockchain-core 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "lockchain-files 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lockchain-core 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lockchain-files 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lockchain-http 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -933,7 +933,7 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -943,12 +943,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
-version = "1.0.50"
+version = "1.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_derive"
-version = "1.0.50"
+version = "1.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -963,7 +963,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1281,7 +1281,7 @@ dependencies = [
[[package]]
name = "unicode-normalization"
-version = "0.1.6"
+version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -1457,9 +1457,9 @@ dependencies = [
"checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b"
"checksum linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70fb39025bc7cdd76305867c4eccf2f2dcf6e9a57f5b21a93e1c2d86cd03ec9e"
"checksum lockchain-core 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8cc843193c0f63da17232b600423a6da3117a86ed43b91652ab81300d017f0c1"
-"checksum lockchain-core 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d8734dcfea6d8b3a58dfcb8acb68ad2beac6e3dc3c31b50d3b4352d1317c5052"
"checksum lockchain-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "35d7f2d6b5f34326a3ed5d7212a8a7109aeee0cd522dd6aaf6a953cf8d9a66a9"
-"checksum lockchain-files 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c6404bea015818d89ea81e86c6eb463cfc0e0599a04c67bfcfbb285f4ff4133"
+"checksum lockchain-core 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ae784d9429a01f69d1eb546f04b5128bcc2fd3dcbc2ae52613034d3f04d06505"
+"checksum lockchain-files 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8363236595cab498d4a0e93efa893c800c26a230bc39f037cf062e179cdfcfad"
"checksum lockchain-http 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cfef9fc7585ca94e9f5294854d208914fbff1bbd85a7f47a3e335665b08df302"
"checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
"checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2"
@@ -1499,8 +1499,8 @@ dependencies = [
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum semver 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bee2bc909ab2d8d60dab26e8cad85b25d795b14603a0dcb627b78b9d30b6454b"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
-"checksum serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)" = "428d3d818cb94ee037a17bf4f2200db2552e19b1825d33df2196624290716f92"
-"checksum serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)" = "ee76093b16868c4c9c8e5329c3d30745833e35390624019738472bd13e996e79"
+"checksum serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)" = "de4dee3b122edad92d80c66cac8d967ec7f8bf16a3b452247d6eb1dbf83c8f22"
+"checksum serde_derive 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)" = "7149ef7af607b09e0e7df38b1fd74264f08a29a67f604d5cb09d3fbdb1e256bc"
"checksum serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "f3ad6d546e765177cf3dded3c2e424a8040f870083a0e64064746b958ece9cb1"
"checksum skeptic 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c8431f8fca168e2db4be547bd8329eac70d095dff1444fee4b0fa0fabc7df75a"
"checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23"
@@ -1535,7 +1535,7 @@ dependencies = [
"checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d"
"checksum unicase 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "284b6d3db520d67fbe88fd778c21510d1b0ba4a551e5d0fbb023d33405f6de8a"
"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
-"checksum unicode-normalization 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "90d662d111b0dbb08a180f2761026cba648c258023c355954a7c00e00e354636"
+"checksum unicode-normalization 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "6a0180bc61fc5a987082bfa111f4cc95c4caff7f9799f3e46df09163a937aa25"
"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
diff --git a/lockchain-core/Cargo.toml b/lockchain-core/Cargo.toml
index 13fe00a..bdcf202 100644
--- a/lockchain-core/Cargo.toml
+++ b/lockchain-core/Cargo.toml
@@ -5,7 +5,7 @@ documentation = "https://docs.rs/lockchain-core"
homepage = "https://github.com/spacekookie/lockchain/tree/master/lockchain-core"
readme = "README.md"
license = "MIT/X11 OR Apache-2.0"
-version = "0.6.0-alpha"
+version = "0.6.0"
authors = ["Katharina Fey <kookie@spacekookie.de>"]
[dependencies]
diff --git a/lockchain-core/src/crypto/utils.rs b/lockchain-core/src/crypto/utils.rs
index 36e611c..a8ae7c0 100644
--- a/lockchain-core/src/crypto/utils.rs
+++ b/lockchain-core/src/crypto/utils.rs
@@ -28,8 +28,8 @@ pub mod encoding {
/// A hashing utility module
pub mod hashing {
- use blake2::Blake2s;
use blake2::digest::{Input, VariableOutput};
+ use blake2::Blake2s;
const BLAKE_16_LENGTH: usize = 16;
@@ -54,7 +54,7 @@ pub mod hashing {
}
/// Random number utility module for lockchain
-///
+///
/// Provides stateless secure random number and byte generation
pub mod random {
use rand::{thread_rng, Rng};
@@ -84,5 +84,21 @@ pub mod random {
}
return vec;
+
+ }
+ /// A small utility wraper around bcrypt to allow
+ /// easy password checking.
+ pub mod passwd {
+ use bcrypt::{self, DEFAULT_COST};
+
+ /// Create a new password, returning a hash
+ pub fn create(pw: &str) -> Option<String> {
+ Some(bcrypt::hash(pw, DEFAULT_COST).ok()?)
+ }
+
+ /// Verify a password against it's stored hash
+ pub fn verify(pw: &str, hash: &str) -> Option<bool> {
+ bcrypt::verify(pw, hash).ok()
+ }
}
}
diff --git a/lockchain-core/src/traits.rs b/lockchain-core/src/traits.rs
index bddbb71..76c028a 100644
--- a/lockchain-core/src/traits.rs
+++ b/lockchain-core/src/traits.rs
@@ -11,12 +11,12 @@
//! compilation work without external crates but not calling
//! functions at runtime.
-use record::{Record, EncryptedBody, Header, Payload};
+use record::{EncryptedBody, Header, Payload, Record};
use serde::{de::DeserializeOwned, Serialize};
use users::User;
use base64;
-use serde_json;
+use serde_json::{self, Error as SerdeError};
/// A Body trait that can be implemented to hook into the generic Record
/// data module.
@@ -87,7 +87,10 @@ pub trait Loading {
/// crate.
///
/// The body backend is being being generic with the `Body` trait.
-pub trait Vault<T> where T: Body {
+pub trait Vault<T>
+where
+ T: Body,
+{
/// A shared constructor for all vault implementations
fn new(name: &str, location: &str) -> Self;
/// Fetch metadata headers for all records
@@ -112,12 +115,12 @@ pub trait Vault<T> where T: Body {
/// Auto-implement this trait to serialise types to json
pub trait AutoEncoder: Serialize + DeserializeOwned {
- fn encode(&self) -> String {
- serde_json::to_string_pretty(self).unwrap()
+ fn encode(&self) -> Result<String, SerdeError> {
+ serde_json::to_string_pretty(self)
}
- fn decode(s: &str) -> Self {
- serde_json::from_str(s).unwrap()
+ fn decode(s: &str) -> Result<Self, SerdeError> {
+ serde_json::from_str(s)
}
}
diff --git a/lockchain-crypto/Cargo.toml b/lockchain-crypto/Cargo.toml
index 802ce6d..6c2c12d 100644
--- a/lockchain-crypto/Cargo.toml
+++ b/lockchain-crypto/Cargo.toml
@@ -5,11 +5,11 @@ documentation = "https://docs.rs/lockchain-crypto"
homepage = "https://github.com/spacekookie/lockchain/tree/master/lockchain-crypto"
readme = "README.md"
license = "MIT/X11 OR Apache-2.0"
-version = "0.3.0-alpha"
+version = "0.3.0"
authors = ["Katharina Fey <kookie@spacekookie.de>"]
[dependencies]
-lockchain-core = { path = "../lockchain-core" }
+lockchain-core = { version = "0.5.0" } # path = "../lockchain-core", }
serde_derive = "1.0"
serde = "1.0"
diff --git a/lockchain-files/Cargo.toml b/lockchain-files/Cargo.toml
index cdcfd41..8bf63f5 100644
--- a/lockchain-files/Cargo.toml
+++ b/lockchain-files/Cargo.toml
@@ -10,8 +10,4 @@ readme = "README.md"
license = "MIT/X11 OR Apache-2.0"
[dependencies]
-lockchain-core = { version = ">=0.5.0" }
-
-serde = "1.0"
-serde_json = "1.0"
-serde_derive = "1.0" \ No newline at end of file
+lockchain-core = { path = "../lockchain-core" } #, version = "0.6.0" }
diff --git a/lockchain-files/src/fs.rs b/lockchain-files/src/fs.rs
index 7c9d44a..dfa08d1 100644
--- a/lockchain-files/src/fs.rs
+++ b/lockchain-files/src/fs.rs
@@ -1,9 +1,9 @@
//! Utility module which handles filesystem writes
-use std::path::PathBuf;
-use std::fs::{self, OpenOptions};
use lcc::traits::AutoEncoder;
-use lcc::Record;
+use std::fs::{self, File};
+use std::io::{self, Read};
+use std::path::PathBuf;
pub struct Filesystem {
name: String,
@@ -15,10 +15,10 @@ pub struct Filesystem {
pub enum FileType {
/// A data record file
Record,
- /// A vault/ zser metadata file
+ /// A vault/ zser metadata file
Metadata,
/// A simple checksum file
- Checksum
+ Checksum,
}
impl Filesystem {
@@ -44,12 +44,47 @@ impl Filesystem {
}
/// Load all files of a certain type into a Vec<String>
- pub fn fetch<T: AutoEncoder>(&self, types: FileType) -> Vec<T> {
+ pub fn fetch<T: AutoEncoder>(&self, types: FileType) -> Result<Vec<T>, io::Error> {
+ Ok(fs::read_dir(match types {
+ FileType::Record => self.root.join("records"),
+ _ => self.root.join("."),
+ })?.into_iter()
+ .filter_map(|r| r.ok())
+ .filter(|f| match f.file_type() {
+ Ok(vf) => vf.is_file(),
+ _ => false,
+ })
+ .map(|de| de.path())
+ .filter_map(|p| p.into_os_string().into_string().ok())
+ .filter_map(|s| File::open(s).ok())
+ .filter_map(|mut f| f.get_string().ok())
+ .filter_map(|s| T::decode(&s).ok())
+ .collect())
+ }
+
+ pub fn pull<T: AutoEncoder>(&self, types: FileType, id: &str) -> Result<T, io::Error> {
unimplemented!()
}
- /// Load a single file of a certain type
- pub fn pull<T: AutoEncoder>(&self, ftype: FileType, id: &str) -> T {
+ pub fn sync<T: AutoEncoder>(&self, types: FileType) -> Result<(), io::Error> {
unimplemented!()
}
}
+
+/// A utility trait to read the conents from a file in
+/// a single line.
+pub trait FileToString {
+ /// Read the file contents into a string without any
+ /// error handling.
+ fn get_string(&mut self) -> Result<String, io::Error>;
+}
+
+impl FileToString for File {
+ fn get_string(&mut self) -> Result<String, io::Error> {
+ let mut s = String::new();
+ return match self.read_to_string(&mut s) {
+ Ok(_) => Ok(s),
+ Err(e) => Err(e),
+ };
+ }
+}
diff --git a/lockchain-files/src/lib.rs b/lockchain-files/src/lib.rs
index d42c222..2bf7422 100644
--- a/lockchain-files/src/lib.rs
+++ b/lockchain-files/src/lib.rs
@@ -5,7 +5,7 @@
extern crate lockchain_core as lcc;
-use lcc::traits::{AutoEncoder, Body, Vault};
+use lcc::traits::{Body, Vault};
use lcc::{Payload, Record};
use std::collections::HashMap;
@@ -34,10 +34,12 @@ impl<T: Body> Vault<T> for DataVault<T> {
}.initialize()
}
+ /// Caches all files from disk to memory
fn fetch(&mut self) {
self.records.clear();
self.fs
.fetch::<Record<T>>(FileType::Record)
+ .unwrap()
.into_iter()
.map(|rec| (rec.header.name.clone(), rec))
.for_each(|x| {
@@ -45,35 +47,41 @@ impl<T: Body> Vault<T> for DataVault<T> {
});
}
+ /// Make sure a single record is loaded
fn pull(&mut self, name: &str) {
- unimplemented!()
+ self.records.remove(name);
+ self.records.insert(
+ name.to_owned(),
+ self.fs.pull::<Record<T>>(FileType::Record, name).unwrap(),
+ );
}
fn sync(&mut self) {
- unimplemented!()
+ self.fs.sync::<Record<T>>(FileType::Record).unwrap();
}
fn get_record(&self, name: &str) -> Option<&Record<T>> {
- unimplemented!()
+ self.records.get(name)
}
fn contains(&self, name: &str) -> bool {
- unimplemented!()
+ self.records.contains_key(name)
}
fn add_record(&mut self, key: &str, category: &str, tags: Vec<&str>) {
- unimplemented!()
+ self.records
+ .insert(key.to_owned(), Record::new(key, category, tags));
}
fn delete_record(&mut self, record: &str) -> Option<Record<T>> {
- unimplemented!()
+ self.records.remove(record)
}
fn add_data(&mut self, record: &str, key: &str, data: Payload) -> Option<()> {
- unimplemented!()
+ self.records.get_mut(record)?.add_data(key, data)
}
fn get_data(&self, record: &str, key: &str) -> Option<&Payload> {
- unimplemented!()
+ self.records.get(record)?.get_data(key)
}
}