Skip to content

Commit

Permalink
Allow converting AnyConnectOptions to a specific ConnectOptions (#1610)
Browse files Browse the repository at this point in the history
  • Loading branch information
05storm26 committed Feb 17, 2022
1 parent 45854a4 commit 8bccd53
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ _tls-rustls = ["rustls", "webpki", "webpki-roots"]
offline = ["serde", "either/serde"]

[dependencies]
paste = "1.0.6"
ahash = "0.7.6"
atoi = "0.4.0"
sqlx-rt = { path = "../sqlx-rt", version = "0.5.10"}
Expand Down
66 changes: 64 additions & 2 deletions sqlx-core/src/any/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::connection::ConnectOptions;
use crate::error::Error;
use futures_core::future::BoxFuture;
use log::LevelFilter;
use std::convert::TryFrom;
use std::str::FromStr;
use std::time::Duration;

Expand All @@ -26,7 +27,7 @@ use crate::mssql::MssqlConnectOptions;
/// postgres://postgres:password@localhost/database
/// mysql://root:password@localhost/database
/// ```
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct AnyConnectOptions(pub(crate) AnyConnectOptionsKind);

impl AnyConnectOptions {
Expand All @@ -47,7 +48,68 @@ impl AnyConnectOptions {
}
}

#[derive(Debug)]
macro_rules! try_from_any_connect_options_to {
($to:ty, $kind:path, $name:expr) => {
impl TryFrom<AnyConnectOptions> for $to {
type Error = Error;

fn try_from(value: AnyConnectOptions) -> Result<Self, Self::Error> {
#[allow(irrefutable_let_patterns)]
if let $kind(connect_options) = value.0 {
Ok(connect_options)
} else {
Err(Error::Configuration(
format!("Not {} typed AnyConnectOptions", $name).into(),
))
}
}
}

impl AnyConnectOptions {
paste::item! {
pub fn [< as_ $name >] (&self) -> Option<&$to> {
#[allow(irrefutable_let_patterns)]
if let $kind(ref connect_options) = self.0 {
Some(connect_options)
} else {
None
}
}

pub fn [< as_ $name _mut >] (&mut self) -> Option<&mut $to> {
#[allow(irrefutable_let_patterns)]
if let $kind(ref mut connect_options) = self.0 {
Some(connect_options)
} else {
None
}
}
}
}
};
}

#[cfg(feature = "postgres")]
try_from_any_connect_options_to!(
PgConnectOptions,
AnyConnectOptionsKind::Postgres,
"postgres"
);

#[cfg(feature = "mysql")]
try_from_any_connect_options_to!(MySqlConnectOptions, AnyConnectOptionsKind::MySql, "mysql");

#[cfg(feature = "sqlite")]
try_from_any_connect_options_to!(
SqliteConnectOptions,
AnyConnectOptionsKind::Sqlite,
"sqlite"
);

#[cfg(feature = "mssql")]
try_from_any_connect_options_to!(MssqlConnectOptions, AnyConnectOptionsKind::Mssql, "mssql");

#[derive(Debug, Clone)]
pub(crate) enum AnyConnectOptionsKind {
#[cfg(feature = "postgres")]
Postgres(PgConnectOptions),
Expand Down

0 comments on commit 8bccd53

Please sign in to comment.