use std::{
fmt,
fmt::{Display, Formatter},
};
use borsh::BorshSerialize;
use minicbor::{CborLen, Decode, Encode};
use serde::{Deserialize, Serialize};
use tari_engine_types::commit_result::{AbortReason, TransactionResult};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize, BorshSerialize, Encode, Decode, CborLen)]
#[cfg_attr(feature = "ts", derive(ts_rs::TS), ts(export))]
pub enum Decision {
#[n(0)]
Commit,
#[n(1)]
Abort(#[n(0)] AbortReason),
}
impl Decision {
pub fn is_same_outcome(&self, other: Decision) -> bool {
matches!(
(self, other),
(Decision::Commit, Decision::Commit) | (Decision::Abort(_), Decision::Abort(_))
)
}
}
impl Decision {
pub fn is_commit(&self) -> bool {
matches!(self, Decision::Commit)
}
pub fn is_abort(&self) -> bool {
matches!(self, Decision::Abort(_))
}
pub fn and(self, other: Self) -> Self {
match self {
Decision::Commit => other,
Decision::Abort(reason) => Decision::Abort(reason),
}
}
pub fn abort_reason(&self) -> Option<AbortReason> {
match self {
Decision::Commit => None,
Decision::Abort(reason) => Some(*reason),
}
}
}
impl Display for Decision {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Decision::Commit => write!(f, "Commit"),
Decision::Abort(reason) => write!(f, "Abort({:?})", reason),
}
}
}
impl From<&TransactionResult> for Decision {
fn from(result: &TransactionResult) -> Self {
match result {
TransactionResult::Accept(_) | TransactionResult::AcceptFeeRejectRest(_, _) => Decision::Commit,
TransactionResult::Reject(reason) => Decision::Abort(AbortReason::from(reason)),
}
}
}