Allow converting AnyConnectOptions to a specific ConnectOptions (#1610)

This commit is contained in:
05storm26 2022-02-17 23:57:48 +01:00 committed by GitHub
parent 45854a4246
commit 8bccd53346
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 2 deletions

1
Cargo.lock generated
View file

@ -2449,6 +2449,7 @@ dependencies = [
"memchr",
"num-bigint",
"once_cell",
"paste",
"percent-encoding",
"rand",
"regex",

View file

@ -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"}

View file

@ -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;
@ -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 {
@ -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),