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
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;
use regex::Regex;
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(),
}
}
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))
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum LinkActionKind {
ADD,
REMOVE,
}
pub enum LinkMatch<S: Into<String>> {
Any,
Exactly(S),
Regex(S),
}
impl<S: Into<String>> LinkMatch<S> {
#[allow(clippy::wrong_self_convention)]
pub fn to_regex_string(self) -> Result<String, String> {
let re_string: String = match self {
LinkMatch::Any => ".*".into(),
LinkMatch::Exactly(s) => "^".to_owned() + ®ex::escape(&s.into()) + "$",
LinkMatch::Regex(s) => s.into(),
};
match Regex::new(&re_string) {
Ok(_) => Ok(re_string),
Err(_) => Err("Invalid regex passed to get_links".into()),
}
}
}
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
}