pub struct Config {
pub application_name: Option<ApplicationName>,
pub database: Database,
pub endpoint: Endpoint,
pub password: Option<Password>,
pub ssl_mode: SslMode,
pub ssl_root_cert: Option<SslRootCert>,
pub username: Username,
}Expand description
PG connection config with various presentation modes.
Supported:
- Env variables via
to_pg_env() - JSON document via
serde - sqlx connect options via
to_sqlx_connect_options() - Individual field access
Fields§
§application_name: Option<ApplicationName>§database: Database§endpoint: Endpoint§password: Option<Password>§ssl_mode: SslMode§ssl_root_cert: Option<SslRootCert>§username: UsernameImplementations§
Source§impl Config
impl Config
Sourcepub fn to_url(&self) -> Url
pub fn to_url(&self) -> Url
Convert to PG connection URL
let config = Config {
application_name: None,
database: Database::from_str("some-database").unwrap(),
endpoint: Endpoint::Network {
host: Host::from_str("some-host").unwrap(),
host_addr: None,
port: Some(Port(5432)),
},
password: None,
ssl_mode: SslMode::VerifyFull,
ssl_root_cert: None,
username: Username::from_str("some-username").unwrap(),
};
let options = config.to_sqlx_connect_options();
assert_eq!(
Url::parse(
"postgres://some-username@some-host:5432/some-database?sslmode=verify-full"
).unwrap(),
config.to_url()
);
assert_eq!(
Url::parse(
"postgres://some-username:some-password@some-host:5432/some-database?application_name=some-app&sslmode=verify-full&sslrootcert=%2Fsome.pem"
).unwrap(),
Config {
application_name: Some(ApplicationName::from_str("some-app").unwrap()),
password: Some(Password::from_str("some-password").unwrap()),
ssl_root_cert: Some(SslRootCert::File("/some.pem".into())),
..config.clone()
}.to_url()
);
assert_eq!(
Url::parse(
"postgres://some-username@some-host:5432/some-database?hostaddr=127.0.0.1&sslmode=verify-full"
).unwrap(),
Config {
endpoint: Endpoint::Network {
host: Host::from_str("some-host").unwrap(),
host_addr: Some("127.0.0.1".parse().unwrap()),
port: Some(Port(5432)),
},
..config.clone()
}.to_url()
);
// IPv4 example
let ipv4_config = Config {
application_name: None,
database: Database::from_str("mydb").unwrap(),
endpoint: Endpoint::Network {
host: Host::IpAddr(std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1))),
host_addr: None,
port: Some(Port(5432)),
},
password: None,
ssl_mode: SslMode::Disable,
ssl_root_cert: None,
username: Username::from_str("user").unwrap(),
};
assert_eq!(
ipv4_config.to_url().to_string(),
"postgres://user@127.0.0.1:5432/mydb?sslmode=disable"
);
// IPv6 example (automatically bracketed)
let ipv6_config = Config {
application_name: None,
database: Database::from_str("mydb").unwrap(),
endpoint: Endpoint::Network {
host: Host::IpAddr(std::net::IpAddr::V6(std::net::Ipv6Addr::LOCALHOST)),
host_addr: None,
port: Some(Port(5432)),
},
password: None,
ssl_mode: SslMode::Disable,
ssl_root_cert: None,
username: Username::from_str("user").unwrap(),
};
assert_eq!(
ipv6_config.to_url().to_string(),
"postgres://user@[::1]:5432/mydb?sslmode=disable"
);Sourcepub fn to_pg_env(&self) -> BTreeMap<&'static str, String>
pub fn to_pg_env(&self) -> BTreeMap<&'static str, String>
Convert to PG environment variable names
let config = Config {
application_name: None,
database: "some-database".parse().unwrap(),
endpoint: Endpoint::Network {
host: "some-host".parse().unwrap(),
host_addr: None,
port: Some(Port(5432)),
},
password: None,
ssl_mode: SslMode::VerifyFull,
ssl_root_cert: None,
username: "some-username".parse().unwrap(),
};
let expected = BTreeMap::from([
("PGDATABASE", "some-database".to_string()),
("PGHOST", "some-host".to_string()),
("PGPORT", "5432".to_string()),
("PGSSLMODE", "verify-full".to_string()),
("PGUSER", "some-username".to_string()),
]);
assert_eq!(expected, config.to_pg_env());
let config_with_optionals = Config {
application_name: Some("some-app".parse().unwrap()),
endpoint: Endpoint::Network {
host: "some-host".parse().unwrap(),
host_addr: Some("127.0.0.1".parse().unwrap()),
port: Some(Port(5432)),
},
password: Some("some-password".parse().unwrap()),
ssl_root_cert: Some(SslRootCert::File("/some.pem".into())),
..config
};
let expected = BTreeMap::from([
("PGAPPNAME", "some-app".to_string()),
("PGDATABASE", "some-database".to_string()),
("PGHOST", "some-host".to_string()),
("PGHOSTADDR", "127.0.0.1".to_string()),
("PGPASSWORD", "some-password".to_string()),
("PGPORT", "5432".to_string()),
("PGSSLMODE", "verify-full".to_string()),
("PGSSLROOTCERT", "/some.pem".to_string()),
("PGUSER", "some-username".to_string()),
]);
assert_eq!(expected, config_with_optionals.to_pg_env());Sourcepub fn to_sqlx_connect_options(
&self,
) -> Result<PgConnectOptions, SqlxOptionsError>
pub fn to_sqlx_connect_options( &self, ) -> Result<PgConnectOptions, SqlxOptionsError>
Convert to an sqlx pg connection config
let config = Config {
application_name: Some(ApplicationName::from_str("some-app").unwrap()),
database: Database::from_str("some-database").unwrap(),
endpoint: Endpoint::Network {
host: Host::from_str("some-host").unwrap(),
host_addr: None,
port: Some(Port(5432)),
},
password: Some(Password::from_str("some-password").unwrap()),
ssl_mode: SslMode::VerifyFull,
ssl_root_cert: Some(SslRootCert::File("/some.pem".into())),
username: Username::from_str("some-username").unwrap(),
};
let options = config.to_sqlx_connect_options().unwrap();
// `PgConnectOptions` does not have `PartialEq` and only partial getters
// so we can only assert a few fields.
assert_eq!(Some("some-app"), options.get_application_name());
assert_eq!("some-host", options.get_host());
assert_eq!(5432, options.get_port());
assert_eq!("some-username", options.get_username());
// No PartialEQ instance
assert_eq!(format!("{:#?}", sqlx::postgres::PgSslMode::VerifyFull), format!("{:#?}", options.get_ssl_mode()));
assert_eq!(Some("some-database"), options.get_database());
// Unsupported.
// assert_eq!("some-password", options.get_password());
// assert_eq!("/some.pem", options.get_ssl_root_cert());§Errors
Returns an error if fields inferred from the process environment variables
by PgConnectOptions::new contradict the settings in Config, and
there is no public API in PgConnectOptions to reset these values.
pub async fn with_sqlx_connection<T, F: AsyncFnMut(&mut PgConnection) -> T>( &self, action: F, ) -> Result<T, SqlxConnectionError>
pub fn endpoint(self, endpoint: Endpoint) -> Self
Trait Implementations§
impl Eq for Config
impl StructuralPartialEq for Config
Auto Trait Implementations§
impl Freeze for Config
impl RefUnwindSafe for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl UnwindSafe for Config
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more