@@ -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,72 @@ 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
+ fn try_from( value: AnyConnectOptions ) -> Result <Self , Self :: Error > {
57
+ #[ allow( irrefutable_let_patterns) ]
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
+ #[ allow( irrefutable_let_patterns) ]
72
+ if let $kind( ref connect_options) = self . 0 {
73
+ Some ( connect_options)
74
+ } else {
75
+ None
76
+ }
77
+ }
78
+
79
+ pub fn [ < as_ $name _mut >] ( & mut self ) -> Option <& mut $to> {
80
+ #[ allow( irrefutable_let_patterns) ]
81
+ if let $kind( ref mut connect_options) = self . 0 {
82
+ Some ( connect_options)
83
+ } else {
84
+ None
85
+ }
86
+ }
87
+
88
+ pub fn [ < as_ $name _cloned >] ( & self ) -> Option <$to> {
89
+ self . [ < as_ $name >] ( ) . cloned( )
90
+ }
91
+ }
92
+ }
93
+ } ;
94
+ }
95
+
96
+ #[ cfg( feature = "postgres" ) ]
97
+ try_from_any_connect_options_to ! (
98
+ PgConnectOptions ,
99
+ AnyConnectOptionsKind :: Postgres ,
100
+ "postgres"
101
+ ) ;
102
+
103
+ #[ cfg( feature = "mysql" ) ]
104
+ try_from_any_connect_options_to ! ( MySqlConnectOptions , AnyConnectOptionsKind :: MySql , "mysql" ) ;
105
+
106
+ #[ cfg( feature = "sqlite" ) ]
107
+ try_from_any_connect_options_to ! (
108
+ SqliteConnectOptions ,
109
+ AnyConnectOptionsKind :: Sqlite ,
110
+ "sqlite"
111
+ ) ;
112
+
113
+ #[ cfg( feature = "mssql" ) ]
114
+ try_from_any_connect_options_to ! ( MssqlConnectOptions , AnyConnectOptionsKind :: Mssql , "mssql" ) ;
115
+
116
+ #[ derive( Debug , Clone ) ]
51
117
pub ( crate ) enum AnyConnectOptionsKind {
52
118
#[ cfg( feature = "postgres" ) ]
53
119
Postgres ( PgConnectOptions ) ,
0 commit comments