aboutsummaryrefslogtreecommitdiff
path: root/lockchain-crypto/src/lib.rs
blob: 56481065c51036c97da7791f4c78206e13cf2de5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//!

#[macro_use]
extern crate serde_derive;
extern crate serde;

extern crate lockchain_core as lcc;

use lcc::{Payload, EncryptedBody};
use lcc::traits::{Body, AutoEncoder, Base64AutoEncoder, Encryption};

use std::collections::BTreeMap;

#[derive(Serialize, Deserialize)]
pub struct DataBody {
    tree: BTreeMap<String, Payload>,
}

impl AutoEncoder for DataBody {}

impl Body for DataBody {
    fn get_field(&self, key: &str) -> Option<&Payload> {
        self.tree.get(key).as_ref()?
    }

    fn set_field(&mut self, key: &str, value: Payload) -> Option<()> {
        self.tree.insert(key, value);
        Some(())
    }

    fn flatten(&mut self) -> Option<()> {
        None
    }
}

impl Encryption for DataBody {
    fn encrypt(&mut self) -> EncryptedBody {
        EncryptedBody {
            cipher: self.encode()
        }
    }
}