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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#[macro_export]
macro_rules! crud {
(
$crud_type:ident, $i:ident, $path:expr, $get_peers:ident, $convert_to_receiver_signal:ident
) => {
::paste::paste! {
pub const [<$i:upper _PATH>]: &str = $path;
pub fn [<get_ $i _path>]() -> Path {
Path::from([<$i:upper _PATH>])
}
#[doc = "This data structure will be very broadly useful and represents
how an entry should be serialized along with what metadata to
form a consistent pattern that the UI or client can expect.
It is called `" $crud_type "WireEntry` because it is how data looks passed
'over the wire' or network."]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
pub struct [<$crud_type WireEntry>] {
pub entry: $crud_type,
pub address: $crate::WrappedHeaderHash,
pub entry_address: $crate::WrappedEntryHash
}
impl From<$crate::EntryAndHash<$crud_type>> for [<$crud_type WireEntry>] {
fn from(entry_and_hash: $crate::EntryAndHash<$crud_type>) -> Self {
[<$crud_type WireEntry>] {
entry: entry_and_hash.0,
address: $crate::WrappedHeaderHash(entry_and_hash.1),
entry_address: $crate::WrappedEntryHash(entry_and_hash.2)
}
}
}
$crate::signal_types!($crud_type);
#[doc ="This is what is expected by a call to [update_" $path "] or [inner_update_" $path "]"]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
pub struct [<$crud_type UpdateInput>] {
pub entry: $crud_type,
pub address: $crate::WrappedHeaderHash,
}
#[doc ="This is what will be returned by a call to [fetch_" $path "s] or [inner_fetch_" $path "s]
It is just a list/vec of [" $crud_type "WireEntry]. It is defined for the purposes of serialization."]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
pub struct [<Vec $crud_type WireEntry>](pub Vec<[<$crud_type WireEntry>]>);
#[doc="This will be called with `send_signal` as `true` by [create_" $i "]"]
pub fn [<inner_create_ $i>](entry: $crud_type, send_signal: bool) -> ExternResult<[<$crud_type WireEntry>]> {
let address = create_entry(&entry)?;
let entry_hash = hash_entry(&entry)?;
let path = [< get_ $i _path >]();
path.ensure()?;
let path_hash = path.hash()?;
create_link(path_hash, entry_hash.clone(), ())?;
let wire_entry = [<$crud_type WireEntry>] {
entry,
address: $crate::WrappedHeaderHash(address),
entry_address: $crate::WrappedEntryHash(entry_hash)
};
if (send_signal) {
let signal = $convert_to_receiver_signal([<$crud_type Signal>] {
entry_type: $path.to_string(),
action: $crate::ActionType::Create,
data: [<$crud_type SignalData>]::Create(wire_entry.clone()),
});
let payload = ExternIO::encode(signal)?;
let peers = $get_peers()?;
remote_signal(payload, peers)?;
}
Ok(wire_entry)
}
#[doc="This just calls [inner_create_" $i "] with `send_signal` as `true`."]
#[hdk_extern]
pub fn [<create_ $i>](entry: $crud_type) -> ExternResult<[<$crud_type WireEntry>]> {
[<inner_create_ $i>](entry, true)
}
pub fn [<inner_fetch_ $i s>](fetch_options: $crate::FetchOptions, get_options: GetOptions) -> ExternResult<[<Vec $crud_type WireEntry>]> {
let entries = $crate::fetch_entries::<$crud_type, [<$crud_type WireEntry>]>([< get_ $i _path >](), fetch_options, get_options)?;
Ok([<Vec $crud_type WireEntry>](entries))
}
#[doc="This just calls [inner_fetch_" $i "s]."]
#[hdk_extern]
pub fn [<fetch_ $i s>](fetch_options: $crate::FetchOptions) -> ExternResult<[<Vec $crud_type WireEntry>]> {
[<inner_fetch_ $i s>](fetch_options, GetOptions::latest())
}
pub fn [<inner_update_ $i>](update: [<$crud_type UpdateInput>], send_signal: bool) -> ExternResult<[<$crud_type WireEntry>]> {
update_entry(update.address.0.clone(), &update.entry)?;
let entry_address = hash_entry(&update.entry)?;
let wire_entry = [<$crud_type WireEntry>] {
entry: update.entry,
address: update.address,
entry_address: $crate::WrappedEntryHash(entry_address)
};
if (send_signal) {
let signal = $convert_to_receiver_signal([<$crud_type Signal>] {
entry_type: $path.to_string(),
action: $crate::ActionType::Update,
data: [<$crud_type SignalData>]::Update(wire_entry.clone()),
});
let payload = ExternIO::encode(signal)?;
let peers = $get_peers()?;
remote_signal(payload, peers)?;
}
Ok(wire_entry)
}
#[doc="This just calls [inner_update_" $i "] with `send_signal` as `true`."]
#[hdk_extern]
pub fn [<update_ $i>](update: [<$crud_type UpdateInput>]) -> ExternResult<[<$crud_type WireEntry>]> {
[<inner_update_ $i>](update, true)
}
#[doc="It will no longer be returned by [fetch_" $i "s]."]
#[doc="This will be called with `send_signal` as `true` by [archive_" $i "]"]
pub fn [<inner_archive_ $i>](address: $crate::WrappedHeaderHash, send_signal: bool) -> ExternResult<$crate::WrappedHeaderHash> {
delete_entry(address.0.clone())?;
if (send_signal) {
let signal = $convert_to_receiver_signal([<$crud_type Signal>] {
entry_type: $path.to_string(),
action: $crate::ActionType::Delete,
data: [<$crud_type SignalData>]::Delete(address.clone()),
});
let payload = ExternIO::encode(signal)?;
let peers = $get_peers()?;
remote_signal(payload, peers)?;
}
Ok(address)
}
#[doc="It will no longer be returned by [fetch_" $i "s]."]
#[doc="This just calls [inner_archive_" $i "] with `send_signal` as `true`."]
#[hdk_extern]
pub fn [<archive_ $i>](address: $crate::WrappedHeaderHash) -> ExternResult<$crate::WrappedHeaderHash> {
[<inner_archive_ $i>](address, true)
}
}
};
}
pub mod example {
use hdk::prelude::*;
#[hdk_entry(id = "example")]
#[derive(Clone, PartialEq)]
pub struct Example {
pub number: i32,
}
#[derive(Debug, Serialize, Deserialize, SerializedBytes)]
#[serde(untagged)]
pub enum SignalTypes {
Example(ExampleSignal)
}
pub fn recv_remote_signal(signal: ExternIO) -> ExternResult<()> {
Ok(emit_signal(&signal)?)
}
pub fn convert_to_receiver_signal(signal: ExampleSignal) -> SignalTypes {
SignalTypes::Example(signal)
}
pub fn get_peers() -> ExternResult<Vec<AgentPubKey>> {
Ok(Vec::new())
}
crud!(
Example,
example,
"example",
get_peers,
convert_to_receiver_signal
);
}
#[cfg(test)]
mod tests {
use super::example::*;
#[test]
fn it_works() {
let e: Example = Example { number: 2 };
assert_eq!(e, Example { number: 2 });
}
}