use anchor_lang::prelude::*;
#[derive(Debug, thiserror::Error)]
pub enum ActionError {
#[error("preconditions are not met: {0}")]
PreconditionsAreNotMet(&'static str),
}
type ActionResult<T> = std::result::Result<T, ActionError>;
pub const MAX_ACTION_FLAGS: usize = 8;
#[repr(u8)]
#[non_exhaustive]
#[derive(num_enum::IntoPrimitive, num_enum::TryFromPrimitive)]
pub enum ActionFlag {
ShouldUnwrapNativeToken,
}
#[non_exhaustive]
#[repr(u8)]
#[derive(
Clone,
Copy,
num_enum::IntoPrimitive,
num_enum::TryFromPrimitive,
PartialEq,
Eq,
strum::EnumString,
strum::Display,
AnchorSerialize,
AnchorDeserialize,
InitSpace,
)]
#[strum(serialize_all = "snake_case")]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum ActionState {
Pending,
Completed,
Cancelled,
}
impl ActionState {
pub fn completed(self) -> ActionResult<Self> {
let Self::Pending = self else {
return Err(ActionError::PreconditionsAreNotMet("expected pending"));
};
Ok(Self::Completed)
}
pub fn cancelled(self) -> ActionResult<Self> {
let Self::Pending = self else {
return Err(ActionError::PreconditionsAreNotMet("expected pending"));
};
Ok(Self::Cancelled)
}
pub fn is_completed_or_cancelled(&self) -> bool {
matches!(self, Self::Completed | Self::Cancelled)
}
pub fn is_pending(&self) -> bool {
matches!(self, Self::Pending)
}
pub fn is_cancelled(&self) -> bool {
matches!(self, Self::Cancelled)
}
pub fn is_completed(&self) -> bool {
matches!(self, Self::Completed)
}
}
#[non_exhaustive]
#[repr(u8)]
#[derive(
Clone,
Copy,
num_enum::IntoPrimitive,
num_enum::TryFromPrimitive,
PartialEq,
Eq,
strum::EnumString,
strum::Display,
AnchorSerialize,
AnchorDeserialize,
InitSpace,
)]
#[strum(serialize_all = "snake_case")]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum ActionCallbackKind {
Disabled,
General,
}