cdbc_pg/options/ssl_mode.rs
1use cdbc::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)]
8pub enum PgSslMode {
9 /// Only try a non-SSL connection.
10 /// This is the default
11 Disable,
12
13 /// First try a non-SSL connection; if that fails, try an SSL connection.
14 Allow,
15
16 /// First try an SSL connection; if that fails, try a non-SSL connection.
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 Default for PgSslMode {
33 fn default() -> Self {
34 PgSslMode::Disable
35 }
36}
37
38impl FromStr for PgSslMode {
39 type Err = Error;
40
41 fn from_str(s: &str) -> Result<Self, Error> {
42 Ok(match &*s.to_ascii_lowercase() {
43 "disable" => PgSslMode::Disable,
44 "allow" => PgSslMode::Allow,
45 "prefer" => PgSslMode::Prefer,
46 "require" => PgSslMode::Require,
47 "verify-ca" => PgSslMode::VerifyCa,
48 "verify-full" => PgSslMode::VerifyFull,
49
50 _ => {
51 return Err(Error::Configuration(
52 format!("unknown value {:?} for `ssl_mode`", s).into(),
53 ));
54 }
55 })
56 }
57}