use crate::SwarmAddress;
use thiserror::Error;
use super::type_id::ChunkTypeId;
pub(crate) type Result<T> = std::result::Result<T, ChunkError>;
#[derive(Error, Debug)]
pub enum ChunkError {
#[error("Invalid chunk size: {message} (expected: {expected}, got: {actual})")]
InvalidSize {
message: &'static str,
expected: usize,
actual: usize,
},
#[error("Invalid chunk format: {0}")]
InvalidFormat(String),
#[error("Chunk address verification failed: expected {expected}, got {actual}")]
VerificationFailed {
expected: SwarmAddress,
actual: SwarmAddress,
},
#[error("Signature error: {0}")]
Signature(#[from] alloy_primitives::SignatureError),
#[error("Signer error: {0}")]
Signer(#[from] alloy_signer::Error),
#[error("Invalid chunk signature: {0}")]
InvalidSignature(String),
#[error("Unsupported chunk type: {0}")]
UnsupportedType(ChunkTypeId),
}
impl ChunkError {
pub const fn invalid_size(message: &'static str, expected: usize, actual: usize) -> Self {
Self::InvalidSize {
message,
expected,
actual,
}
}
pub fn invalid_format<S: Into<String>>(msg: S) -> Self {
Self::InvalidFormat(msg.into())
}
pub const fn verification_failed(expected: SwarmAddress, actual: SwarmAddress) -> Self {
Self::VerificationFailed { expected, actual }
}
pub fn invalid_signature<S: Into<String>>(msg: S) -> Self {
Self::InvalidSignature(msg.into())
}
pub const fn unsupported_type(type_id: ChunkTypeId) -> Self {
Self::UnsupportedType(type_id)
}
}