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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! holochain_agent provides a library for managing holochain agent info, including identities, keys etc..
extern crate holochain_core_types;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate serde;

use holochain_core_types::{
    cas::content::{AddressableContent, Content},
    entry::{Entry, ToEntry},
    entry_type::EntryType,
};

/// Object holding an Agent's identity.
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct Identity(Content);

impl Identity {
    pub fn new(content: Content) -> Self {
        Identity(content)
    }
}

impl ToString for Identity {
    fn to_string(&self) -> String {
        self.0.to_string()
    }
}

impl From<String> for Identity {
    fn from(s: String) -> Identity {
        Identity::new(s)
    }
}

/// Object holding all Agent's data.
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct Agent(Identity);

impl Agent {
    pub fn new(id: Identity) -> Self {
        Agent(id)
    }
}

impl ToString for Agent {
    fn to_string(&self) -> String {
        self.0.to_string()
    }
}

impl From<String> for Agent {
    fn from(s: String) -> Agent {
        Agent::new(Identity::from(s))
    }
}

impl ToEntry for Agent {
    fn to_entry(&self) -> Entry {
        Entry::new(&EntryType::AgentId, &self.to_string())
    }

    fn from_entry(entry: &Entry) -> Self {
        assert_eq!(&EntryType::AgentId, entry.entry_type());
        Agent::from(entry.value().to_owned())
    }
}

impl AddressableContent for Agent {
    fn content(&self) -> Content {
        self.to_entry().content()
    }

    fn from_content(content: &Content) -> Self {
        Agent::from_entry(&Entry::from_content(content))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use holochain_core_types::cas::content::Content;

    pub fn test_identity_value() -> Content {
        "bob".to_string()
    }

    pub fn test_identity() -> Identity {
        Identity(test_identity_value())
    }

    pub fn test_agent() -> Agent {
        Agent(test_identity())
    }

    #[test]
    /// smoke test new identities
    fn new_identity_test() {
        test_identity();
    }

    #[test]
    /// smoke test new agents
    fn new_agent_test() {
        test_agent();
    }

    #[test]
    /// show ToString implementation for Identity
    fn identity_to_string_test() {
        assert_eq!(test_identity_value(), test_identity().to_string(),);
    }

    #[test]
    /// show ToString implementation for Agent
    fn agent_to_string_test() {
        assert_eq!(test_identity_value(), test_agent().to_string(),)
    }

    #[test]
    /// show ToEntry implementation for Agent
    fn agent_to_entry_test() {
        // to_entry()
        assert_eq!(
            Entry::new(&EntryType::AgentId, &test_identity_value()),
            test_agent().to_entry(),
        );

        // from_entry()
        assert_eq!(
            test_agent(),
            Agent::from_entry(&Entry::new(&EntryType::AgentId, &test_identity_value())),
        );
    }

    #[test]
    /// show AddressableContent implementation for Agent
    fn agent_addressable_content_test() {
        let expected_content = String::from("{\"value\":\"bob\",\"entry_type\":\"AgentId\"}");
        // content()
        assert_eq!(expected_content, test_agent().content(),);

        // from_content()
        assert_eq!(test_agent(), Agent::from_content(&expected_content),);
    }
}