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
use hdk::prelude::*;
use std::fmt;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
#[serde(from = "UIEnum")]
#[serde(into = "UIEnum")]
pub enum ActionType {
Create,
Update,
Delete,
}
#[derive(Debug, Serialize, Deserialize, SerializedBytes, Clone, PartialEq)]
struct UIEnum(String);
impl From<UIEnum> for ActionType {
fn from(ui_enum: UIEnum) -> Self {
match ui_enum.0.as_str() {
"create" => Self::Create,
"update" => Self::Update,
_ => Self::Delete,
}
}
}
impl From<ActionType> for UIEnum {
fn from(action_type: ActionType) -> Self {
Self(action_type.to_string().to_lowercase())
}
}
impl fmt::Display for ActionType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub fn create_receive_signal_cap_grant() -> ExternResult<()> {
let mut functions: GrantedFunctions = BTreeSet::new();
functions.insert((zome_info()?.zome_name, "recv_remote_signal".into()));
create_cap_grant(CapGrantEntry {
tag: "".into(),
access: ().into(),
functions,
})?;
Ok(())
}
#[macro_export]
macro_rules! signal_types {
(
$crud_type:ident
) => {
::paste::paste! {
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
#[serde(untagged)]
pub enum [<$crud_type SignalData>] {
Create([<$crud_type WireEntry>]),
Update([<$crud_type WireEntry>]),
Delete($crate::WrappedHeaderHash),
}
#[doc = " then `data` should be [" $crud_type "SignalData::Create]."]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
pub struct [<$crud_type Signal>] {
pub entry_type: String,
pub action: $crate::ActionType,
pub data: [<$crud_type SignalData>],
}
}
};
}
#[cfg(test)]
mod tests {
use super::create_receive_signal_cap_grant;
use ::fixt::prelude::*;
use hdk::prelude::*;
#[test]
fn test_create_receive_signal_cap_grant() {
let mut mock_hdk = MockHdkT::new();
let zome_info = fixt!(ZomeInfo);
mock_hdk
.expect_zome_info()
.times(1)
.return_const(Ok(zome_info.clone()));
let mut functions: GrantedFunctions = BTreeSet::new();
functions.insert((zome_info.zome_name, "recv_remote_signal".into()));
let expected = EntryWithDefId::new(
EntryDefId::CapGrant,
Entry::CapGrant(CapGrantEntry {
tag: "".into(),
access: ().into(),
functions,
}),
);
let header_hash = fixt!(HeaderHash);
mock_hdk
.expect_create()
.with(mockall::predicate::eq(expected))
.times(1)
.return_const(Ok(header_hash));
set_hdk(mock_hdk);
let result = create_receive_signal_cap_grant();
assert_eq!(result.is_ok(), true);
}
}