holochain_integrity_types/
entry_def.rs1use holochain_serialized_bytes::prelude::*;
2use std::borrow::Borrow;
3use std::borrow::Cow;
4
5const DEFAULT_REQUIRED_VALIDATIONS: u8 = 5;
6
7#[derive(
8 Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
9)]
10pub enum EntryDefId {
11 App(AppEntryName),
12 CapClaim,
13 CapGrant,
14}
15
16#[derive(
19 Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
20)]
21pub struct AppEntryName(pub Cow<'static, str>);
22
23pub trait EntryDefRegistration {
30 const ENTRY_DEFS: &'static [EntryDef];
33}
34
35#[derive(
38 Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
39)]
40pub struct RequiredValidations(pub u8);
41
42#[derive(
43 Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
44)]
45pub struct EntryDef {
46 pub id: EntryDefId,
48 pub visibility: EntryVisibility,
50 pub required_validations: RequiredValidations,
52 pub cache_at_agent_activity: bool,
57}
58
59#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
61pub struct EntryDefs(pub Vec<EntryDef>);
62
63#[derive(
64 Clone,
65 Copy,
66 Debug,
67 PartialEq,
68 Eq,
69 PartialOrd,
70 Ord,
71 Hash,
72 Serialize,
73 Deserialize,
74 SerializedBytes,
75)]
76pub enum EntryVisibility {
77 Public,
78 Private,
79}
80
81#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, SerializedBytes)]
82pub enum EntryDefsCallbackResult {
83 Defs(EntryDefs),
84}
85
86impl AppEntryName {
87 pub fn new(s: impl Into<Cow<'static, str>>) -> Self {
89 Self(s.into())
90 }
91 pub const fn from_str(s: &'static str) -> Self {
93 Self(Cow::Borrowed(s))
94 }
95}
96
97impl Default for EntryVisibility {
98 fn default() -> Self {
99 Self::Public
100 }
101}
102
103impl From<u8> for RequiredValidations {
104 fn from(u: u8) -> Self {
105 Self(u)
106 }
107}
108
109impl From<RequiredValidations> for u8 {
110 fn from(required_validations: RequiredValidations) -> Self {
111 required_validations.0
112 }
113}
114
115impl Default for RequiredValidations {
116 fn default() -> Self {
117 Self(DEFAULT_REQUIRED_VALIDATIONS)
118 }
119}
120
121impl EntryVisibility {
122 pub fn is_public(&self) -> bool {
124 *self == EntryVisibility::Public
125 }
126}
127
128impl EntryDef {
129 pub fn new(
130 id: EntryDefId,
131 visibility: EntryVisibility,
132 required_validations: RequiredValidations,
133 cache_at_agent_activity: bool,
134 ) -> Self {
135 Self {
136 id,
137 visibility,
138 required_validations,
139 cache_at_agent_activity,
140 }
141 }
142
143 #[cfg(any(test, feature = "test_utils"))]
144 pub fn default_from_id<I: Into<EntryDefId>>(id: I) -> Self {
145 EntryDef {
146 id: id.into(),
147 ..Default::default()
148 }
149 }
150}
151
152impl std::ops::Index<usize> for EntryDefs {
153 type Output = EntryDef;
154 fn index(&self, i: usize) -> &Self::Output {
155 &self.0[i]
156 }
157}
158
159impl IntoIterator for EntryDefs {
160 type Item = EntryDef;
161 type IntoIter = std::vec::IntoIter<Self::Item>;
162 fn into_iter(self) -> Self::IntoIter {
163 self.0.into_iter()
164 }
165}
166
167impl From<Vec<EntryDef>> for EntryDefs {
168 fn from(v: Vec<EntryDef>) -> Self {
169 Self(v)
170 }
171}
172
173impl From<Vec<EntryDef>> for EntryDefsCallbackResult {
174 fn from(v: Vec<EntryDef>) -> Self {
175 Self::Defs(v.into())
176 }
177}
178
179impl From<String> for EntryDefId {
180 fn from(s: String) -> Self {
181 Self::App(s.into())
182 }
183}
184
185impl From<&str> for EntryDefId {
186 fn from(s: &str) -> Self {
187 Self::App(s.to_string().into())
188 }
189}
190
191impl From<&'static str> for AppEntryName {
192 fn from(s: &'static str) -> Self {
193 Self(Cow::Borrowed(s))
194 }
195}
196
197impl From<String> for AppEntryName {
198 fn from(s: String) -> Self {
199 Self(Cow::Owned(s))
200 }
201}
202
203impl From<AppEntryName> for EntryDefId {
204 fn from(name: AppEntryName) -> Self {
205 EntryDefId::App(name)
206 }
207}
208
209impl Borrow<str> for AppEntryName {
210 fn borrow(&self) -> &str {
211 self.0.borrow()
212 }
213}
214
215impl std::fmt::Display for AppEntryName {
216 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
217 self.0.fmt(f)
218 }
219}
220
221#[cfg(any(test, feature = "test_utils"))]
222impl Default for EntryDef {
223 fn default() -> Self {
224 Self {
225 id: EntryDefId::App(AppEntryName(Default::default())),
226 visibility: Default::default(),
227 required_validations: Default::default(),
228 cache_at_agent_activity: false,
229 }
230 }
231}