use thiserror::Error;
pub type Result<T> = core::result::Result<T, AdapterError>;
#[derive(Error, Debug)]
pub enum AdapterError {
#[error("Seal replay detected: seal {0:?}")]
SealReplay(String),
#[error("Invalid seal: {0}")]
InvalidSeal(String),
#[error("Commitment hash mismatch: expected {expected}, got {actual}")]
CommitmentMismatch {
expected: String,
actual: String,
},
#[error("Inclusion proof failed: {0}")]
InclusionProofFailed(String),
#[error("Finality not reached: {0}")]
FinalityNotReached(String),
#[error("Anchor invalidated by reorg: {0:?}")]
ReorgInvalid(String),
#[error("Network error: {0}")]
NetworkError(String),
#[error("Publish failed: {0}")]
PublishFailed(String),
#[error("Serialization error: {0}")]
SerializationError(String),
#[error("Invalid configuration: {0}")]
InvalidConfig(String),
#[error("Version mismatch: expected {expected}, got {actual}")]
VersionMismatch {
expected: u8,
actual: u8,
},
#[error("Domain separator mismatch")]
DomainSeparatorMismatch,
#[error("Signature verification failed: {0}")]
SignatureVerificationFailed(String),
#[error("Adapter error: {0}")]
Generic(String),
}
impl AdapterError {
pub fn is_reorg(&self) -> bool {
matches!(self, AdapterError::ReorgInvalid(_))
}
pub fn is_replay(&self) -> bool {
matches!(self, AdapterError::SealReplay(_))
}
pub fn is_signature_error(&self) -> bool {
matches!(self, AdapterError::SignatureVerificationFailed(_))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = AdapterError::SealReplay("abc123".to_string());
assert!(err.to_string().contains("replay"));
}
#[test]
fn test_error_is_reorg() {
let err = AdapterError::ReorgInvalid("anchor".to_string());
assert!(err.is_reorg());
}
#[test]
fn test_error_is_replay() {
let err = AdapterError::SealReplay("seal".to_string());
assert!(err.is_replay());
}
#[test]
fn test_error_is_signature_error() {
let err = AdapterError::SignatureVerificationFailed("invalid sig".to_string());
assert!(err.is_signature_error());
}
#[test]
fn test_error_signature_verification_failed() {
let err = AdapterError::SignatureVerificationFailed("bad signature".to_string());
assert!(err.to_string().contains("Signature verification failed"));
}
}