#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SagaOutcome<T> {
Succeeded(T),
Rejected {
code: &'static str,
},
Unknown {
code: &'static str,
},
Halted {
code: &'static str,
},
}
impl<T> SagaOutcome<T> {
pub fn is_succeeded(&self) -> bool {
matches!(self, Self::Succeeded(_))
}
pub fn is_terminal(&self) -> bool {
!matches!(self, Self::Unknown { .. })
}
pub fn code(&self) -> Option<&'static str> {
match self {
Self::Succeeded(_) => None,
Self::Rejected { code } | Self::Unknown { code } | Self::Halted { code } => Some(code),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompensationOutcome {
Succeeded,
Unknown {
code: &'static str,
},
Halted {
code: &'static str,
},
}
impl CompensationOutcome {
pub fn is_succeeded(&self) -> bool {
matches!(self, Self::Succeeded)
}
pub fn code(&self) -> Option<&'static str> {
match self {
Self::Succeeded => None,
Self::Unknown { code } | Self::Halted { code } => Some(code),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SagaExecutionError {
Retryable {
code: &'static str,
},
}
impl SagaExecutionError {
pub fn code(&self) -> &'static str {
match self {
Self::Retryable { code } => code,
}
}
}
impl std::fmt::Display for SagaExecutionError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Retryable { code } => write!(formatter, "saga retryable execution error: {code}"),
}
}
}
impl std::error::Error for SagaExecutionError {}
pub type SagaResult<T> = Result<SagaOutcome<T>, SagaExecutionError>;
pub type CompensationResult = Result<CompensationOutcome, SagaExecutionError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TerminalForwardOutcome {
Succeeded,
Rejected {
code: &'static str,
},
Halted {
code: &'static str,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CancelOutcome {
CancelConfirmed,
AlreadyTerminal(TerminalForwardOutcome),
ResolutionPending {
code: &'static str,
},
}
impl CancelOutcome {
pub fn is_resolved(&self) -> bool {
!matches!(self, Self::ResolutionPending { .. })
}
}
pub type CancelResult = Result<CancelOutcome, SagaExecutionError>;