use std::fmt;
use std::io;
use crate::{MAX_CLIENTS, MAX_PAYLOAD_BYTES, MAX_SERVERS_PER_CONNECT};
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
InvalidConnectToken,
EncryptFailed,
DecryptFailed,
InvalidPayloadSize(usize),
InvalidServerAddresses,
InvalidMaxClients(usize),
Io(io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidConnectToken => write!(f, "invalid connect token"),
Error::EncryptFailed => write!(f, "encryption failed"),
Error::DecryptFailed => write!(f, "decryption failed"),
Error::InvalidPayloadSize(size) => {
write!(f, "payload size {size} is out of range [1,{MAX_PAYLOAD_BYTES}]")
}
Error::InvalidServerAddresses => write!(
f,
"number of server addresses is out of range [1,{MAX_SERVERS_PER_CONNECT}] \
or public and internal address lists differ in length"
),
Error::InvalidMaxClients(n) => {
write!(f, "max clients {n} is out of range [1,{MAX_CLIENTS}]")
}
Error::Io(error) => write!(f, "socket error: {error}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(error) => Some(error),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::Io(error)
}
}