use std::convert::TryFrom;
use holo_hash::DhtOpHash;
use holochain_types::dht_op::DhtOp;
use super::workflow::error::WorkflowResult;
use super::SourceChainError;
use super::SysValidationError;
use super::ValidationOutcome;
#[derive(Debug)]
pub enum OutcomeOrError<T, E> {
Outcome(T),
Err(E),
}
impl<T, E> OutcomeOrError<T, E> {
pub fn into_outcome(self) -> Option<T> {
if let Self::Outcome(t) = self {
Some(t)
} else {
None
}
}
pub fn into_err(self) -> Option<E> {
if let Self::Err(e) = self {
Some(e)
} else {
None
}
}
}
#[macro_export]
macro_rules! from_sub_error {
($error_type:ident, $sub_error_type:ident) => {
impl<T> From<$sub_error_type> for OutcomeOrError<T, $error_type> {
fn from(e: $sub_error_type) -> Self {
OutcomeOrError::Err($error_type::from(e))
}
}
};
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum DhtOpOrder {
RegisterAgentActivity(holochain_zome_types::timestamp::Timestamp),
StoreEntry(holochain_zome_types::timestamp::Timestamp),
StoreRecord(holochain_zome_types::timestamp::Timestamp),
RegisterUpdatedContent(holochain_zome_types::timestamp::Timestamp),
RegisterUpdatedRecord(holochain_zome_types::timestamp::Timestamp),
RegisterDeletedBy(holochain_zome_types::timestamp::Timestamp),
RegisterDeletedEntryAction(holochain_zome_types::timestamp::Timestamp),
RegisterAddLink(holochain_zome_types::timestamp::Timestamp),
RegisterRemoveLink(holochain_zome_types::timestamp::Timestamp),
}
#[derive(Debug, Clone)]
pub struct OrderedOp<V> {
pub order: DhtOpOrder,
pub hash: DhtOpHash,
pub op: DhtOp,
pub value: V,
}
impl<V> PartialEq for OrderedOp<V> {
fn eq(&self, other: &Self) -> bool {
self.order.eq(&other.order)
}
}
impl<V> PartialOrd for OrderedOp<V> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.order.partial_cmp(&other.order)
}
}
impl<V> Eq for OrderedOp<V> {}
impl<V> Ord for OrderedOp<V> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.order.cmp(&other.order)
}
}
impl From<&DhtOp> for DhtOpOrder {
fn from(op: &DhtOp) -> Self {
use DhtOpOrder::*;
match op {
DhtOp::StoreRecord(_, h, _) => StoreRecord(h.timestamp()),
DhtOp::StoreEntry(_, h, _) => StoreEntry(h.timestamp()),
DhtOp::RegisterAgentActivity(_, h) => RegisterAgentActivity(h.timestamp()),
DhtOp::RegisterUpdatedContent(_, h, _) => RegisterUpdatedContent(h.timestamp),
DhtOp::RegisterUpdatedRecord(_, h, _) => RegisterUpdatedRecord(h.timestamp),
DhtOp::RegisterDeletedBy(_, h) => RegisterDeletedBy(h.timestamp),
DhtOp::RegisterDeletedEntryAction(_, h) => RegisterDeletedEntryAction(h.timestamp),
DhtOp::RegisterAddLink(_, h) => RegisterAddLink(h.timestamp),
DhtOp::RegisterRemoveLink(_, h) => RegisterRemoveLink(h.timestamp),
}
}
}
impl OutcomeOrError<ValidationOutcome, SysValidationError> {
pub fn invalid_call_zome_commit<T>(self) -> WorkflowResult<T> {
Err(SourceChainError::InvalidCommit(ValidationOutcome::try_from(self)?.to_string()).into())
}
}