1use std::time::Duration;
7
8use serde::Deserialize;
9
10#[cfg(feature = "db-sqlx")]
11pub mod sqlx;
12
13#[cfg(feature = "db-diesel")]
14pub mod diesel;
15
16#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
35pub struct DatabaseConfig {
36 pub host: String,
37 pub user: String,
38 pub password: String,
39 pub db_name: String,
40 pub max_connections: u32,
41}
42
43impl DatabaseConfig {
44 fn connection_timeout(&self) -> Duration {
45 if cfg!(test) {
46 Duration::from_nanos(1)
47 } else {
48 Duration::from_secs(30)
49 }
50 }
51
52 fn postgres_uri(&self) -> String {
53 format!(
54 "postgres://{}:{}@{}/{}",
55 self.user, self.password, self.host, self.db_name
56 )
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use once_cell::sync::Lazy;
63
64 use super::*;
65
66 #[test]
67 fn test_postgres_uri() {
68 assert_eq!(
69 "postgres://username:supersecurepassword@localhost/my_db",
70 CONFIG.postgres_uri(),
71 );
72 }
73
74 static CONFIG: Lazy<DatabaseConfig> = Lazy::new(|| DatabaseConfig {
75 host: "localhost".to_string(),
76 user: "username".to_string(),
77 password: "supersecurepassword".to_string(),
78 db_name: "my_db".to_string(),
79 max_connections: 30,
80 });
81}