forge
Explore
Log in

Don't you dare go Hollow

Collapse error impls into macros

Browse files at this commit

b0443e841142b4a6e81b34b5040caa8972befda7Ahmet Kaan GümüşParent: 719e7a0
  • refactor
    • collapse error impls into macros
Show full message
♻️ refactor(common): collapse error impls into macros

error_from! takes a table of Source => Variant, with an optional `as
Middle` for conversions that hop through an inner error type. Sibling
macros cover the to_string-wrapping leaves and the fully-delegating
Display impls. Enums with per-arm formatting stay hand-written.

ConnectError now maps to network::Error::Connect instead of
AddressParse, which leaves no variant without a producer. Community and
Message swap places in error::Error; the derive encodes variants by
index, so this only holds while both sides ship together.
crates/common/src/error.rsmodified+28 -113
@@ -1,10 +1,11 @@
1-use std::fmt::Display;
2-
3 use bitcode::{Decode, Encode};1 use bitcode::{Decode, Encode};
4 2
3+use crate::{error_display, error_from};
4+
5 pub mod community;5 pub mod community;
6 pub mod decode;6 pub mod decode;
7 pub mod file_operation;7 pub mod file_operation;
8+pub mod macro_;
8 pub mod message;9 pub mod message;
9 pub mod network;10 pub mod network;
10 pub mod user;11 pub mod user;
@@ -13,8 +14,8 @@ pub mod user;
13 pub enum Error {14 pub enum Error {
14 Authenticate,15 Authenticate,
15 User(user::Error),16 User(user::Error),
16- Message(message::Error),
17 Community(community::Error),17 Community(community::Error),
18+ Message(message::Error),
18 FileOperation(file_operation::Error),19 FileOperation(file_operation::Error),
19 Network(network::Error),20 Network(network::Error),
20 Decode(decode::Error),21 Decode(decode::Error),
@@ -23,113 +24,27 @@ pub enum Error {
23 24
24 impl std::error::Error for Error {}25 impl std::error::Error for Error {}
25 26
26-impl Display for Error {27+error_display! { Error,
27- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {28+ delegate = [Error::User, Error::Message, Error::Community, Error::FileOperation, Error::Network, Error::Decode],
28- match self {29+ unit = [Error::Authenticate => "Authenticate", Error::Server => "Server"],
29- Error::Authenticate => write!(f, "Authenticate"),30+}
30- Error::User(error_value) => error_value.fmt(f),31+
31- Error::Message(error_value) => error_value.fmt(f),32+error_from! { Error {
32- Error::Community(error_value) => error_value.fmt(f),33+ user::Error => Error::User,
33- Error::FileOperation(error_value) => error_value.fmt(f),34+ community::Error => Error::Community,
34- Error::Network(error_value) => error_value.fmt(f),35+ message::Error => Error::Message,
35- Error::Decode(error_value) => error_value.fmt(f),36+ file_operation::Error => Error::FileOperation,
36- Error::Server => write!(f, "Server"),37+ network::Error => Error::Network,
37- }38+ decode::Error => Error::Decode,
38- }39+
39-}40+ std::net::AddrParseError => Error::Network as network::Error,
40-41+ std::io::Error => Error::Network as network::Error,
41-impl From<user::Error> for Error {42+ quinn::ConnectionError => Error::Network as network::Error,
42- fn from(value: user::Error) -> Self {43+ quinn::ConnectError => Error::Network as network::Error,
43- Error::User(value)44+ quinn::ReadError => Error::Network as network::Error,
44- }45+ quinn::ReadToEndError => Error::Network as network::Error,
45-}46+ quinn::ReadExactError => Error::Network as network::Error,
46-47+ quinn::WriteError => Error::Network as network::Error,
47-impl From<message::Error> for Error {48+ quinn::ClosedStream => Error::Network as network::Error,
48- fn from(value: message::Error) -> Self {49+ bitcode::Error => Error::Decode as decode::Error,
49- Error::Message(value)50+} }
50- }
51-}
52-
53-impl From<community::Error> for Error {
54- fn from(value: community::Error) -> Self {
55- Error::Community(value)
56- }
57-}
58-
59-impl From<file_operation::Error> for Error {
60- fn from(value: file_operation::Error) -> Self {
61- Error::FileOperation(value)
62- }
63-}
64-
65-impl From<network::Error> for Error {
66- fn from(value: network::Error) -> Self {
67- Error::Network(value)
68- }
69-}
70-
71-impl From<decode::Error> for Error {
72- fn from(value: decode::Error) -> Self {
73- Error::Decode(value)
74- }
75-}
76-
77-impl From<std::net::AddrParseError> for Error {
78- fn from(value: std::net::AddrParseError) -> Self {
79- Self::Network(value.into())
80- }
81-}
82-
83-impl From<std::io::Error> for Error {
84- fn from(value: std::io::Error) -> Self {
85- Self::Network(value.into())
86- }
87-}
88-
89-impl From<quinn::ConnectionError> for Error {
90- fn from(value: quinn::ConnectionError) -> Self {
91- Self::Network(value.into())
92- }
93-}
94-
95-impl From<quinn::ConnectError> for Error {
96- fn from(value: quinn::ConnectError) -> Self {
97- Self::Network(value.into())
98- }
99-}
100-
101-impl From<quinn::ReadError> for Error {
102- fn from(value: quinn::ReadError) -> Self {
103- Self::Network(value.into())
104- }
105-}
106-
107-impl From<quinn::ReadToEndError> for Error {
108- fn from(value: quinn::ReadToEndError) -> Self {
109- Self::Network(value.into())
110- }
111-}
112-
113-impl From<quinn::ReadExactError> for Error {
114- fn from(value: quinn::ReadExactError) -> Self {
115- Self::Network(value.into())
116- }
117-}
118-
119-impl From<quinn::WriteError> for Error {
120- fn from(value: quinn::WriteError) -> Self {
121- Self::Network(value.into())
122- }
123-}
124-
125-impl From<quinn::ClosedStream> for Error {
126- fn from(value: quinn::ClosedStream) -> Self {
127- Self::Network(value.into())
128- }
129-}
130-
131-impl From<bitcode::Error> for Error {
132- fn from(value: bitcode::Error) -> Self {
133- Self::Decode(value.into())
134- }
135-}
crates/common/src/error/decode.rsmodified+5 -5
@@ -2,6 +2,8 @@ use std::fmt::Display;
2 2
3 use bitcode::{Decode, Encode};3 use bitcode::{Decode, Encode};
4 4
5+use crate::error_from_display;
6+
5 #[derive(Debug, Clone, Encode, Decode)]7 #[derive(Debug, Clone, Encode, Decode)]
6 pub enum Error {8 pub enum Error {
7 Inner(String),9 Inner(String),
@@ -17,8 +19,6 @@ impl Display for Error {
17 }19 }
18 }20 }
19 21
20-impl From<bitcode::Error> for Error {22+error_from_display! { Error {
21- fn from(value: bitcode::Error) -> Self {23+ bitcode::Error => Error::Inner,
22- Self::Inner(value.to_string())24+} }
23- }
24-}
crates/common/src/error/macro_.rsadded+56 -0
@@ -0,0 +1,56 @@
1+#[macro_export]
2+macro_rules! error_from {
3+ ($target:ty { $($rest:tt)* }) => {
4+ $crate::error_from!(@arm $target, $($rest)*);
5+ };
6+
7+ (@arm $target:ty, $source:ty => $variant:path as $middle:ty, $($rest:tt)*) => {
8+ impl From<$source> for $target {
9+ fn from(value: $source) -> Self {
10+ $variant(<$middle>::from(value).into())
11+ }
12+ }
13+ $crate::error_from!(@arm $target, $($rest)*);
14+ };
15+
16+ (@arm $target:ty, $source:ty => $variant:path, $($rest:tt)*) => {
17+ impl From<$source> for $target {
18+ fn from(value: $source) -> Self {
19+ $variant(value)
20+ }
21+ }
22+ $crate::error_from!(@arm $target, $($rest)*);
23+ };
24+
25+ (@arm $target:ty,) => {};
26+}
27+
28+#[macro_export]
29+macro_rules! error_from_display {
30+ ($target:ty { $($source:ty => $variant:path),+ $(,)? }) => {
31+ $(
32+ impl From<$source> for $target {
33+ fn from(value: $source) -> Self {
34+ $variant(value.to_string())
35+ }
36+ }
37+ )+
38+ };
39+}
40+
41+#[macro_export]
42+macro_rules! error_display {
43+ ($target:ty,
44+ delegate = [$($variant:path),* $(,)?],
45+ unit = [$($unit:path => $message:literal),* $(,)?] $(,)?
46+ ) => {
47+ impl std::fmt::Display for $target {
48+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49+ match self {
50+ $($variant(error_value) => std::fmt::Display::fmt(error_value, formatter),)*
51+ $($unit => formatter.write_str($message),)*
52+ }
53+ }
54+ }
55+ };
56+}
crates/common/src/error/network.rsmodified+13 -53
@@ -2,6 +2,8 @@ use std::fmt::Display;
2 2
3 use bitcode::{Decode, Encode};3 use bitcode::{Decode, Encode};
4 4
5+use crate::error_from_display;
6+
5 #[derive(Debug, Clone, Encode, Decode)]7 #[derive(Debug, Clone, Encode, Decode)]
6 pub enum Error {8 pub enum Error {
7 AddressParse(String),9 AddressParse(String),
@@ -43,56 +45,14 @@ impl Display for Error {
43 }45 }
44 }46 }
45 47
46-impl From<std::net::AddrParseError> for Error {48+error_from_display! { Error {
47- fn from(value: std::net::AddrParseError) -> Self {49+ std::net::AddrParseError => Error::AddressParse,
48- Self::AddressParse(value.to_string())50+ std::io::Error => Error::InputOutput,
49- }51+ quinn::ConnectionError => Error::Connection,
50-}52+ quinn::ConnectError => Error::Connect,
51-53+ quinn::ReadError => Error::Read,
52-impl From<std::io::Error> for Error {54+ quinn::ReadToEndError => Error::ReadToEnd,
53- fn from(value: std::io::Error) -> Self {55+ quinn::ReadExactError => Error::ReadExact,
54- Self::InputOutput(value.to_string())56+ quinn::WriteError => Error::Write,
55- }57+ quinn::ClosedStream => Error::ClosedStream,
56-}58+} }
57-
58-impl From<quinn::ConnectionError> for Error {
59- fn from(value: quinn::ConnectionError) -> Self {
60- Self::Connection(value.to_string())
61- }
62-}
63-
64-impl From<quinn::ConnectError> for Error {
65- fn from(value: quinn::ConnectError) -> Self {
66- Self::AddressParse(value.to_string())
67- }
68-}
69-
70-impl From<quinn::ReadError> for Error {
71- fn from(value: quinn::ReadError) -> Self {
72- Self::Read(value.to_string())
73- }
74-}
75-
76-impl From<quinn::ReadToEndError> for Error {
77- fn from(value: quinn::ReadToEndError) -> Self {
78- Self::ReadToEnd(value.to_string())
79- }
80-}
81-
82-impl From<quinn::ReadExactError> for Error {
83- fn from(value: quinn::ReadExactError) -> Self {
84- Self::ReadExact(value.to_string())
85- }
86-}
87-
88-impl From<quinn::WriteError> for Error {
89- fn from(value: quinn::WriteError) -> Self {
90- Self::Write(value.to_string())
91- }
92-}
93-
94-impl From<quinn::ClosedStream> for Error {
95- fn from(value: quinn::ClosedStream) -> Self {
96- Self::ClosedStream(value.to_string())
97- }
98-}