holochain_integrity_types/
entry.rs1use crate::capability::CapClaim;
9use crate::capability::CapGrant;
10use crate::capability::ZomeCallCapGrant;
11use crate::countersigning::CounterSigningSessionData;
12use crate::AppEntryDef;
13use crate::EntryDefIndex;
14use crate::EntryType;
15use crate::ZomeIndex;
16use holo_hash::hash_type;
17use holo_hash::ActionHash;
18use holo_hash::AgentPubKey;
19use holo_hash::EntryHash;
20use holo_hash::HashableContent;
21use holo_hash::HashableContentBytes;
22use holochain_serialized_bytes::prelude::*;
23
24mod app_entry_bytes;
25mod error;
26pub use app_entry_bytes::*;
27pub use error::*;
28
29pub const ENTRY_SIZE_LIMIT: usize = 4 * 1000 * 1000; pub type CapGrantEntry = ZomeCallCapGrant;
38
39pub type CapClaimEntry = CapClaim;
41
42pub type EntryHashed = holo_hash::HoloHashed<Entry>;
44
45pub trait EntryTypesHelper: Sized {
49 type Error;
51 fn deserialize_from_type<Z, I>(
55 zome_index: Z,
56 entry_def_index: I,
57 entry: &Entry,
58 ) -> Result<Option<Self>, Self::Error>
59 where
60 Z: Into<ZomeIndex>,
61 I: Into<EntryDefIndex>;
62}
63
64impl EntryTypesHelper for () {
65 type Error = core::convert::Infallible;
66
67 fn deserialize_from_type<Z, I>(
68 _zome_index: Z,
69 _entry_def_index: I,
70 _entry: &Entry,
71 ) -> Result<Option<Self>, Self::Error>
72 where
73 Z: Into<ZomeIndex>,
74 I: Into<EntryDefIndex>,
75 {
76 Ok(Some(()))
77 }
78}
79
80impl From<EntryHashed> for Entry {
81 fn from(entry_hashed: EntryHashed) -> Self {
82 entry_hashed.into_content()
83 }
84}
85
86#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, SerializedBytes)]
88#[serde(tag = "entry_type", content = "entry")]
89pub enum Entry {
90 Agent(AgentPubKey),
93 App(AppEntryBytes),
95 CounterSign(Box<CounterSigningSessionData>, AppEntryBytes),
97 CapClaim(CapClaimEntry),
100 CapGrant(CapGrantEntry),
103}
104
105impl Entry {
106 pub fn as_cap_grant(&self) -> Option<CapGrant> {
108 match self {
109 Entry::Agent(key) => Some(CapGrant::ChainAuthor(key.clone())),
110 Entry::CapGrant(data) => Some(CapGrant::RemoteAgent(data.clone())),
111 _ => None,
112 }
113 }
114
115 pub fn as_cap_claim(&self) -> Option<&CapClaim> {
117 match self {
118 Entry::CapClaim(claim) => Some(claim),
119 _ => None,
120 }
121 }
122
123 pub fn as_app_entry(&self) -> Option<&AppEntryBytes> {
125 match self {
126 Entry::App(bytes) => Some(bytes),
127 _ => None,
128 }
129 }
130
131 pub fn app(sb: SerializedBytes) -> Result<Self, EntryError> {
133 Ok(Entry::App(AppEntryBytes::try_from(sb)?))
134 }
135
136 pub fn app_fancy<SB: TryInto<SerializedBytes, Error = SerializedBytesError>>(
138 sb: SB,
139 ) -> Result<Self, EntryError> {
140 Ok(Entry::App(AppEntryBytes::try_from(sb.try_into()?)?))
141 }
142
143 pub fn entry_type(&self, entry_def: Option<AppEntryDef>) -> Option<EntryType> {
146 match (self, entry_def) {
147 (Entry::Agent(_), _) => Some(EntryType::AgentPubKey),
148 (Entry::CapClaim(_), _) => Some(EntryType::CapClaim),
149 (Entry::CapGrant(_), _) => Some(EntryType::CapGrant),
150 (Entry::App(_), Some(aed)) | (Entry::CounterSign(_, _), Some(aed)) => {
151 Some(EntryType::App(aed))
152 }
153 _ => None,
154 }
155 }
156}
157
158impl HashableContent for Entry {
159 type HashType = hash_type::Entry;
160
161 fn hash_type(&self) -> Self::HashType {
162 hash_type::Entry
163 }
164
165 fn hashable_content(&self) -> HashableContentBytes {
166 match self {
167 Entry::Agent(agent_pubkey) => {
168 HashableContentBytes::Prehashed39(
171 EntryHash::from(agent_pubkey.clone()).into_inner(),
172 )
173 }
174 entry => HashableContentBytes::Content(
175 entry
176 .try_into()
177 .expect("Could not serialize HashableContent"),
178 ),
179 }
180 }
181}
182
183#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
185pub struct MustGetValidRecordInput(pub ActionHash);
186
187impl MustGetValidRecordInput {
188 pub fn new(action_hash: ActionHash) -> Self {
190 Self(action_hash)
191 }
192
193 pub fn into_inner(self) -> ActionHash {
195 self.0
196 }
197}
198
199#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
201pub struct MustGetEntryInput(pub EntryHash);
202
203impl MustGetEntryInput {
204 pub fn new(entry_hash: EntryHash) -> Self {
206 Self(entry_hash)
207 }
208
209 pub fn into_inner(self) -> EntryHash {
211 self.0
212 }
213}
214
215#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
217pub struct MustGetActionInput(pub ActionHash);
218
219impl MustGetActionInput {
220 pub fn new(action_hash: ActionHash) -> Self {
222 Self(action_hash)
223 }
224
225 pub fn into_inner(self) -> ActionHash {
227 self.0
228 }
229}