sqlx_mysql/options/
ssl_mode.rs

1use crate::error::Error;
2use std::str::FromStr;
3
4/// Options for controlling the desired security state of the connection to the MySQL server.
5///
6/// It is used by the [`ssl_mode`](super::MySqlConnectOptions::ssl_mode) method.
7#[derive(Debug, Clone, Copy, Default)]
8pub enum MySqlSslMode {
9    /// Establish an unencrypted connection.
10    Disabled,
11
12    /// Establish an encrypted connection if the server supports encrypted connections, falling
13    /// back to an unencrypted connection if an encrypted connection cannot be established.
14    ///
15    /// This is the default if `ssl_mode` is not specified.
16    #[default]
17    Preferred,
18
19    /// Establish an encrypted connection if the server supports encrypted connections.
20    /// The connection attempt fails if an encrypted connection cannot be established.
21    Required,
22
23    /// Like `Required`, but additionally verify the server Certificate Authority (CA)
24    /// certificate against the configured CA certificates. The connection attempt fails
25    /// if no valid matching CA certificates are found.
26    VerifyCa,
27
28    /// Like `VerifyCa`, but additionally perform host name identity verification by
29    /// checking the host name the client uses for connecting to the server against the
30    /// identity in the certificate that the server sends to the client.
31    VerifyIdentity,
32}
33
34impl FromStr for MySqlSslMode {
35    type Err = Error;
36
37    fn from_str(s: &str) -> Result<Self, Error> {
38        Ok(match &*s.to_ascii_lowercase() {
39            "disabled" => MySqlSslMode::Disabled,
40            "preferred" => MySqlSslMode::Preferred,
41            "required" => MySqlSslMode::Required,
42            "verify_ca" => MySqlSslMode::VerifyCa,
43            "verify_identity" => MySqlSslMode::VerifyIdentity,
44
45            _ => {
46                return Err(Error::Configuration(
47                    format!("unknown value {s:?} for `ssl_mode`").into(),
48                ));
49            }
50        })
51    }
52}