1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/// Defines a connection to a Postgres server.
pub struct DatabaseConfig {
pub host: String,
pub port: i32,
pub username: String,
pub password: String,
pub database: String,
pub ssl: bool,
pub debug: bool,
}
impl DatabaseConfig {
/// Creates a new database connection config.
/// It is setup by default to connect to localhost,
/// with the username "postgres" and a blank password.
pub fn new() -> DatabaseConfig {
DatabaseConfig {
host: "localhost".to_string(),
port: 5432,
username: "postgres".to_string(),
password: String::new(),
database: "postgres".to_string(),
ssl: false,
debug: false,
}
}
/// Define the host. Can be chained.
///
/// # Examples
///
/// ```
/// use tusk_rs::config::DatabaseConfig;
///
/// DatabaseConfig::new().username("username").password("password")
/// ```
pub fn host<T: AsRef<str>>(mut self, host: T) -> DatabaseConfig {
self.host = host.as_ref().to_string();
self
}
/// Define the username. Can be chained.
///
/// # Examples
///
/// ```
/// use tusk_rs::config::DatabaseConfig;
///
/// DatabaseConfig::new().username("username").password("password")
/// ```
pub fn username<T: AsRef<str>>(mut self, username: T) -> DatabaseConfig {
self.username = username.as_ref().to_string();
self
}
/// Define the password. Can be chained.
///
/// # Examples
///
/// ```
/// use tusk_rs::config::DatabaseConfig;
///
/// DatabaseConfig::new().username("username").password("password")
/// ```
pub fn password<T: AsRef<str>>(mut self, password: T) -> DatabaseConfig {
self.password = password.as_ref().to_string();
self
}
/// Define the database name. Can be chained.
///
/// # Examples
///
/// ```
/// use tusk_rs::config::DatabaseConfig;
///
/// DatabaseConfig::new().username("username").password("password").database("database")
/// ```
pub fn database<T: AsRef<str>>(mut self, database: T) -> DatabaseConfig {
self.database = database.as_ref().to_string();
self
}
/// Define whether SSL should be used. Can be chained.
///
/// # Examples
///
/// ```
/// use tusk_rs::config::DatabaseConfig;
///
/// DatabaseConfig::new().username("username").password("password").ssl(true)
/// ```
pub fn ssl(mut self, ssl: bool) -> DatabaseConfig {
self.ssl = ssl;
self
}
/// Define the port. Can be chained.
///
/// # Examples
///
/// ```
/// use tusk_rs::config::DatabaseConfig;
///
/// DatabaseConfig::new().username("username").password("password").port(5432)
/// ```
pub fn port(mut self, port: i32) -> DatabaseConfig {
self.port = port;
self
}
/// Define whether debug mode should be used. Can be chained.
///
/// # Examples
///
/// ```
/// use tusk_rs::config::DatabaseConfig;
///
/// DatabaseConfig::new().username("username").password("password").debug(true)
/// ```
pub fn debug(mut self, debug: bool) -> DatabaseConfig {
self.debug = debug;
self
}
}
impl Default for DatabaseConfig {
fn default() -> Self {
DatabaseConfig::new()
}
}