use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum IdempotentResultCode {
Applied,
AppliedTtlMissing,
Duplicate,
RejectedLedgerType,
RejectedOperation,
RejectedCommand,
}
impl IdempotentResultCode {
pub(crate) fn parse(code: &str) -> Option<Self> {
Some(match code {
"APPLIED" => Self::Applied,
"APPLIED_TTL_MISSING" => Self::AppliedTtlMissing,
"DUPLICATE" => Self::Duplicate,
"REJECTED_LEDGER_TYPE" => Self::RejectedLedgerType,
"REJECTED_OPERATION" => Self::RejectedOperation,
"REJECTED_COMMAND" => Self::RejectedCommand,
_ => return None,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IdempotentRejection {
LedgerType,
Operation,
Command,
}
impl IdempotentRejection {
pub fn label(self) -> &'static str {
match self {
Self::LedgerType => "ledger_type",
Self::Operation => "operation",
Self::Command => "command",
}
}
}
#[derive(Debug, Clone)]
pub struct IdempotentCounterError {
pub code: IdempotentRejection,
pub detail: String,
}
impl fmt::Display for IdempotentCounterError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"idempotent counter rejected: {} ({})",
self.code.label(),
self.detail
)
}
}
impl std::error::Error for IdempotentCounterError {}