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
//! Plain primitives of the Glycos graph.

use std::fmt::{self, Debug, Formatter};
use std::str;

use crate::crypto::ec::*;

pub struct Vertex {
    pub owner: PublicKey,
    pub value: Option<Vec<u8>>,
    pub clock: u32,
    pub edge_access_key: Option<PrivateKey>,
    // XXX: how to define what access is granted by this key?
}

impl Vertex {
    /// Gives a short representation of this vertex.
    /// This representation is *not* collission resistant.
    pub fn short_hash(&self) -> String {
        self.owner.short_hash()
    }

    pub fn identifier(&self) -> [u8; 32] {
        self.owner.identifier()
    }
}

impl Debug for Vertex {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        let short_hash = self.short_hash();
        if let Some(value) = self.value.as_ref() {
            if let Ok(s) = str::from_utf8(value) {
                write!(f, "<#Vertex {} `{}'>", short_hash, s)
            } else {
                write!(f, "<#Vertex {} invalid-utf8>", short_hash)
            }
        } else {
            write!(f, "<#Vertex {}>", short_hash)
        }
    }
}

pub struct Edge {
    pub subject: PublicKey,
    pub predicate: String,
    pub object: PublicKey,
}

impl Debug for Edge {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        write!(
            f,
            "<#Edge {} -> {}>",
            self.predicate,
            self.object.short_hash()
        )
    }
}

#[cfg(test)]
#[path = "primitives_tests.rs"]
mod tests;