Group errors using only the enum

This commit is contained in:
Daniele Tricoli 2019-05-18 03:02:45 +02:00
parent 9bb0c00708
commit 80b2e7abe9

View file

@ -5,23 +5,31 @@ use std::sync;
use crate::commands::Command;
// Aggregate all kind of different errors for SimpleUDPServer.
// Aggregate all kind of different errors for udp::Server.
#[derive(Debug)]
pub struct UDPServerError<'a> {
detail: ErrorDetail<'a>,
pub enum UDPServerError<'a> {
IOError(io::Error),
PoisonError(sync::PoisonError<sync::RwLockWriteGuard<'a, Command>>),
FromUtf8Error(string::FromUtf8Error),
}
impl fmt::Display for UDPServerError<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.detail)
match self {
UDPServerError::IOError(err) => write!(f, "{}", err.to_string()),
UDPServerError::PoisonError(err) => {
write!(f, "{}", err.to_string())
}
UDPServerError::FromUtf8Error(err) => {
write!(f, "{}", err.to_string())
}
}
}
}
impl<'a> From<io::Error> for UDPServerError<'a> {
fn from(err: io::Error) -> UDPServerError<'a> {
UDPServerError {
detail: ErrorDetail::IOError(err),
}
UDPServerError::IOError(err)
}
}
@ -31,33 +39,12 @@ impl<'a> From<sync::PoisonError<sync::RwLockWriteGuard<'a, Command>>>
fn from(
err: sync::PoisonError<sync::RwLockWriteGuard<'a, Command>>,
) -> UDPServerError {
UDPServerError {
detail: ErrorDetail::PoisonError(err),
}
UDPServerError::PoisonError(err)
}
}
impl<'a> From<string::FromUtf8Error> for UDPServerError<'a> {
fn from(err: string::FromUtf8Error) -> UDPServerError<'a> {
UDPServerError {
detail: ErrorDetail::FromUtf8Error(err),
}
}
}
#[derive(Debug)]
enum ErrorDetail<'a> {
IOError(io::Error),
PoisonError(sync::PoisonError<sync::RwLockWriteGuard<'a, Command>>),
FromUtf8Error(string::FromUtf8Error),
}
impl fmt::Display for ErrorDetail<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ErrorDetail::IOError(err) => write!(f, "{}", err.to_string()),
ErrorDetail::PoisonError(err) => write!(f, "{}", err.to_string()),
ErrorDetail::FromUtf8Error(err) => write!(f, "{}", err.to_string()),
}
UDPServerError::FromUtf8Error(err)
}
}