use revm::context::result::ResultAndState;
#[derive(Debug, Clone, Copy)]
pub enum PostflightResult {
Discard(&'static str),
Apply,
}
impl From<bool> for PostflightResult {
fn from(b: bool) -> Self {
if b {
Self::Apply
} else {
Self::Discard("")
}
}
}
impl From<&'static str> for PostflightResult {
fn from(s: &'static str) -> Self {
Self::Discard(s)
}
}
impl From<Option<&'static str>> for PostflightResult {
fn from(s: Option<&'static str>) -> Self {
s.map(Self::Discard).unwrap_or(Self::Apply)
}
}
impl PostflightResult {
pub const fn is_discard(&self) -> bool {
matches!(self, Self::Discard(_))
}
pub const fn as_discard_reason(&self) -> Option<&'static str> {
match self {
Self::Discard(reason) => Some(reason),
_ => None,
}
}
pub const fn is_apply(&self) -> bool {
matches!(self, Self::Apply)
}
}
#[macro_export]
macro_rules! discard_if {
($a:expr, $reason:literal) => {
if $a {
tracing::debug!(reason = $reason, "Discarding transaction");
return $crate::PostflightResult::Discard($reason);
}
};
}
pub trait PostTx {
fn run_post_tx(&mut self, result: &ResultAndState) -> PostflightResult;
}
impl<T, O> PostTx for T
where
T: for<'a> FnMut(&'a ResultAndState) -> O,
O: Into<PostflightResult>,
{
fn run_post_tx(&mut self, result: &ResultAndState) -> PostflightResult {
self(result).into()
}
}