pub struct Config {
pub endpoint: Endpoint,
pub session: Session,
pub ssl_mode: SslMode,
pub ssl_root_cert: Option<SslRootCert>,
}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§
§endpoint: Endpoint§session: Session§ssl_mode: SslMode§ssl_root_cert: Option<SslRootCert>Implementations§
Source§impl Config
impl Config
Sourcepub fn to_url(&self) -> Uri<String>
pub fn to_url(&self) -> Uri<String>
Convert to PG connection URL
let config = Config {
endpoint: Endpoint::Network {
host: Host::from_str("some-host").unwrap(),
channel_binding: None,
host_addr: None,
port: Some(Port::new(5432)),
},
session: Session {
application_name: None,
database: Database::from_static_or_panic("some-database"),
password: None,
user: User::from_static_or_panic("some-user"),
},
ssl_mode: SslMode::VerifyFull,
ssl_root_cert: None,
sqlx: Default::default(), // requires "sqlx" feature
};
assert_eq!(
config.to_url_string(),
"postgres://some-user@some-host:5432/some-database?sslmode=verify-full"
);
assert_eq!(
Config {
session: Session {
application_name: Some(ApplicationName::from_str("some-app").unwrap()),
password: Some(Password::from_str("some-password").unwrap()),
..config.session.clone()
},
ssl_root_cert: Some(SslRootCert::File("/some.pem".into())),
..config.clone()
}.to_url_string(),
"postgres://some-user:some-password@some-host:5432/some-database?application_name=some-app&sslmode=verify-full&sslrootcert=%2Fsome.pem"
);
assert_eq!(
Config {
endpoint: Endpoint::Network {
host: Host::from_str("some-host").unwrap(),
channel_binding: None,
host_addr: Some("127.0.0.1".parse().unwrap()),
port: Some(Port::new(5432)),
},
..config.clone()
}.to_url_string(),
"postgres://some-user@some-host:5432/some-database?hostaddr=127.0.0.1&sslmode=verify-full"
);
// IPv4 example
let ipv4_config = Config {
endpoint: Endpoint::Network {
host: Host::IpAddr(std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1))),
channel_binding: None,
host_addr: None,
port: Some(Port::new(5432)),
},
session: Session {
application_name: None,
database: Database::from_static_or_panic("mydb"),
password: None,
user: User::from_static_or_panic("user"),
},
ssl_mode: SslMode::Disable,
ssl_root_cert: None,
sqlx: Default::default(), // requires "sqlx" feature
};
assert_eq!(
ipv4_config.to_url_string(),
"postgres://user@127.0.0.1:5432/mydb?sslmode=disable"
);
// IPv6 example (automatically bracketed)
let ipv6_config = Config {
endpoint: Endpoint::Network {
host: Host::IpAddr(std::net::IpAddr::V6(std::net::Ipv6Addr::LOCALHOST)),
channel_binding: None,
host_addr: None,
port: Some(Port::new(5432)),
},
session: Session {
application_name: None,
database: Database::from_static_or_panic("mydb"),
password: None,
user: User::from_static_or_panic("user"),
},
ssl_mode: SslMode::Disable,
ssl_root_cert: None,
sqlx: Default::default(), // requires "sqlx" feature
};
assert_eq!(
ipv6_config.to_url_string(),
"postgres://user@[::1]:5432/mydb?sslmode=disable"
);Sourcepub fn to_url_string(&self) -> String
pub fn to_url_string(&self) -> String
Convert to PG connection URL string
Sourcepub fn to_pg_env(&self) -> BTreeMap<EnvVariableName<'static>, String>
pub fn to_pg_env(&self) -> BTreeMap<EnvVariableName<'static>, String>
Convert to PG environment variable names
let config = Config {
endpoint: Endpoint::Network {
host: "some-host".parse().unwrap(),
channel_binding: None,
host_addr: None,
port: Some(Port::new(5432)),
},
session: Session {
application_name: None,
database: "some-database".parse().unwrap(),
password: None,
user: "some-user".parse().unwrap(),
},
ssl_mode: SslMode::VerifyFull,
ssl_root_cert: None,
sqlx: Default::default(), // requires "sqlx" feature
};
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-user".to_string()),
]);
assert_eq!(expected, config.to_pg_env());
let config_with_optionals = Config {
endpoint: Endpoint::Network {
host: "some-host".parse().unwrap(),
channel_binding: None,
host_addr: Some("127.0.0.1".parse().unwrap()),
port: Some(Port::new(5432)),
},
session: Session {
application_name: Some("some-app".parse().unwrap()),
password: Some("some-password".parse().unwrap()),
..config.session.clone()
},
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-user".to_string()),
]);
assert_eq!(expected, config_with_optionals.to_pg_env());pub fn endpoint(self, endpoint: Endpoint) -> Self
Sourcepub fn from_str_url(url: &str) -> Result<Self, ParseError>
pub fn from_str_url(url: &str) -> Result<Self, ParseError>
Parse a PostgreSQL connection URL string into a Config.
When the URL does not specify sslmode, it defaults to verify-full
to ensure secure connections by default.
See url::parse for full documentation.
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 UnsafeUnpin 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