use crate::capability::CapClaim;
use crate::capability::CapGrant;
use crate::capability::ZomeCallCapGrant;
use crate::countersigning::CounterSigningSessionData;
use crate::AppEntryDef;
use crate::EntryDefIndex;
use crate::EntryType;
use crate::ZomeIndex;
use holo_hash::hash_type;
use holo_hash::ActionHash;
use holo_hash::AgentPubKey;
use holo_hash::EntryHash;
use holo_hash::HashableContent;
use holo_hash::HashableContentBytes;
use holochain_serialized_bytes::prelude::*;
mod app_entry_bytes;
mod error;
pub use app_entry_bytes::*;
pub use error::*;
pub const ENTRY_SIZE_LIMIT: usize = 4 * 1000 * 1000;
pub type CapGrantEntry = ZomeCallCapGrant;
pub type CapClaimEntry = CapClaim;
pub type EntryHashed = holo_hash::HoloHashed<Entry>;
pub trait EntryTypesHelper: Sized {
type Error;
fn deserialize_from_type<Z, I>(
zome_index: Z,
entry_def_index: I,
entry: &Entry,
) -> Result<Option<Self>, Self::Error>
where
Z: Into<ZomeIndex>,
I: Into<EntryDefIndex>;
}
impl EntryTypesHelper for () {
type Error = core::convert::Infallible;
fn deserialize_from_type<Z, I>(
_zome_index: Z,
_entry_def_index: I,
_entry: &Entry,
) -> Result<Option<Self>, Self::Error>
where
Z: Into<ZomeIndex>,
I: Into<EntryDefIndex>,
{
Ok(Some(()))
}
}
impl From<EntryHashed> for Entry {
fn from(entry_hashed: EntryHashed) -> Self {
entry_hashed.into_content()
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, SerializedBytes)]
#[cfg_attr(
feature = "fuzzing",
derive(arbitrary::Arbitrary, proptest_derive::Arbitrary)
)]
#[serde(tag = "entry_type", content = "entry")]
pub enum Entry {
Agent(AgentPubKey),
App(AppEntryBytes),
CounterSign(Box<CounterSigningSessionData>, AppEntryBytes),
CapClaim(CapClaimEntry),
CapGrant(CapGrantEntry),
}
impl Entry {
pub fn as_cap_grant(&self) -> Option<CapGrant> {
match self {
Entry::Agent(key) => Some(CapGrant::ChainAuthor(key.clone())),
Entry::CapGrant(data) => Some(CapGrant::RemoteAgent(data.clone())),
_ => None,
}
}
pub fn as_cap_claim(&self) -> Option<&CapClaim> {
match self {
Entry::CapClaim(claim) => Some(claim),
_ => None,
}
}
pub fn as_app_entry(&self) -> Option<&AppEntryBytes> {
match self {
Entry::App(bytes) => Some(bytes),
_ => None,
}
}
pub fn app(sb: SerializedBytes) -> Result<Self, EntryError> {
Ok(Entry::App(AppEntryBytes::try_from(sb)?))
}
pub fn app_fancy<SB: TryInto<SerializedBytes, Error = SerializedBytesError>>(
sb: SB,
) -> Result<Self, EntryError> {
Ok(Entry::App(AppEntryBytes::try_from(sb.try_into()?)?))
}
pub fn entry_type(&self, entry_def: Option<AppEntryDef>) -> Option<EntryType> {
match (self, entry_def) {
(Entry::Agent(_), _) => Some(EntryType::AgentPubKey),
(Entry::CapClaim(_), _) => Some(EntryType::CapClaim),
(Entry::CapGrant(_), _) => Some(EntryType::CapGrant),
(Entry::App(_), Some(aed)) | (Entry::CounterSign(_, _), Some(aed)) => {
Some(EntryType::App(aed))
}
_ => None,
}
}
}
impl HashableContent for Entry {
type HashType = hash_type::Entry;
fn hash_type(&self) -> Self::HashType {
hash_type::Entry
}
fn hashable_content(&self) -> HashableContentBytes {
match self {
Entry::Agent(agent_pubkey) => {
HashableContentBytes::Prehashed39(
EntryHash::from(agent_pubkey.clone()).into_inner(),
)
}
entry => HashableContentBytes::Content(
entry
.try_into()
.expect("Could not serialize HashableContent"),
),
}
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct MustGetValidRecordInput(pub ActionHash);
impl MustGetValidRecordInput {
pub fn new(action_hash: ActionHash) -> Self {
Self(action_hash)
}
pub fn into_inner(self) -> ActionHash {
self.0
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct MustGetEntryInput(pub EntryHash);
impl MustGetEntryInput {
pub fn new(entry_hash: EntryHash) -> Self {
Self(entry_hash)
}
pub fn into_inner(self) -> EntryHash {
self.0
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct MustGetActionInput(pub ActionHash);
impl MustGetActionInput {
pub fn new(action_hash: ActionHash) -> Self {
Self(action_hash)
}
pub fn into_inner(self) -> ActionHash {
self.0
}
}