use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum TxCompilerErrorCode {
InvalidPayload,
UnsupportedChain,
UnsupportedTxType,
UnsupportedFeeMode,
InvalidAddress,
InvalidAmount,
MissingFeeParams,
InvalidBlockHeader,
InvalidCalldata,
CompilationFailed,
}
impl TxCompilerErrorCode {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::InvalidPayload => "INVALID_PAYLOAD",
Self::UnsupportedChain => "UNSUPPORTED_CHAIN",
Self::UnsupportedTxType => "UNSUPPORTED_TX_TYPE",
Self::UnsupportedFeeMode => "UNSUPPORTED_FEE_MODE",
Self::InvalidAddress => "INVALID_ADDRESS",
Self::InvalidAmount => "INVALID_AMOUNT",
Self::MissingFeeParams => "MISSING_FEE_PARAMS",
Self::InvalidBlockHeader => "INVALID_BLOCK_HEADER",
Self::InvalidCalldata => "INVALID_CALLDATA",
Self::CompilationFailed => "COMPILATION_FAILED",
}
}
}
impl fmt::Display for TxCompilerErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TxCompilerError {
pub code: TxCompilerErrorCode,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub details: Option<serde_json::Value>,
}
impl TxCompilerError {
#[must_use]
pub fn new(code: TxCompilerErrorCode, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
details: None,
}
}
#[must_use]
pub fn with_details(
code: TxCompilerErrorCode,
message: impl Into<String>,
details: serde_json::Value,
) -> Self {
Self {
code,
message: message.into(),
details: Some(details),
}
}
}
impl fmt::Display for TxCompilerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.code, self.message)
}
}
impl std::error::Error for TxCompilerError {}