Debian Codegraph @

src/any/options.rs

raw symbols SCIP
1 use crate::any::AnyConnection;
2 use crate::connection::{ConnectOptions, LogSettings};
3 use crate::error::Error;
4 use futures_core::future::BoxFuture;
5 use log::LevelFilter;
6 use std::str::FromStr;
7 use std::time::Duration;
8 use url::Url;
9
10 /// Opaque options for connecting to a database. These may only be constructed by parsing from
11 /// a connection url.
12 ///
13 /// ```text
14 /// postgres://postgres:password@localhost/database
15 /// mysql://root:password@localhost/database
16 /// ```
17 #[derive(Debug, Clone)]
18 #[non_exhaustive]
19 pub struct AnyConnectOptions {
20 pub database_url: Url,
21 pub log_settings: LogSettings,
22 }
23 impl FromStr for AnyConnectOptions {
24 type Err = Error;
25
26 fn from_str(url: &str) -> Result<Self, Self::Err> {
27 Ok(AnyConnectOptions {
28 database_url: url
29 .parse::<Url>()
30 .map_err(|e| Error::Configuration(e.into()))?,
31 log_settings: LogSettings::default(),
32 })
33 }
34 }
35
36 impl ConnectOptions for AnyConnectOptions {
37 type Connection = AnyConnection;
38
39 fn from_url(url: &Url) -> Result<Self, Error> {
40 Ok(AnyConnectOptions {
41 database_url: url.clone(),
42 log_settings: LogSettings::default(),
43 })
44 }
45
46 fn to_url_lossy(&self) -> Url {
47 self.database_url.clone()
48 }
49
50 #[inline]
51 fn connect(&self) -> BoxFuture<'_, Result<AnyConnection, Error>> {
52 AnyConnection::connect(self)
53 }
54
55 fn log_statements(mut self, level: LevelFilter) -> Self {
56 self.log_settings.statements_level = level;
57 self
58 }
59
60 fn log_slow_statements(mut self, level: LevelFilter, duration: Duration) -> Self {
61 self.log_settings.slow_statements_level = level;
62 self.log_settings.slow_statements_duration = duration;
63 self
64 }
65 }