use crate::prelude::*;
pub use hdk_derive::hdk_entry_helper;
pub use hdk_derive::hdk_entry_types;
#[cfg(doc)]
pub mod examples;
pub fn must_get_entry(entry_hash: EntryHash) -> ExternResult<EntryHashed> {
HDI.with(|h| {
h.borrow()
.must_get_entry(MustGetEntryInput::new(entry_hash))
})
}
pub fn must_get_action(action_hash: ActionHash) -> ExternResult<SignedActionHashed> {
HDI.with(|h| {
h.borrow()
.must_get_action(MustGetActionInput::new(action_hash))
})
}
pub fn must_get_valid_record(action_hash: ActionHash) -> ExternResult<Record> {
HDI.with(|h| {
h.borrow()
.must_get_valid_record(MustGetValidRecordInput::new(action_hash))
})
}
#[macro_export]
macro_rules! app_entry {
( $t:ident ) => {
impl TryFrom<&$crate::prelude::Entry> for $t {
type Error = $crate::prelude::WasmError;
fn try_from(entry: &$crate::prelude::Entry) -> Result<Self, Self::Error> {
match entry {
$crate::prelude::Entry::App(eb) => Ok(Self::try_from(
$crate::prelude::SerializedBytes::from(eb.to_owned()),
).map_err(|e| $crate::prelude::wasm_error!(e))?),
$crate::prelude::Entry::CounterSign(_, eb) => Ok(Self::try_from(
$crate::prelude::SerializedBytes::from(eb.to_owned()),
).map_err(|e| $crate::prelude::wasm_error!(e))?),
_ => Err($crate::prelude::wasm_error!(
"{:?} is not an Entry::App or Entry::CounterSign so has no serialized bytes",
entry
)),
}
}
}
impl TryFrom<$crate::prelude::Entry> for $t {
type Error = $crate::prelude::WasmError;
fn try_from(entry: $crate::prelude::Entry) -> Result<Self, Self::Error> {
Self::try_from(&entry)
}
}
impl TryFrom<$crate::prelude::EntryHashed> for $t {
type Error = $crate::prelude::WasmError;
fn try_from(entry_hashed: $crate::prelude::EntryHashed) -> Result<Self, Self::Error> {
Self::try_from(entry_hashed.as_content())
}
}
impl TryFrom<&$crate::prelude::Record> for $t {
type Error = $crate::prelude::WasmError;
fn try_from(record: &$crate::prelude::Record) -> Result<Self, Self::Error> {
Ok(match &record.entry {
RecordEntry::Present(entry) => Self::try_from(entry)?,
_ => return Err(
$crate::prelude::wasm_error!(
$crate::prelude::WasmErrorInner::Guest(format!("Tried to deserialize a record, expecting it to contain entry data, but there was none. Record ActionHash: {}", record.signed_action.hashed.hash))),
)
})
}
}
impl TryFrom<$crate::prelude::Record> for $t {
type Error = $crate::prelude::WasmError;
fn try_from(record: $crate::prelude::Record) -> Result<Self, Self::Error> {
(&record).try_into()
}
}
impl TryFrom<&$t> for $crate::prelude::AppEntryBytes {
type Error = $crate::prelude::WasmError;
fn try_from(t: &$t) -> Result<Self, Self::Error> {
AppEntryBytes::try_from(SerializedBytes::try_from(t).map_err(|e| wasm_error!(e))?).map_err(|entry_error| match entry_error {
EntryError::SerializedBytes(serialized_bytes_error) => {
wasm_error!(WasmErrorInner::Serialize(serialized_bytes_error))
}
EntryError::EntryTooLarge(_) => {
wasm_error!(WasmErrorInner::Guest(entry_error.to_string()))
}
})
}
}
impl TryFrom<$t> for $crate::prelude::AppEntryBytes {
type Error = $crate::prelude::WasmError;
fn try_from(t: $t) -> Result<Self, Self::Error> {
Self::try_from(&t)
}
}
impl TryFrom<&$t> for $crate::prelude::Entry {
type Error = $crate::prelude::WasmError;
fn try_from(t: &$t) -> Result<Self, Self::Error> {
Ok(Self::App($crate::prelude::AppEntryBytes::try_from(t)?))
}
}
impl TryFrom<$t> for $crate::prelude::Entry {
type Error = $crate::prelude::WasmError;
fn try_from(t: $t) -> Result<Self, Self::Error> {
Self::try_from(&t)
}
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! entry_types {
[ $( $def:expr ),* ] => {
#[hdk_extern]
pub fn entry_defs(_: ()) -> $crate::prelude::ExternResult<$crate::prelude::EntryDefsCallbackResult> {
Ok($crate::prelude::EntryDefsCallbackResult::from(vec![ $( $def ),* ]))
}
};
}