1use crate::hash_type;
4use crate::HashType;
5use crate::HoloHash;
6use crate::PrimitiveHashType;
7
8pub type AgentPubKey = HoloHash<hash_type::Agent>;
15
16pub type ZomeCallSigningKey = AgentPubKey;
18
19pub type DnaHash = HoloHash<hash_type::Dna>;
21
22pub type DhtOpHash = HoloHash<hash_type::DhtOp>;
24
25pub type EntryHash = HoloHash<hash_type::Entry>;
27
28pub type ActionHash = HoloHash<hash_type::Action>;
30
31pub type NetIdHash = HoloHash<hash_type::NetId>;
33
34pub type WasmHash = HoloHash<hash_type::Wasm>;
36
37pub type WarrantHash = HoloHash<hash_type::Warrant>;
39
40pub type ExternalHash = HoloHash<hash_type::External>;
42
43pub type AnyDhtHash = HoloHash<hash_type::AnyDht>;
48
49pub type AnyLinkableHash = HoloHash<hash_type::AnyLinkable>;
51
52pub type OpBasis = AnyLinkableHash;
54
55pub enum AnyDhtHashPrimitive {
57 Entry(EntryHash),
59 Action(ActionHash),
61}
62
63pub enum AnyLinkableHashPrimitive {
65 Entry(EntryHash),
67 Action(ActionHash),
69 External(ExternalHash),
71}
72
73impl AnyLinkableHash {
74 pub fn into_primitive(self) -> AnyLinkableHashPrimitive {
76 match self.hash_type() {
77 hash_type::AnyLinkable::Entry => {
78 AnyLinkableHashPrimitive::Entry(self.retype(hash_type::Entry))
79 }
80 hash_type::AnyLinkable::Action => {
81 AnyLinkableHashPrimitive::Action(self.retype(hash_type::Action))
82 }
83 hash_type::AnyLinkable::External => {
84 AnyLinkableHashPrimitive::External(self.retype(hash_type::External))
85 }
86 }
87 }
88
89 pub fn into_any_dht_hash(self) -> Option<AnyDhtHash> {
91 match self.into_primitive() {
92 AnyLinkableHashPrimitive::Action(hash) => Some(AnyDhtHash::from(hash)),
93 AnyLinkableHashPrimitive::Entry(hash) => Some(AnyDhtHash::from(hash)),
94 AnyLinkableHashPrimitive::External(_) => None,
95 }
96 }
97
98 pub fn into_action_hash(self) -> Option<ActionHash> {
100 if *self.hash_type() == hash_type::AnyLinkable::Action {
101 Some(self.retype(hash_type::Action))
102 } else {
103 None
104 }
105 }
106
107 pub fn into_entry_hash(self) -> Option<EntryHash> {
109 if *self.hash_type() == hash_type::AnyLinkable::Entry {
110 Some(self.retype(hash_type::Entry))
111 } else {
112 None
113 }
114 }
115
116 pub fn into_agent_pub_key(self) -> Option<AgentPubKey> {
122 if *self.hash_type() == hash_type::AnyLinkable::Entry {
123 Some(self.retype(hash_type::Agent))
124 } else {
125 None
126 }
127 }
128
129 pub fn into_external_hash(self) -> Option<ExternalHash> {
131 if *self.hash_type() == hash_type::AnyLinkable::External {
132 Some(self.retype(hash_type::External))
133 } else {
134 None
135 }
136 }
137}
138
139impl AnyDhtHash {
140 pub fn into_primitive(self) -> AnyDhtHashPrimitive {
142 match self.hash_type() {
143 hash_type::AnyDht::Entry => AnyDhtHashPrimitive::Entry(self.retype(hash_type::Entry)),
144 hash_type::AnyDht::Action => {
145 AnyDhtHashPrimitive::Action(self.retype(hash_type::Action))
146 }
147 }
148 }
149
150 pub fn into_action_hash(self) -> Option<ActionHash> {
152 if *self.hash_type() == hash_type::AnyDht::Action {
153 Some(self.retype(hash_type::Action))
154 } else {
155 None
156 }
157 }
158
159 pub fn into_entry_hash(self) -> Option<EntryHash> {
161 if *self.hash_type() == hash_type::AnyDht::Entry {
162 Some(self.retype(hash_type::Entry))
163 } else {
164 None
165 }
166 }
167
168 pub fn into_agent_pub_key(self) -> Option<AgentPubKey> {
174 if *self.hash_type() == hash_type::AnyDht::Entry {
175 Some(self.retype(hash_type::Agent))
176 } else {
177 None
178 }
179 }
180}
181
182impl From<AnyDhtHash> for AnyLinkableHash {
194 fn from(hash: AnyDhtHash) -> Self {
195 let t = (*hash.hash_type()).into();
196 hash.retype(t)
197 }
198}
199
200impl TryFrom<AnyLinkableHash> for AnyDhtHash {
201 type Error = CompositeHashConversionError<hash_type::AnyLinkable>;
202
203 fn try_from(hash: AnyLinkableHash) -> Result<Self, Self::Error> {
204 hash.clone()
205 .into_any_dht_hash()
206 .ok_or_else(|| CompositeHashConversionError(hash, "AnyDht".into()))
207 }
208}
209
210impl From<ActionHash> for AnyDhtHash {
213 fn from(hash: ActionHash) -> Self {
214 hash.retype(hash_type::AnyDht::Action)
215 }
216}
217
218impl From<EntryHash> for AnyDhtHash {
219 fn from(hash: EntryHash) -> Self {
220 hash.retype(hash_type::AnyDht::Entry)
221 }
222}
223
224impl From<AgentPubKey> for AnyDhtHash {
227 fn from(hash: AgentPubKey) -> Self {
228 hash.retype(hash_type::AnyDht::Entry)
229 }
230}
231
232impl TryFrom<AnyDhtHash> for ActionHash {
233 type Error = HashConversionError<hash_type::AnyDht, hash_type::Action>;
234
235 fn try_from(hash: AnyDhtHash) -> Result<Self, Self::Error> {
236 hash.clone()
237 .into_action_hash()
238 .ok_or(HashConversionError(hash, hash_type::Action))
239 }
240}
241
242impl TryFrom<AnyDhtHash> for EntryHash {
243 type Error = HashConversionError<hash_type::AnyDht, hash_type::Entry>;
244
245 fn try_from(hash: AnyDhtHash) -> Result<Self, Self::Error> {
246 hash.clone()
247 .into_entry_hash()
248 .ok_or(HashConversionError(hash, hash_type::Entry))
249 }
250}
251
252impl TryFrom<AnyDhtHash> for AgentPubKey {
255 type Error = HashConversionError<hash_type::AnyDht, hash_type::Agent>;
256
257 fn try_from(hash: AnyDhtHash) -> Result<Self, Self::Error> {
258 hash.clone()
259 .into_agent_pub_key()
260 .ok_or(HashConversionError(hash, hash_type::Agent))
261 }
262}
263
264impl From<ActionHash> for AnyLinkableHash {
267 fn from(hash: ActionHash) -> Self {
268 hash.retype(hash_type::AnyLinkable::Action)
269 }
270}
271
272impl From<EntryHash> for AnyLinkableHash {
273 fn from(hash: EntryHash) -> Self {
274 hash.retype(hash_type::AnyLinkable::Entry)
275 }
276}
277
278impl From<AgentPubKey> for AnyLinkableHash {
279 fn from(hash: AgentPubKey) -> Self {
280 hash.retype(hash_type::AnyLinkable::Entry)
281 }
282}
283
284impl From<ExternalHash> for AnyLinkableHash {
285 fn from(hash: ExternalHash) -> Self {
286 hash.retype(hash_type::AnyLinkable::External)
287 }
288}
289
290impl TryFrom<AnyLinkableHash> for ActionHash {
291 type Error = HashConversionError<hash_type::AnyLinkable, hash_type::Action>;
292
293 fn try_from(hash: AnyLinkableHash) -> Result<Self, Self::Error> {
294 hash.clone()
295 .into_action_hash()
296 .ok_or(HashConversionError(hash, hash_type::Action))
297 }
298}
299
300impl TryFrom<AnyLinkableHash> for EntryHash {
301 type Error = HashConversionError<hash_type::AnyLinkable, hash_type::Entry>;
302
303 fn try_from(hash: AnyLinkableHash) -> Result<Self, Self::Error> {
304 hash.clone()
305 .into_entry_hash()
306 .ok_or(HashConversionError(hash, hash_type::Entry))
307 }
308}
309
310impl TryFrom<AnyLinkableHash> for AgentPubKey {
313 type Error = HashConversionError<hash_type::AnyLinkable, hash_type::Agent>;
314
315 fn try_from(hash: AnyLinkableHash) -> Result<Self, Self::Error> {
316 hash.clone()
317 .into_agent_pub_key()
318 .ok_or(HashConversionError(hash, hash_type::Agent))
319 }
320}
321
322impl TryFrom<AnyLinkableHash> for ExternalHash {
325 type Error = HashConversionError<hash_type::AnyLinkable, hash_type::External>;
326
327 fn try_from(hash: AnyLinkableHash) -> Result<Self, Self::Error> {
328 hash.clone()
329 .into_external_hash()
330 .ok_or(HashConversionError(hash, hash_type::External))
331 }
332}
333
334#[cfg(feature = "serialization")]
335use holochain_serialized_bytes::prelude::*;
336
337#[cfg(feature = "serialization")]
339#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize, SerializedBytes)]
340#[repr(transparent)]
341#[serde(transparent)]
342pub struct EntryHashes(pub Vec<EntryHash>);
343
344#[derive(Debug, Clone, PartialEq, Eq)]
346pub struct HashConversionError<T: HashType, P: PrimitiveHashType>(HoloHash<T>, P);
347
348#[derive(Debug, Clone, PartialEq, Eq)]
350pub struct CompositeHashConversionError<T: HashType>(HoloHash<T>, String);
351
352#[cfg(feature = "holochain-wasmer")]
353use holochain_wasmer_common::WasmErrorInner;
354
355#[cfg(feature = "holochain-wasmer")]
356impl<T: HashType, P: PrimitiveHashType> From<HashConversionError<T, P>> for WasmErrorInner {
357 fn from(err: HashConversionError<T, P>) -> Self {
358 WasmErrorInner::Guest(format!("{:?}", err))
359 }
360}
361
362#[cfg(feature = "holochain-wasmer")]
363impl<T: HashType> From<CompositeHashConversionError<T>> for WasmErrorInner {
364 fn from(err: CompositeHashConversionError<T>) -> Self {
365 WasmErrorInner::Guest(format!("{:?}", err))
366 }
367}