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
//! This module contains definitions for what a Link is in Holochain, as well as
//! structs relating to the adding and removing of links between entries
//! and lists of links.

pub mod link_data;
pub mod link_list;

use holochain_json_api::{error::JsonError, json::JsonString};
use holochain_persistence_api::cas::content::Address;

use crate::{
    agent::AgentId,
    chain_header::ChainHeader,
    entry::{test_entry_a, test_entry_b},
};
use entry::Entry;
use holochain_persistence_api::cas::content::AddressableContent;
use link::link_data::LinkData;

type LinkType = String;
type LinkTag = String;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash, DefaultJson)]
pub struct Link {
    base: Address,
    target: Address,
    link_type: LinkType,
    tag: LinkTag,
}

impl Link {
    pub fn new(base: &Address, target: &Address, link_type: &str, tag: &str) -> Self {
        Link {
            base: base.to_owned(),
            target: target.to_owned(),
            link_type: link_type.to_owned(),
            tag: tag.to_owned(),
        }
    }

    // Getters
    pub fn base(&self) -> &Address {
        &self.base
    }

    pub fn target(&self) -> &Address {
        &self.target
    }

    pub fn link_type(&self) -> &LinkType {
        &self.link_type
    }

    pub fn tag(&self) -> &LinkTag {
        &self.tag
    }

    pub fn add_entry(&self, top_chain_header: ChainHeader, agent_id: AgentId) -> Entry {
        Entry::LinkAdd(LinkData::add_from_link(self, top_chain_header, agent_id))
    }

    pub fn remove_entry(&self, top_chain_header: ChainHeader, agent_id: AgentId) -> Entry {
        Entry::LinkAdd(LinkData::remove_from_link(self, top_chain_header, agent_id))
    }
}

// HC.LinkAction sync with hdk-rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum LinkActionKind {
    ADD,
    REMOVE,
}

pub enum LinkMatch<S: Into<String>> {
    Any,
    Exactly(S),
}

impl<S: Into<String>> Into<Option<String>> for LinkMatch<S> {
    fn into(self) -> Option<String> {
        match self {
            LinkMatch::Exactly(s) => Some(s.into()),
            LinkMatch::Any => None,
        }
    }
}

pub fn example_link() -> Link {
    Link::new(
        &test_entry_a().address(),
        &test_entry_b().address(),
        &example_link_type(),
        &example_link_tag(),
    )
}

pub fn example_link_type() -> LinkType {
    LinkType::from("foo-link-type")
}

pub fn example_link_tag() -> LinkTag {
    LinkTag::from("foo-link-tag")
}

pub fn example_link_action_kind() -> LinkActionKind {
    LinkActionKind::ADD
}