Skip to content

Commit 9890bdb

Browse files
committed
Allow converting AnyConnectOptions to a specific ConnectOptions
1 parent d901694 commit 9890bdb

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqlx-core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ _tls-rustls = ["rustls", "webpki", "webpki-roots"]
9999
offline = ["serde", "either/serde"]
100100

101101
[dependencies]
102+
paste = "1.0.6"
102103
ahash = "0.7.6"
103104
atoi = "0.4.0"
104105
sqlx-rt = { path = "../sqlx-rt", version = "0.5.10"}

sqlx-core/src/any/options.rs

+68-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::connection::ConnectOptions;
33
use crate::error::Error;
44
use futures_core::future::BoxFuture;
55
use log::LevelFilter;
6+
use std::convert::TryFrom;
67
use std::str::FromStr;
78
use std::time::Duration;
89

@@ -26,7 +27,7 @@ use crate::mssql::MssqlConnectOptions;
2627
/// postgres://postgres:password@localhost/database
2728
/// mysql://root:password@localhost/database
2829
/// ```
29-
#[derive(Debug)]
30+
#[derive(Debug, Clone)]
3031
pub struct AnyConnectOptions(pub(crate) AnyConnectOptionsKind);
3132

3233
impl AnyConnectOptions {
@@ -47,7 +48,72 @@ impl AnyConnectOptions {
4748
}
4849
}
4950

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)]
51117
pub(crate) enum AnyConnectOptionsKind {
52118
#[cfg(feature = "postgres")]
53119
Postgres(PgConnectOptions),

0 commit comments

Comments
 (0)