@@ -3,6 +3,7 @@ use crate::connection::ConnectOptions;
3
3
use crate :: error:: Error ;
4
4
use futures_core:: future:: BoxFuture ;
5
5
use log:: LevelFilter ;
6
+ use std:: convert:: TryFrom ;
6
7
use std:: str:: FromStr ;
7
8
use std:: time:: Duration ;
8
9
@@ -26,7 +27,7 @@ use crate::mssql::MssqlConnectOptions;
26
27
/// postgres://postgres:password@localhost/database
27
28
/// mysql://root:password@localhost/database
28
29
/// ```
29
- #[ derive( Debug ) ]
30
+ #[ derive( Debug , Clone ) ]
30
31
pub struct AnyConnectOptions ( pub ( crate ) AnyConnectOptionsKind ) ;
31
32
32
33
impl AnyConnectOptions {
@@ -47,7 +48,62 @@ impl AnyConnectOptions {
47
48
}
48
49
}
49
50
50
- #[ derive( Debug ) ]
51
+ macro_rules! try_from_any_connect_options_to {
52
+ ( $to: ty, $kind: path, $name: expr) => {
53
+ impl TryFrom <AnyConnectOptions > for $to {
54
+ type Error = Error ;
55
+
56
+ #[ allow( irrefutable_if_let) ]
57
+ fn try_from( value: AnyConnectOptions ) -> Result <Self , Self :: Error > {
58
+ if let $kind( connect_options) = value. 0 {
59
+ Ok ( connect_options)
60
+ } else {
61
+ Err ( Error :: Configuration (
62
+ format!( "Not {} typed AnyConnectOptions" , $name) . into( ) ,
63
+ ) )
64
+ }
65
+ }
66
+ }
67
+
68
+ impl AnyConnectOptions {
69
+ paste:: item! {
70
+ pub fn [ < as_ $name >] ( & self ) -> Option <& $to> {
71
+ if let $kind( ref connect_options) = self . 0 {
72
+ Some ( connect_options)
73
+ } else {
74
+ None
75
+ }
76
+ }
77
+
78
+ pub fn [ < as_ $name _mut >] ( & mut self ) -> Option <& mut $to> {
79
+ if let $kind( ref mut connect_options) = self . 0 {
80
+ Some ( connect_options)
81
+ } else {
82
+ None
83
+ }
84
+ }
85
+
86
+ pub fn [ < as_ $name _cloned >] ( & self ) -> Option <$to> {
87
+ self . [ < as_ $name >] ( ) . cloned( )
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
93
+
94
+ #[ cfg( feature = "postgres" ) ]
95
+ try_from_any_connect_options_to ! ( PgConnectOptions , AnyConnectOptionsKind :: Postgres , "postgres" ) ;
96
+
97
+ #[ cfg( feature = "mysql" ) ]
98
+ try_from_any_connect_options_to ! ( MySqlConnectOptions , AnyConnectOptionsKind :: MySql , "mysql" ) ;
99
+
100
+ #[ cfg( feature = "sqlite" ) ]
101
+ try_from_any_connect_options_to ! ( SqliteConnectOptions , AnyConnectOptionsKind :: Sqlite , "sqlite" ) ;
102
+
103
+ #[ cfg( feature = "mssql" ) ]
104
+ try_from_any_connect_options_to ! ( MssqlConnectOptions , AnyConnectOptionsKind :: Mssql , "mssql" ) ;
105
+
106
+ #[ derive( Debug , Clone ) ]
51
107
pub ( crate ) enum AnyConnectOptionsKind {
52
108
#[ cfg( feature = "postgres" ) ]
53
109
Postgres ( PgConnectOptions ) ,
0 commit comments