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
use std::borrow::Borrow;
use std::borrow::Cow;
use holochain_serialized_bytes::prelude::*;
const DEFAULT_REQUIRED_VALIDATIONS: u8 = 5;
#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub enum EntryDefId {
App(AppEntryDefName),
CapClaim,
CapGrant,
}
#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct AppEntryDefName(pub Cow<'static, str>);
pub trait EntryDefRegistration {
const ENTRY_DEFS: &'static [EntryDef];
}
#[derive(
Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct RequiredValidations(pub u8);
#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct EntryDef {
pub id: EntryDefId,
pub visibility: EntryVisibility,
pub required_validations: RequiredValidations,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct EntryDefs(pub Vec<EntryDef>);
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize,
SerializedBytes,
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum EntryVisibility {
Public,
Private,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, SerializedBytes)]
pub enum EntryDefsCallbackResult {
Defs(EntryDefs),
}
impl AppEntryDefName {
pub fn new(s: impl Into<Cow<'static, str>>) -> Self {
Self(s.into())
}
pub const fn from_str(s: &'static str) -> Self {
Self(Cow::Borrowed(s))
}
}
impl Default for EntryVisibility {
fn default() -> Self {
Self::Public
}
}
impl From<u8> for RequiredValidations {
fn from(u: u8) -> Self {
Self(u)
}
}
impl From<RequiredValidations> for u8 {
fn from(required_validations: RequiredValidations) -> Self {
required_validations.0
}
}
impl Default for RequiredValidations {
fn default() -> Self {
Self(DEFAULT_REQUIRED_VALIDATIONS)
}
}
impl EntryVisibility {
pub fn is_public(&self) -> bool {
*self == EntryVisibility::Public
}
}
impl EntryDef {
pub fn new(
id: EntryDefId,
visibility: EntryVisibility,
required_validations: RequiredValidations,
) -> Self {
Self {
id,
visibility,
required_validations,
}
}
#[cfg(any(test, feature = "test_utils"))]
pub fn default_with_id<I: Into<EntryDefId>>(id: I) -> Self {
EntryDef::new(id.into(), Default::default(), Default::default())
}
}
impl std::ops::Index<usize> for EntryDefs {
type Output = EntryDef;
fn index(&self, i: usize) -> &Self::Output {
&self.0[i]
}
}
impl IntoIterator for EntryDefs {
type Item = EntryDef;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl From<Vec<EntryDef>> for EntryDefs {
fn from(v: Vec<EntryDef>) -> Self {
Self(v)
}
}
impl From<Vec<EntryDef>> for EntryDefsCallbackResult {
fn from(v: Vec<EntryDef>) -> Self {
Self::Defs(v.into())
}
}
impl From<String> for EntryDefId {
fn from(s: String) -> Self {
Self::App(s.into())
}
}
impl From<&str> for EntryDefId {
fn from(s: &str) -> Self {
Self::App(s.to_string().into())
}
}
impl From<&'static str> for AppEntryDefName {
fn from(s: &'static str) -> Self {
Self(Cow::Borrowed(s))
}
}
impl From<String> for AppEntryDefName {
fn from(s: String) -> Self {
Self(Cow::Owned(s))
}
}
impl From<AppEntryDefName> for EntryDefId {
fn from(name: AppEntryDefName) -> Self {
EntryDefId::App(name)
}
}
impl Borrow<str> for AppEntryDefName {
fn borrow(&self) -> &str {
self.0.borrow()
}
}
impl std::fmt::Display for AppEntryDefName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[cfg(any(test, feature = "test_utils"))]
impl Default for EntryDef {
fn default() -> Self {
Self {
id: EntryDefId::App(AppEntryDefName(Default::default())),
visibility: Default::default(),
required_validations: Default::default(),
}
}
}