diff --git a/crates/common/src/error.rs b/crates/common/src/error.rs index 9a4a3d3..a917f9b 100644 --- a/crates/common/src/error.rs +++ b/crates/common/src/error.rs @@ -1,10 +1,11 @@ -use std::fmt::Display; - use bitcode::{Decode, Encode}; +use crate::{error_display, error_from}; + pub mod community; pub mod decode; pub mod file_operation; +pub mod macro_; pub mod message; pub mod network; pub mod user; @@ -13,8 +14,8 @@ pub mod user; pub enum Error { Authenticate, User(user::Error), - Message(message::Error), Community(community::Error), + Message(message::Error), FileOperation(file_operation::Error), Network(network::Error), Decode(decode::Error), @@ -23,113 +24,27 @@ pub enum Error { impl std::error::Error for Error {} -impl Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Error::Authenticate => write!(f, "Authenticate"), - Error::User(error_value) => error_value.fmt(f), - Error::Message(error_value) => error_value.fmt(f), - Error::Community(error_value) => error_value.fmt(f), - Error::FileOperation(error_value) => error_value.fmt(f), - Error::Network(error_value) => error_value.fmt(f), - Error::Decode(error_value) => error_value.fmt(f), - Error::Server => write!(f, "Server"), - } - } -} - -impl From for Error { - fn from(value: user::Error) -> Self { - Error::User(value) - } -} - -impl From for Error { - fn from(value: message::Error) -> Self { - Error::Message(value) - } -} - -impl From for Error { - fn from(value: community::Error) -> Self { - Error::Community(value) - } -} - -impl From for Error { - fn from(value: file_operation::Error) -> Self { - Error::FileOperation(value) - } -} - -impl From for Error { - fn from(value: network::Error) -> Self { - Error::Network(value) - } -} - -impl From for Error { - fn from(value: decode::Error) -> Self { - Error::Decode(value) - } -} - -impl From for Error { - fn from(value: std::net::AddrParseError) -> Self { - Self::Network(value.into()) - } -} - -impl From for Error { - fn from(value: std::io::Error) -> Self { - Self::Network(value.into()) - } -} - -impl From for Error { - fn from(value: quinn::ConnectionError) -> Self { - Self::Network(value.into()) - } -} - -impl From for Error { - fn from(value: quinn::ConnectError) -> Self { - Self::Network(value.into()) - } -} - -impl From for Error { - fn from(value: quinn::ReadError) -> Self { - Self::Network(value.into()) - } -} - -impl From for Error { - fn from(value: quinn::ReadToEndError) -> Self { - Self::Network(value.into()) - } -} - -impl From for Error { - fn from(value: quinn::ReadExactError) -> Self { - Self::Network(value.into()) - } -} - -impl From for Error { - fn from(value: quinn::WriteError) -> Self { - Self::Network(value.into()) - } -} - -impl From for Error { - fn from(value: quinn::ClosedStream) -> Self { - Self::Network(value.into()) - } -} - -impl From for Error { - fn from(value: bitcode::Error) -> Self { - Self::Decode(value.into()) - } -} +error_display! { Error, + delegate = [Error::User, Error::Message, Error::Community, Error::FileOperation, Error::Network, Error::Decode], + unit = [Error::Authenticate => "Authenticate", Error::Server => "Server"], +} + +error_from! { Error { + user::Error => Error::User, + community::Error => Error::Community, + message::Error => Error::Message, + file_operation::Error => Error::FileOperation, + network::Error => Error::Network, + decode::Error => Error::Decode, + + std::net::AddrParseError => Error::Network as network::Error, + std::io::Error => Error::Network as network::Error, + quinn::ConnectionError => Error::Network as network::Error, + quinn::ConnectError => Error::Network as network::Error, + quinn::ReadError => Error::Network as network::Error, + quinn::ReadToEndError => Error::Network as network::Error, + quinn::ReadExactError => Error::Network as network::Error, + quinn::WriteError => Error::Network as network::Error, + quinn::ClosedStream => Error::Network as network::Error, + bitcode::Error => Error::Decode as decode::Error, +} } diff --git a/crates/common/src/error/decode.rs b/crates/common/src/error/decode.rs index 14ecf05..afe3812 100644 --- a/crates/common/src/error/decode.rs +++ b/crates/common/src/error/decode.rs @@ -2,6 +2,8 @@ use std::fmt::Display; use bitcode::{Decode, Encode}; +use crate::error_from_display; + #[derive(Debug, Clone, Encode, Decode)] pub enum Error { Inner(String), @@ -17,8 +19,6 @@ impl Display for Error { } } -impl From for Error { - fn from(value: bitcode::Error) -> Self { - Self::Inner(value.to_string()) - } -} +error_from_display! { Error { + bitcode::Error => Error::Inner, +} } diff --git a/crates/common/src/error/macro_.rs b/crates/common/src/error/macro_.rs new file mode 100644 index 0000000..a58cd78 --- /dev/null +++ b/crates/common/src/error/macro_.rs @@ -0,0 +1,56 @@ +#[macro_export] +macro_rules! error_from { + ($target:ty { $($rest:tt)* }) => { + $crate::error_from!(@arm $target, $($rest)*); + }; + + (@arm $target:ty, $source:ty => $variant:path as $middle:ty, $($rest:tt)*) => { + impl From<$source> for $target { + fn from(value: $source) -> Self { + $variant(<$middle>::from(value).into()) + } + } + $crate::error_from!(@arm $target, $($rest)*); + }; + + (@arm $target:ty, $source:ty => $variant:path, $($rest:tt)*) => { + impl From<$source> for $target { + fn from(value: $source) -> Self { + $variant(value) + } + } + $crate::error_from!(@arm $target, $($rest)*); + }; + + (@arm $target:ty,) => {}; +} + +#[macro_export] +macro_rules! error_from_display { + ($target:ty { $($source:ty => $variant:path),+ $(,)? }) => { + $( + impl From<$source> for $target { + fn from(value: $source) -> Self { + $variant(value.to_string()) + } + } + )+ + }; +} + +#[macro_export] +macro_rules! error_display { + ($target:ty, + delegate = [$($variant:path),* $(,)?], + unit = [$($unit:path => $message:literal),* $(,)?] $(,)? + ) => { + impl std::fmt::Display for $target { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + $($variant(error_value) => std::fmt::Display::fmt(error_value, formatter),)* + $($unit => formatter.write_str($message),)* + } + } + } + }; +} diff --git a/crates/common/src/error/network.rs b/crates/common/src/error/network.rs index 57e6178..6aaa574 100644 --- a/crates/common/src/error/network.rs +++ b/crates/common/src/error/network.rs @@ -2,6 +2,8 @@ use std::fmt::Display; use bitcode::{Decode, Encode}; +use crate::error_from_display; + #[derive(Debug, Clone, Encode, Decode)] pub enum Error { AddressParse(String), @@ -43,56 +45,14 @@ impl Display for Error { } } -impl From for Error { - fn from(value: std::net::AddrParseError) -> Self { - Self::AddressParse(value.to_string()) - } -} - -impl From for Error { - fn from(value: std::io::Error) -> Self { - Self::InputOutput(value.to_string()) - } -} - -impl From for Error { - fn from(value: quinn::ConnectionError) -> Self { - Self::Connection(value.to_string()) - } -} - -impl From for Error { - fn from(value: quinn::ConnectError) -> Self { - Self::AddressParse(value.to_string()) - } -} - -impl From for Error { - fn from(value: quinn::ReadError) -> Self { - Self::Read(value.to_string()) - } -} - -impl From for Error { - fn from(value: quinn::ReadToEndError) -> Self { - Self::ReadToEnd(value.to_string()) - } -} - -impl From for Error { - fn from(value: quinn::ReadExactError) -> Self { - Self::ReadExact(value.to_string()) - } -} - -impl From for Error { - fn from(value: quinn::WriteError) -> Self { - Self::Write(value.to_string()) - } -} - -impl From for Error { - fn from(value: quinn::ClosedStream) -> Self { - Self::ClosedStream(value.to_string()) - } -} +error_from_display! { Error { + std::net::AddrParseError => Error::AddressParse, + std::io::Error => Error::InputOutput, + quinn::ConnectionError => Error::Connection, + quinn::ConnectError => Error::Connect, + quinn::ReadError => Error::Read, + quinn::ReadToEndError => Error::ReadToEnd, + quinn::ReadExactError => Error::ReadExact, + quinn::WriteError => Error::Write, + quinn::ClosedStream => Error::ClosedStream, +} }