servlet 1.0.0

A small application runtime with an explicit operational contract
Documentation
//! Native options are built only after explicit ambient-policy checks.

use super::{PgConfig, PgError, PgTls};
use sqlx::{
    ConnectOptions,
    postgres::{PgConnectOptions, PgSslMode},
};

impl PgConfig {
    pub(super) fn options(&self) -> Result<PgConnectOptions, PgError> {
        // SQLx 0.9 has no environment-free options constructor. Reject the inputs
        // it consumes; do not mutate the process environment or expose their values.
        if [
            "PGPORT",
            "PGHOSTADDR",
            "PGHOST",
            "PGUSER",
            "PGDATABASE",
            "PGPASSWORD",
            "PGSSLROOTCERT",
            "PGSSLCERT",
            "PGSSLKEY",
            "PGSSLMODE",
            "PGAPPNAME",
            "PGOPTIONS",
        ]
        .iter()
        .any(|name| std::env::var_os(name).is_some())
        {
            return Err(PgError::AmbientConfiguration);
        }
        let endpoint = &self.endpoint;
        let options = PgConnectOptions::new_without_pgpass()
            .host(&endpoint.host)
            .port(endpoint.port)
            .username(&endpoint.username)
            .password(&endpoint.password)
            .database(&endpoint.database)
            .application_name(&self.application_name)
            .options([
                (
                    "statement_timeout",
                    self.session.statement_timeout.as_millis(),
                ),
                ("lock_timeout", self.session.lock_timeout.as_millis()),
                (
                    "idle_in_transaction_session_timeout",
                    self.session.idle_transaction_timeout.as_millis(),
                ),
            ])
            .disable_statement_logging();
        Ok(match &self.tls {
            PgTls::VerifyFull => options.ssl_mode(PgSslMode::VerifyFull),
            PgTls::VerifyFullWithRoot(root) => options
                .ssl_mode(PgSslMode::VerifyFull)
                .ssl_root_cert_from_pem(root.clone()),
            PgTls::PlaintextLoopback => options.ssl_mode(PgSslMode::Disable),
        })
    }
}