use crate::hash_type;
use crate::HashType;
use crate::HoloHash;
use crate::PrimitiveHashType;
pub type AgentPubKey = HoloHash<hash_type::Agent>;
pub type ZomeCallSigningKey = AgentPubKey;
pub type DnaHash = HoloHash<hash_type::Dna>;
pub type DhtOpHash = HoloHash<hash_type::DhtOp>;
pub type EntryHash = HoloHash<hash_type::Entry>;
pub type ActionHash = HoloHash<hash_type::Action>;
pub type NetIdHash = HoloHash<hash_type::NetId>;
pub type WasmHash = HoloHash<hash_type::Wasm>;
pub type ExternalHash = HoloHash<hash_type::External>;
pub type AnyDhtHash = HoloHash<hash_type::AnyDht>;
pub type AnyLinkableHash = HoloHash<hash_type::AnyLinkable>;
pub type OpBasis = AnyLinkableHash;
pub enum AnyDhtHashPrimitive {
Entry(EntryHash),
Action(ActionHash),
}
pub enum AnyLinkableHashPrimitive {
Entry(EntryHash),
Action(ActionHash),
External(ExternalHash),
}
impl AnyLinkableHash {
pub fn into_primitive(self) -> AnyLinkableHashPrimitive {
match self.hash_type() {
hash_type::AnyLinkable::Entry => {
AnyLinkableHashPrimitive::Entry(self.retype(hash_type::Entry))
}
hash_type::AnyLinkable::Action => {
AnyLinkableHashPrimitive::Action(self.retype(hash_type::Action))
}
hash_type::AnyLinkable::External => {
AnyLinkableHashPrimitive::External(self.retype(hash_type::External))
}
}
}
pub fn into_any_dht_hash(self) -> Option<AnyDhtHash> {
match self.into_primitive() {
AnyLinkableHashPrimitive::Action(hash) => Some(AnyDhtHash::from(hash)),
AnyLinkableHashPrimitive::Entry(hash) => Some(AnyDhtHash::from(hash)),
AnyLinkableHashPrimitive::External(_) => None,
}
}
pub fn into_action_hash(self) -> Option<ActionHash> {
if *self.hash_type() == hash_type::AnyLinkable::Action {
Some(self.retype(hash_type::Action))
} else {
None
}
}
pub fn into_entry_hash(self) -> Option<EntryHash> {
if *self.hash_type() == hash_type::AnyLinkable::Entry {
Some(self.retype(hash_type::Entry))
} else {
None
}
}
pub fn into_agent_pub_key(self) -> Option<AgentPubKey> {
if *self.hash_type() == hash_type::AnyLinkable::Entry {
Some(self.retype(hash_type::Agent))
} else {
None
}
}
pub fn into_external_hash(self) -> Option<ExternalHash> {
if *self.hash_type() == hash_type::AnyLinkable::External {
Some(self.retype(hash_type::External))
} else {
None
}
}
}
impl AnyDhtHash {
pub fn into_primitive(self) -> AnyDhtHashPrimitive {
match self.hash_type() {
hash_type::AnyDht::Entry => AnyDhtHashPrimitive::Entry(self.retype(hash_type::Entry)),
hash_type::AnyDht::Action => {
AnyDhtHashPrimitive::Action(self.retype(hash_type::Action))
}
}
}
pub fn into_action_hash(self) -> Option<ActionHash> {
if *self.hash_type() == hash_type::AnyDht::Action {
Some(self.retype(hash_type::Action))
} else {
None
}
}
pub fn into_entry_hash(self) -> Option<EntryHash> {
if *self.hash_type() == hash_type::AnyDht::Entry {
Some(self.retype(hash_type::Entry))
} else {
None
}
}
pub fn into_agent_pub_key(self) -> Option<AgentPubKey> {
if *self.hash_type() == hash_type::AnyDht::Entry {
Some(self.retype(hash_type::Agent))
} else {
None
}
}
}
impl From<AnyDhtHash> for AnyLinkableHash {
fn from(hash: AnyDhtHash) -> Self {
let t = (*hash.hash_type()).into();
hash.retype(t)
}
}
impl TryFrom<AnyLinkableHash> for AnyDhtHash {
type Error = CompositeHashConversionError<hash_type::AnyLinkable>;
fn try_from(hash: AnyLinkableHash) -> Result<Self, Self::Error> {
hash.clone()
.into_any_dht_hash()
.ok_or_else(|| CompositeHashConversionError(hash, "AnyDht".into()))
}
}
impl From<ActionHash> for AnyDhtHash {
fn from(hash: ActionHash) -> Self {
hash.retype(hash_type::AnyDht::Action)
}
}
impl From<EntryHash> for AnyDhtHash {
fn from(hash: EntryHash) -> Self {
hash.retype(hash_type::AnyDht::Entry)
}
}
impl From<AgentPubKey> for AnyDhtHash {
fn from(hash: AgentPubKey) -> Self {
hash.retype(hash_type::AnyDht::Entry)
}
}
impl TryFrom<AnyDhtHash> for ActionHash {
type Error = HashConversionError<hash_type::AnyDht, hash_type::Action>;
fn try_from(hash: AnyDhtHash) -> Result<Self, Self::Error> {
hash.clone()
.into_action_hash()
.ok_or(HashConversionError(hash, hash_type::Action))
}
}
impl TryFrom<AnyDhtHash> for EntryHash {
type Error = HashConversionError<hash_type::AnyDht, hash_type::Entry>;
fn try_from(hash: AnyDhtHash) -> Result<Self, Self::Error> {
hash.clone()
.into_entry_hash()
.ok_or(HashConversionError(hash, hash_type::Entry))
}
}
impl TryFrom<AnyDhtHash> for AgentPubKey {
type Error = HashConversionError<hash_type::AnyDht, hash_type::Agent>;
fn try_from(hash: AnyDhtHash) -> Result<Self, Self::Error> {
hash.clone()
.into_agent_pub_key()
.ok_or(HashConversionError(hash, hash_type::Agent))
}
}
impl From<ActionHash> for AnyLinkableHash {
fn from(hash: ActionHash) -> Self {
hash.retype(hash_type::AnyLinkable::Action)
}
}
impl From<EntryHash> for AnyLinkableHash {
fn from(hash: EntryHash) -> Self {
hash.retype(hash_type::AnyLinkable::Entry)
}
}
impl From<AgentPubKey> for AnyLinkableHash {
fn from(hash: AgentPubKey) -> Self {
hash.retype(hash_type::AnyLinkable::Entry)
}
}
impl From<ExternalHash> for AnyLinkableHash {
fn from(hash: ExternalHash) -> Self {
hash.retype(hash_type::AnyLinkable::External)
}
}
impl TryFrom<AnyLinkableHash> for ActionHash {
type Error = HashConversionError<hash_type::AnyLinkable, hash_type::Action>;
fn try_from(hash: AnyLinkableHash) -> Result<Self, Self::Error> {
hash.clone()
.into_action_hash()
.ok_or(HashConversionError(hash, hash_type::Action))
}
}
impl TryFrom<AnyLinkableHash> for EntryHash {
type Error = HashConversionError<hash_type::AnyLinkable, hash_type::Entry>;
fn try_from(hash: AnyLinkableHash) -> Result<Self, Self::Error> {
hash.clone()
.into_entry_hash()
.ok_or(HashConversionError(hash, hash_type::Entry))
}
}
impl TryFrom<AnyLinkableHash> for AgentPubKey {
type Error = HashConversionError<hash_type::AnyLinkable, hash_type::Agent>;
fn try_from(hash: AnyLinkableHash) -> Result<Self, Self::Error> {
hash.clone()
.into_agent_pub_key()
.ok_or(HashConversionError(hash, hash_type::Agent))
}
}
impl TryFrom<AnyLinkableHash> for ExternalHash {
type Error = HashConversionError<hash_type::AnyLinkable, hash_type::External>;
fn try_from(hash: AnyLinkableHash) -> Result<Self, Self::Error> {
hash.clone()
.into_external_hash()
.ok_or(HashConversionError(hash, hash_type::External))
}
}
#[cfg(feature = "serialization")]
use holochain_serialized_bytes::prelude::*;
#[cfg(feature = "serialization")]
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize, SerializedBytes)]
#[repr(transparent)]
#[serde(transparent)]
pub struct EntryHashes(pub Vec<EntryHash>);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HashConversionError<T: HashType, P: PrimitiveHashType>(HoloHash<T>, P);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompositeHashConversionError<T: HashType>(HoloHash<T>, String);
#[cfg(feature = "holochain-wasmer")]
use holochain_wasmer_common::WasmErrorInner;
#[cfg(feature = "holochain-wasmer")]
impl<T: HashType, P: PrimitiveHashType> From<HashConversionError<T, P>> for WasmErrorInner {
fn from(err: HashConversionError<T, P>) -> Self {
WasmErrorInner::Guest(format!("{:?}", err))
}
}
#[cfg(feature = "holochain-wasmer")]
impl<T: HashType> From<CompositeHashConversionError<T>> for WasmErrorInner {
fn from(err: CompositeHashConversionError<T>) -> Self {
WasmErrorInner::Guest(format!("{:?}", err))
}
}