aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Sabel <katharina.sabel@asquera.de>2018-05-07 16:10:54 +0200
committerKatharina Sabel <katharina.sabel@asquera.de>2018-05-07 16:10:54 +0200
commitb506e5968cc257f256a36064a309b4c2f19e37ab (patch)
treeefe78196b73fd8eb1983b8fc57a3cb8e4913ea06
parentcb7612ae9e1bcea664f3e320c7e5ba0cc95cffa0 (diff)
Fleshing out basic traits provided by lockchain-core
Diffstat (limited to '')
-rw-r--r--lockchain-core/README.md13
-rw-r--r--lockchain-core/src/record.rs20
-rw-r--r--lockchain-core/src/traits.rs13
-rw-r--r--lockchain-core/src/vault.rs3
4 files changed, 33 insertions, 16 deletions
diff --git a/lockchain-core/README.md b/lockchain-core/README.md
index cdb78c5..cdb20ad 100644
--- a/lockchain-core/README.md
+++ b/lockchain-core/README.md
@@ -12,13 +12,13 @@ The core types provided by this crate are
- `Record<T>`
- `Body`
-The last one is a trait with an associated type `Impl` which should be `Self`. This then leaves feature implementations (encrypted data, clear-text data, etc) open to the user (you 😆). Additionally we have more types to round out a vault (`Payload`, `Header`, etc).
+The last one is a trait that provides a few shared functions for backend implementations. Some might want to only keep encrypted data around while others could selectively query different fields. Some have more advanced versioning, some don't, etc. Additionally we have more types to round out a vault (`Payload`, `Header`, etc).
A simple implementation of a body type could look like this.
```rust
extern crate lockchain_core as lockchain;
-use lockchain::traits::Body;
+use lockchain::traits::{Body, Encryption};
#[derive(Serialize, Deserialize)]
struct EncryptedBody {
@@ -26,19 +26,22 @@ struct EncryptedBody {
}
impl Body for EncryptedBody {
- type Impl = Self;
// ... function implementations ...
}
+
+impl Encryption for EncryptedBody {
+ // ... implement the functions ...
+}
```
## License
-`barrel` is free software: you can redistribute it and/or modify it under the terms of the MIT Public License.
+`lockchain` is free software: you can redistribute it and/or modify it under the terms of the MIT Public License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MIT Public License for more details.
## Conduct
-In the interest of fostering an open and welcoming environment, the `barrel` project pledges to making participation a harassment-free experience for everyone. See [Code of Conduct](../code_of_conduct.md) for details. In case of violations, e-mail [kookie@spacekookie.de](mailto:kookie@spacekookie.de).
+In the interest of fostering an open and welcoming environment, the `lockchain` project pledges to making participation a harassment-free experience for everyone. See [Code of Conduct](../code_of_conduct.md) for details. In case of violations, e-mail [kookie@spacekookie.de](mailto:kookie@spacekookie.de).
diff --git a/lockchain-core/src/record.rs b/lockchain-core/src/record.rs
index 9a5d9ef..717aefb 100644
--- a/lockchain-core/src/record.rs
+++ b/lockchain-core/src/record.rs
@@ -11,7 +11,9 @@
use chrono::{DateTime, Local};
use std::collections::BTreeMap;
-use traits::Body;
+use serde::{de::DeserializeOwned, Serialize};
+
+// use traits::Body;
/// An enum that wraps around all possible data types to store
/// as the value of a vault record.
@@ -62,5 +64,19 @@ pub struct Header {
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub struct Record<T: Body> {
pub header: Header,
- pub body: Option<Box<T::Impl>>,
+ #[serde(bound(deserialize = "T: Body"))]
+ pub body: Option<T>,
+}
+
+/// A Body trait that can be implemented to hook into the generic Record
+/// data module.
+///
+/// This allows working with both encrypted and cleartext data bodies.
+pub trait Body: DeserializeOwned + Serialize {
+ ///Get the value of a field from this body
+ fn get_field(&self, key: &str) -> Option<Payload>;
+ /// Set the value of a field
+ fn set_field(&mut self, key: &str, value: &Payload);
+ /// Remove versioning and flatten the data tree to a single level.
+ fn flatten(&mut self);
}
diff --git a/lockchain-core/src/traits.rs b/lockchain-core/src/traits.rs
index ad08ff8..bb89f07 100644
--- a/lockchain-core/src/traits.rs
+++ b/lockchain-core/src/traits.rs
@@ -19,7 +19,6 @@ use record::{Header, Payload};
///
/// This allows working with both encrypted and cleartext data bodies.
pub trait Body: DeserializeOwned + Serialize {
- type Impl: Serialize + DeserializeOwned;
///Get the value of a field from this body
fn get_field(&self, key: &str) -> Option<Payload>;
/// Set the value of a field
@@ -31,25 +30,25 @@ pub trait Body: DeserializeOwned + Serialize {
/// A simple trait that allows libraries to hook into the
/// `body()` and `record()` hooks for vault records.
-pub trait LoadRecord {
+pub trait LoadRecord<T: Body> {
fn header() -> Header {
unimplemented!()
}
- // fn body() -> Body {
- // unimplemented!()
- // }
+ fn body() -> T {
+ unimplemented!()
+ }
}
/// This is a trait which needs to be implemented by any
/// backend which hopes to do encryption on data.
-pub trait Encryption {
+pub trait Encryption: Body {
fn encrypt(&mut self) -> Vec<u8> {
unimplemented!()
}
- fn decrypt(_: Vec<u8>) -> Box<Self> {
+ fn decrypt(_data: Vec<u8>) -> Box<Self> {
unimplemented!()
}
}
diff --git a/lockchain-core/src/vault.rs b/lockchain-core/src/vault.rs
index 00882d9..4825174 100644
--- a/lockchain-core/src/vault.rs
+++ b/lockchain-core/src/vault.rs
@@ -10,8 +10,7 @@
use std::collections::HashMap;
-use record::{Payload, Record};
-use traits::Body;
+use record::{Payload, Record, Body};
/// In-memory representation of a lockchain vault.
///