Skip to content

Commit 0e79aa6

Browse files
committed
Allow converting AnyConnectOptions to a specific ConnectOptions
1 parent d901694 commit 0e79aa6

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-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

+58-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,62 @@ 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+
#[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)]
51107
pub(crate) enum AnyConnectOptionsKind {
52108
#[cfg(feature = "postgres")]
53109
Postgres(PgConnectOptions),

0 commit comments

Comments
 (0)