aboutsummaryrefslogtreecommitdiff
path: root/lockchain-core/src/meta.rs
blob: 74b998d907cc1c149442bf4a7c9c43f24204febd (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! A cleartext metadata record for a vault metadata store
//!
//! This MetaItem uses a lot of the same traits and mechanisms
//! as the normal Vault Record, without having to rely on encryption
//! or trait base security.

use record::Payload;
use std::collections::HashMap;
use traits::{AutoEncoder, Body};

/// A simple representation of metadata for a vault or vault section
pub struct VaultMetadata {
    pub name: String,
    pub location: String,
    pub size: usize,
}

/// A metadomain is a simplified version of a cleartext record.
///
/// It is not encoded in special ways, it is not used in any way
/// for secret information. All data inside a metadata file
/// (for example, living inside the `metadata` folder of a vault)
/// is public to all.
///
/// It can be used for things that need to be stored in encrypted form
/// where the encryption key is never present. Or for simple authentication
/// verification such as:
///
/// - User registry
/// - Per-user encrypted primary keys
/// - Usage statistics shared between clients
///
/// It implements a series of traits which means it's possible to easily
/// interact with to store data.
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct MetaDomain {
    /// The name of this meta domain
    name: String,
    body: HashMap<String, Payload>,
}

impl MetaDomain {
    /// Create a new domain space struct
    pub fn new<S>(name: S) -> Self
    where
        S: Into<String>,
    {
        Self {
            name: name.into(),
            body: HashMap::new(),
        }
    }

    /// Return a MetaDomain that is filled with pre-existing data
    pub fn fill(self, new_body: HashMap<String, Payload>) -> Self {
        Self {
            body: new_body,
            ..self
        }
    }

    /// Insert a single value into the body
    pub fn insert<S: Into<String>>(&mut self, key: S, value: Payload) -> &mut Self {
        unimplemented!()
    }

    /// Return a read-only reference to the entire body
    pub fn all(&self) -> &HashMap<String, Payload> {
        &self.body
    }

    /// Return the domain name for easy comparison
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the number of items in this domain
    pub fn size(&self) -> usize {
        self.body.len()
    }
}

impl AutoEncoder for MetaDomain {}

impl Body for MetaDomain {
    fn get_field(&self, name: &str) -> Option<&Payload> {
        self.body.get(name)
    }

    fn set_field(&mut self, key: &str, value: Payload) -> Option<()> {
        self.body
            .insert(key.into(), value)
            .map_or(Some(()), |_| Some(()))
    }

    /// Not implemented, always returns None
    fn flatten(&mut self) -> Option<()> {
        None
    }
}