@@ -5,6 +5,7 @@ use crate::{
5
5
error:: { AmqpDescribedError , AmqpErrorCondition , AmqpErrorKind } ,
6
6
AmqpError ,
7
7
} ;
8
+ use std:: str:: FromStr ;
8
9
9
10
// newtype implementations for fe2o3_amqp errors. These should only be used if the transform of the
10
11
// fe2o3_amqp error directly to an AmqpError is not possible. This is the case for errors which end up
@@ -45,28 +46,20 @@ impl From<fe2o3_amqp::transport::Error> for Fe2o3TransportError {
45
46
}
46
47
47
48
// Specializations of From for common AMQP types.
48
- impl From < fe2o3_amqp_types:: definitions:: ErrorCondition > for AmqpErrorCondition {
49
- fn from ( e : fe2o3_amqp_types:: definitions:: ErrorCondition ) -> Self {
49
+ impl From < & fe2o3_amqp_types:: definitions:: ErrorCondition > for AmqpErrorCondition {
50
+ fn from ( e : & fe2o3_amqp_types:: definitions:: ErrorCondition ) -> Self {
50
51
match e {
51
52
fe2o3_amqp_types:: definitions:: ErrorCondition :: AmqpError ( amqp_error) => {
52
- AmqpErrorCondition :: from ( crate :: AmqpSymbol :: from (
53
- fe2o3_amqp_types:: primitives:: Symbol :: from ( & amqp_error) ,
54
- ) )
53
+ AmqpErrorCondition :: from ( amqp_error)
55
54
}
56
55
fe2o3_amqp_types:: definitions:: ErrorCondition :: ConnectionError ( connection_error) => {
57
- AmqpErrorCondition :: from ( crate :: AmqpSymbol :: from (
58
- fe2o3_amqp_types:: primitives:: Symbol :: from ( & connection_error) ,
59
- ) )
56
+ AmqpErrorCondition :: from ( connection_error)
60
57
}
61
58
fe2o3_amqp_types:: definitions:: ErrorCondition :: SessionError ( session_error) => {
62
- AmqpErrorCondition :: from ( crate :: AmqpSymbol :: from (
63
- fe2o3_amqp_types:: primitives:: Symbol :: from ( & session_error) ,
64
- ) )
59
+ AmqpErrorCondition :: from ( session_error)
65
60
}
66
61
fe2o3_amqp_types:: definitions:: ErrorCondition :: LinkError ( link_error) => {
67
- AmqpErrorCondition :: from ( crate :: AmqpSymbol :: from (
68
- fe2o3_amqp_types:: primitives:: Symbol :: from ( & link_error) ,
69
- ) )
62
+ AmqpErrorCondition :: from ( link_error)
70
63
}
71
64
fe2o3_amqp_types:: definitions:: ErrorCondition :: Custom ( symbol) => {
72
65
AmqpErrorCondition :: from ( crate :: AmqpSymbol :: from ( symbol) )
@@ -75,12 +68,42 @@ impl From<fe2o3_amqp_types::definitions::ErrorCondition> for AmqpErrorCondition
75
68
}
76
69
}
77
70
71
+ // Implement specific From traits for the error types we need instead of using a generic implementation
72
+ impl From < & fe2o3_amqp_types:: definitions:: AmqpError > for AmqpErrorCondition {
73
+ fn from ( e : & fe2o3_amqp_types:: definitions:: AmqpError ) -> Self {
74
+ // Note that the `from_str` implementation from `create_extensible_enum` will
75
+ // never return an error. So the `unwrap` is there to silence the compiler.
76
+ AmqpErrorCondition :: from_str ( fe2o3_amqp_types:: primitives:: Symbol :: from ( e) . as_str ( ) )
77
+ . unwrap ( )
78
+ }
79
+ }
80
+
81
+ impl From < & fe2o3_amqp_types:: definitions:: ConnectionError > for AmqpErrorCondition {
82
+ fn from ( e : & fe2o3_amqp_types:: definitions:: ConnectionError ) -> Self {
83
+ AmqpErrorCondition :: from_str ( fe2o3_amqp_types:: primitives:: Symbol :: from ( e) . as_str ( ) )
84
+ . unwrap ( )
85
+ }
86
+ }
87
+
88
+ impl From < & fe2o3_amqp_types:: definitions:: SessionError > for AmqpErrorCondition {
89
+ fn from ( e : & fe2o3_amqp_types:: definitions:: SessionError ) -> Self {
90
+ AmqpErrorCondition :: from_str ( fe2o3_amqp_types:: primitives:: Symbol :: from ( e) . as_str ( ) )
91
+ . unwrap ( )
92
+ }
93
+ }
94
+
95
+ impl From < & fe2o3_amqp_types:: definitions:: LinkError > for AmqpErrorCondition {
96
+ fn from ( e : & fe2o3_amqp_types:: definitions:: LinkError ) -> Self {
97
+ AmqpErrorCondition :: from_str ( fe2o3_amqp_types:: primitives:: Symbol :: from ( e) . as_str ( ) )
98
+ . unwrap ( )
99
+ }
100
+ }
78
101
impl From < fe2o3_amqp_types:: definitions:: Error > for AmqpDescribedError {
79
102
fn from ( e : fe2o3_amqp_types:: definitions:: Error ) -> Self {
80
103
AmqpDescribedError :: new (
81
- e. condition . into ( ) ,
104
+ ( & e. condition ) . into ( ) ,
82
105
e. description ,
83
- e. info . unwrap_or_default ( ) . into ( ) ,
106
+ ( & e. info . unwrap_or_default ( ) ) . into ( ) ,
84
107
)
85
108
}
86
109
}
@@ -162,7 +185,7 @@ mod tests {
162
185
let error = fe2o3_amqp_types:: definitions:: ErrorCondition :: $fe2o3_type(
163
186
fe2o3_amqp_types:: definitions:: $fe2o3_type:: $variant,
164
187
) ;
165
- let amqp_error = AmqpErrorCondition :: from( error) ;
188
+ let amqp_error = AmqpErrorCondition :: from( & error) ;
166
189
assert_eq!( amqp_error, AmqpErrorCondition :: $amqp_variant) ;
167
190
}
168
191
} ;
0 commit comments