@@ -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,67 @@ impl AnyConnectOptions {
47
48
}
48
49
}
49
50
50
- #[ derive( Debug ) ]
51
+ #[ cfg( feature = "postgres" ) ]
52
+ impl TryFrom < AnyConnectOptions > for PgConnectOptions {
53
+ type Error = Error ;
54
+
55
+ fn try_from ( value : AnyConnectOptions ) -> Result < Self , Self :: Error > {
56
+ if let AnyConnectOptionsKind :: Postgres ( connect_options) = value. 0 {
57
+ Ok ( connect_options)
58
+ } else {
59
+ Err ( Error :: Configuration (
60
+ "Not postgres typed AnyConnectOptions" . into ( ) ,
61
+ ) )
62
+ }
63
+ }
64
+ }
65
+
66
+ #[ cfg( feature = "mysql" ) ]
67
+ impl TryFrom < AnyConnectOptions > for MySqlConnectOptions {
68
+ type Error = Error ;
69
+
70
+ fn try_from ( value : AnyConnectOptions ) -> Result < Self , Self :: Error > {
71
+ if let AnyConnectOptionsKind :: MySql ( connect_options) = value. 0 {
72
+ Ok ( connect_options)
73
+ } else {
74
+ Err ( Error :: Configuration (
75
+ "Not mysql typed AnyConnectOptions" . into ( ) ,
76
+ ) )
77
+ }
78
+ }
79
+ }
80
+
81
+ #[ cfg( feature = "sqlite" ) ]
82
+ impl TryFrom < AnyConnectOptions > for SqliteConnectOptions {
83
+ type Error = Error ;
84
+
85
+ fn try_from ( value : AnyConnectOptions ) -> Result < Self , Self :: Error > {
86
+ if let AnyConnectOptionsKind :: Sqlite ( connect_options) = value. 0 {
87
+ Ok ( connect_options)
88
+ } else {
89
+ Err ( Error :: Configuration (
90
+ "Not sqlite typed AnyConnectOptions" . into ( ) ,
91
+ ) )
92
+ }
93
+ }
94
+ }
95
+
96
+ #[ cfg( feature = "mssql" ) ]
97
+ impl TryFrom < AnyConnectOptions > for MssqlConnectOptions {
98
+ type Error = Error ;
99
+
100
+ fn try_from ( value : AnyConnectOptions ) -> Result < Self , Self :: Error > {
101
+ if let AnyConnectOptionsKind :: Mssql ( connect_options) = value {
102
+ Ok ( connect_options)
103
+ } else {
104
+ Err ( Error :: Configuration (
105
+ "Not mssql typed AnyConnectOptions" . into ( ) ,
106
+ ) )
107
+ }
108
+ }
109
+ }
110
+
111
+ #[ derive( Debug , Clone ) ]
51
112
pub ( crate ) enum AnyConnectOptionsKind {
52
113
#[ cfg( feature = "postgres" ) ]
53
114
Postgres ( PgConnectOptions ) ,
0 commit comments