use crate::{DecodeError, EncodeError, Label, ParseLabelError};
#[derive(Debug, thiserror::Error)]
pub enum ProtoDecoderError {
#[error(transparent)]
Decode(#[from] DecodeError),
#[error(transparent)]
Label(#[from] ParseLabelError),
#[error("The label {actual} does not match the expected label {expected}")]
LabelMismatch {
actual: Label,
expected: Label,
},
#[error("label not found")]
LabelNotFound,
#[error("unexpected double packet label header")]
UnexpectedLabel,
#[error("The offload task is canceled")]
Offload,
#[error(
"Receive an encrypted message while the encryption feature is disabled on the running node"
)]
EncryptionDisabled,
#[error(
"Receive a checksumed message while the checksum feature is disabled on the running node"
)]
ChecksumDisabled,
#[error(
"Receive a compressed message while the compression feature is disabled on the running node"
)]
CompressionDisabled,
#[cfg(feature = "encryption")]
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
#[error(transparent)]
Encryption(#[from] crate::EncryptionError),
#[cfg(feature = "encryption")]
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
#[error("The message is not encrypted")]
NotEncrypted,
#[cfg(feature = "encryption")]
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
#[error("key not found for decryption")]
SecretKeyNotFound,
#[cfg(feature = "encryption")]
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
#[error("no installed keys could decrypt the message")]
NoInstalledKeys,
#[cfg(any(
feature = "zstd",
feature = "lz4",
feature = "snappy",
feature = "brotli",
))]
#[cfg_attr(
docsrs,
doc(cfg(any(
feature = "zstd",
feature = "lz4",
feature = "snappy",
feature = "brotli",
)))
)]
#[error(transparent)]
Compression(#[from] crate::CompressionError),
#[cfg(any(
feature = "crc32",
feature = "xxhash32",
feature = "xxhash64",
feature = "xxhash3",
feature = "murmur3",
))]
#[cfg_attr(
docsrs,
doc(cfg(any(
feature = "crc32",
feature = "xxhash64",
feature = "xxhash32",
feature = "xxhash3",
feature = "murmur3",
)))
)]
#[error(transparent)]
Checksum(#[from] crate::ChecksumError),
}
impl From<ProtoDecoderError> for std::io::Error {
fn from(value: ProtoDecoderError) -> Self {
use std::io::ErrorKind;
match value {
ProtoDecoderError::Decode(e) => Self::new(ErrorKind::InvalidData, e),
ProtoDecoderError::Label(e) => Self::new(ErrorKind::InvalidData, e),
ProtoDecoderError::LabelMismatch { .. } => Self::new(ErrorKind::InvalidData, value),
ProtoDecoderError::Offload => Self::other(value),
ProtoDecoderError::EncryptionDisabled => Self::other(value),
ProtoDecoderError::ChecksumDisabled => Self::other(value),
ProtoDecoderError::CompressionDisabled => Self::other(value),
ProtoDecoderError::UnexpectedLabel => Self::new(ErrorKind::InvalidInput, value),
ProtoDecoderError::LabelNotFound => Self::new(ErrorKind::InvalidInput, value),
#[cfg(feature = "encryption")]
ProtoDecoderError::Encryption(e) => Self::new(ErrorKind::InvalidData, e),
#[cfg(feature = "encryption")]
ProtoDecoderError::NotEncrypted => Self::new(ErrorKind::InvalidData, value),
#[cfg(feature = "encryption")]
ProtoDecoderError::SecretKeyNotFound => Self::new(ErrorKind::InvalidData, value),
#[cfg(feature = "encryption")]
ProtoDecoderError::NoInstalledKeys => Self::new(ErrorKind::InvalidData, value),
#[cfg(any(
feature = "zstd",
feature = "lz4",
feature = "snappy",
feature = "brotli",
))]
ProtoDecoderError::Compression(e) => Self::new(ErrorKind::InvalidData, e),
#[cfg(any(
feature = "crc32",
feature = "xxhash32",
feature = "xxhash64",
feature = "xxhash3",
feature = "murmur3",
))]
ProtoDecoderError::Checksum(e) => Self::new(ErrorKind::InvalidData, e),
}
}
}
impl ProtoDecoderError {
#[inline]
pub(crate) const fn label_mismatch(expected: Label, actual: Label) -> Self {
Self::LabelMismatch { expected, actual }
}
#[inline]
pub(crate) const fn double_label() -> Self {
Self::UnexpectedLabel
}
}
#[derive(Debug, thiserror::Error)]
pub enum ProtoEncoderError {
#[error(transparent)]
Encode(#[from] EncodeError),
#[cfg(feature = "encryption")]
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
#[error(transparent)]
Encrypt(#[from] crate::EncryptionError),
#[cfg(any(
feature = "zstd",
feature = "lz4",
feature = "brotli",
feature = "snappy",
))]
#[cfg_attr(
docsrs,
feature(any(
feature = "zstd",
feature = "lz4",
feature = "brotli",
feature = "snappy",
))
)]
#[error(transparent)]
Compress(#[from] crate::CompressionError),
#[cfg(any(
feature = "crc32",
feature = "xxhash32",
feature = "xxhash64",
feature = "xxhash3",
feature = "murmur3",
))]
#[cfg_attr(
docsrs,
feature(any(
feature = "crc32",
feature = "xxhash32",
feature = "xxhash64",
feature = "xxhash3",
feature = "murmur3",
))
)]
#[error(transparent)]
Checksum(#[from] crate::ChecksumError),
}