@@ -8,12 +8,43 @@ use alloy::{
88 } ,
99} ;
1010
11+ /// Errors when connecting a provider
12+ #[ derive( Debug , thiserror:: Error , Clone , PartialEq , Eq ) ]
13+ pub enum ProviderConnectError {
14+ /// Pubsub is not available for the configured transport
15+ #[ error( "pubsub is not available for the configured transport" ) ]
16+ PubsubUnavailable ,
17+ /// Custom error message
18+ #[ error( "{0}" ) ]
19+ Custom ( String ) ,
20+ }
21+
22+ impl From < TransportErrorKind > for ProviderConnectError {
23+ fn from ( err : TransportErrorKind ) -> Self {
24+ match err {
25+ TransportErrorKind :: Custom ( err) => ProviderConnectError :: Custom ( err. to_string ( ) ) ,
26+ TransportErrorKind :: PubsubUnavailable => ProviderConnectError :: PubsubUnavailable ,
27+ _ => panic ! ( "Unexpected TransportErrorKind variant: {err:?}" ) ,
28+ }
29+ }
30+ }
31+
32+ impl From < TransportError > for ProviderConnectError {
33+ fn from ( err : TransportError ) -> Self {
34+ match err {
35+ TransportError :: Transport ( e) => e. into ( ) ,
36+ _ => panic ! ( "Unexpected TransportError variant: {err:?}" ) ,
37+ }
38+ }
39+ }
40+
1141impl FromEnvVar for BuiltInConnectionString {
12- type Error = TransportError ;
42+ type Error = ProviderConnectError ;
1343
1444 fn from_env_var ( env_var : & str ) -> Result < Self , FromEnvErr < Self :: Error > > {
1545 let conn_str = String :: from_env_var ( env_var) . map_err ( FromEnvErr :: infallible_into) ?;
16- conn_str. parse ( ) . map_err ( Into :: into)
46+ let built_in = conn_str. parse ( ) . map_err ( ProviderConnectError :: from) ?;
47+ Ok ( built_in)
1748 }
1849}
1950
@@ -41,7 +72,7 @@ impl ProviderConfig {
4172}
4273
4374impl FromEnvVar for ProviderConfig {
44- type Error = TransportError ;
75+ type Error = ProviderConnectError ;
4576
4677 fn from_env_var ( env_var : & str ) -> Result < Self , FromEnvErr < Self :: Error > > {
4778 let connection_string = BuiltInConnectionString :: from_env_var ( env_var) ?;
@@ -81,21 +112,21 @@ impl PubSubConfig {
81112}
82113
83114impl TryFrom < BuiltInConnectionString > for PubSubConfig {
84- type Error = TransportError ;
115+ type Error = ProviderConnectError ;
85116
86117 fn try_from ( connection_string : BuiltInConnectionString ) -> Result < Self , Self :: Error > {
87118 if !matches ! (
88119 connection_string,
89120 BuiltInConnectionString :: Ws ( _, _) | BuiltInConnectionString :: Ipc ( _)
90121 ) {
91- return Err ( TransportErrorKind :: pubsub_unavailable ( ) ) ;
122+ return Err ( ProviderConnectError :: PubsubUnavailable ) ;
92123 }
93124 Ok ( Self { connection_string } )
94125 }
95126}
96127
97128impl FromEnvVar for PubSubConfig {
98- type Error = TransportError ;
129+ type Error = ProviderConnectError ;
99130
100131 fn from_env_var ( env_var : & str ) -> Result < Self , FromEnvErr < Self :: Error > > {
101132 let cs = BuiltInConnectionString :: from_env_var ( env_var) ?;
@@ -137,3 +168,18 @@ impl PubSubConnect for PubSubConfig {
137168 }
138169 }
139170}
171+
172+ #[ cfg( test) ]
173+ mod test {
174+ use super :: * ;
175+ use crate :: utils:: from_env:: FromEnv ;
176+
177+ #[ derive( FromEnv , Debug , Clone , PartialEq , Eq ) ]
178+ #[ from_env( crate ) ]
179+ struct CompileCheck {
180+ #[ from_env( var = "COOL_DUDE" , desc = "provider" ) ]
181+ cool_dude : ProviderConfig ,
182+ #[ from_env( var = "COOL_DUDE2" , desc = "provider2" ) ]
183+ cool_dude2 : PubSubConfig ,
184+ }
185+ }
0 commit comments