use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DecodeError {
TooLarge,
Truncated,
BadMagic,
UnsupportedVersion(u8),
ReservedFlags,
WrongPacketKind,
EncryptionMismatch,
WrongCluster,
UnknownKey(u32),
AuthFailed,
BadVarint,
BadLength,
EmptyPayload,
TrailingBytes,
BadNodeId,
BadAddr,
BadState,
BadBool,
BadVersionRange,
MetaTooLarge,
BadTags,
}
impl DecodeError {
pub fn is_auth_failure(&self) -> bool {
matches!(self, Self::UnknownKey(_) | Self::AuthFailed)
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooLarge => f.write_str("packet exceeds the size limit"),
Self::Truncated => f.write_str("input ended early"),
Self::BadMagic => f.write_str("bad magic"),
Self::UnsupportedVersion(v) => write!(f, "unsupported wire version {v}"),
Self::ReservedFlags => f.write_str("reserved flag bits set"),
Self::WrongPacketKind => f.write_str("datagram/stream flag mismatch"),
Self::EncryptionMismatch => f.write_str("encrypted/plaintext mismatch"),
Self::WrongCluster => f.write_str("cluster label hash mismatch"),
Self::UnknownKey(id) => write!(f, "no key with id {id:08x}"),
Self::AuthFailed => f.write_str("authentication failed"),
Self::BadVarint => f.write_str("malformed varint"),
Self::BadLength => f.write_str("length exceeds remaining input"),
Self::EmptyPayload => f.write_str("payload holds no messages"),
Self::TrailingBytes => f.write_str("trailing bytes after last message"),
Self::BadNodeId => f.write_str("invalid node id"),
Self::BadAddr => f.write_str("invalid address"),
Self::BadState => f.write_str("invalid member state"),
Self::BadBool => f.write_str("invalid boolean"),
Self::BadVersionRange => f.write_str("invalid version range"),
Self::MetaTooLarge => f.write_str("metadata too large"),
Self::BadTags => f.write_str("malformed metadata tags"),
}
}
}
impl std::error::Error for DecodeError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum EncodeError {
TooLarge,
Empty,
MetaTooLarge,
BadVersionRange,
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooLarge => f.write_str("packet would exceed the size limit"),
Self::Empty => f.write_str("no messages to send"),
Self::MetaTooLarge => f.write_str("metadata too large"),
Self::BadVersionRange => f.write_str("invalid version range"),
}
}
}
impl std::error::Error for EncodeError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ConfigError {
LabelTooLong,
NoKeys,
LimitTooSmall,
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::LabelTooLong => f.write_str("cluster label too long"),
Self::NoKeys => f.write_str("encrypted codec needs at least one key"),
Self::LimitTooSmall => f.write_str("size limit below packet overhead"),
}
}
}
impl std::error::Error for ConfigError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KeyError {
NotBase64,
WrongLength,
}
impl fmt::Display for KeyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotBase64 => f.write_str("key is not valid base64"),
Self::WrongLength => f.write_str("key must be 32 bytes"),
}
}
}
impl std::error::Error for KeyError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KeyringError {
Plaintext,
NotInstalled,
InUse,
LastKey,
}
impl fmt::Display for KeyringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Plaintext => f.write_str("a plaintext node has no keys"),
Self::NotInstalled => f.write_str("key is not installed"),
Self::InUse => f.write_str("key is the one in use"),
Self::LastKey => f.write_str("key is the last one installed"),
}
}
}
impl std::error::Error for KeyringError {}