rbdc_mysql/options/
ssl_mode.rs

1use rbdc::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/// url example:   ?ssl-mode=disabled   or  ?ssl-mode=preferred   or   ?ssl-mode=required
8#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
9pub enum MySqlSslMode {
10    /// Establish an unencrypted connection.
11    /// This is the default if `ssl-mode` is not specified.
12    Disabled,
13
14    /// Establish an encrypted connection if the server supports encrypted connections, falling
15    /// back to an unencrypted connection if an encrypted connection cannot be established.
16    ///
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 Default for MySqlSslMode {
35    fn default() -> Self {
36        MySqlSslMode::Disabled
37    }
38}
39
40impl FromStr for MySqlSslMode {
41    type Err = Error;
42
43    fn from_str(s: &str) -> Result<Self, Error> {
44        Ok(match &*s.to_ascii_lowercase() {
45            "disabled" => MySqlSslMode::Disabled,
46            "preferred" => MySqlSslMode::Preferred,
47            "required" => MySqlSslMode::Required,
48            "verify_ca" => MySqlSslMode::VerifyCa,
49            "verify_identity" => MySqlSslMode::VerifyIdentity,
50
51            _ => {
52                return Err(Error::from(format!("unknown value {:?} for `ssl_mode`", s)));
53            }
54        })
55    }
56}