sqlx_core_oldapi/postgres/options/
ssl_mode.rs

1use crate::error::Error;
2use std::str::FromStr;
3
4/// Options for controlling the level of protection provided for PostgreSQL SSL connections.
5///
6/// It is used by the [`ssl_mode`](super::PgConnectOptions::ssl_mode) method.
7#[derive(Debug, Clone, Copy, Default)]
8pub enum PgSslMode {
9    /// Only try a non-SSL connection.
10    Disable,
11
12    /// First try a non-SSL connection; if that fails, try an SSL connection.
13    Allow,
14
15    /// First try an SSL connection; if that fails, try a non-SSL connection.
16    #[default]
17    Prefer,
18
19    /// Only try an SSL connection. If a root CA file is present, verify the connection
20    /// in the same way as if `VerifyCa` was specified.
21    Require,
22
23    /// Only try an SSL connection, and verify that the server certificate is issued by a
24    /// trusted certificate authority (CA).
25    VerifyCa,
26
27    /// Only try an SSL connection; verify that the server certificate is issued by a trusted
28    /// CA and that the requested server host name matches that in the certificate.
29    VerifyFull,
30}
31
32impl FromStr for PgSslMode {
33    type Err = Error;
34
35    fn from_str(s: &str) -> Result<Self, Error> {
36        Ok(match &*s.to_ascii_lowercase() {
37            "disable" => PgSslMode::Disable,
38            "allow" => PgSslMode::Allow,
39            "prefer" => PgSslMode::Prefer,
40            "require" => PgSslMode::Require,
41            "verify-ca" => PgSslMode::VerifyCa,
42            "verify-full" => PgSslMode::VerifyFull,
43
44            _ => {
45                return Err(Error::Configuration(
46                    format!("unknown value {s:?} for `ssl_mode`").into(),
47                ));
48            }
49        })
50    }
51}