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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use url::{Url, Host, SchemeData};
#[derive(Debug)]
#[derive(PartialEq)]
#[derive(Clone)]
pub struct DbConfig{
pub platform: String,
pub username: Option<String>,
pub password: Option<String>,
pub host: Option<Host>,
pub port: Option<u16>,
pub database: String,
pub ssl: bool
}
impl DbConfig{
pub fn from_url(url: &str)->Option<Self>{
let parsed = Url::parse(url);
match parsed {
Ok(parsed) => {
let non_relative = match parsed.scheme_data{
SchemeData::NonRelative(ref x) =>{
x
},
SchemeData::Relative(ref x)=> {
panic!("Expecting a NonRelative SchemeData {}",x)
}
};
let scheme: &str = &parsed.scheme;
let https_url = format!("https:{}", non_relative);
let reparse = Url::parse(&https_url);
let reparse_relative = match reparse {
Ok(reparse) =>{
match reparse.scheme_data{
SchemeData::Relative(ref relative) =>{
relative.clone()
},
SchemeData::NonRelative(ref x)=> {
panic!("Expecting a Relative SchemeData {}",x)
},
}
},
Err(e) => {
match url{
"sqlite://:memory:" => {
return Some(DbConfig{
platform: scheme.to_owned(),
username: None,
password: None,
host: None,
port: None,
database: ":memory:".to_owned(),
ssl: false,
})
},
_ =>panic!("error parsing https url:{}",e)
}
}
};
match scheme{
"sqlite" => {
let mut complete_path = String::new();
let domain = match reparse_relative.host{
Host::Domain(ref domain) => domain.to_owned(),
_ => panic!("ip is not allowed in sqlite")
};
complete_path.push_str(&format!("/{}",domain));
for p in reparse_relative.path{
complete_path.push_str(&format!("/{}", p));
}
Some(DbConfig{
platform: scheme.to_owned(),
username: None,
password: None,
host: None,
port: None,
database: complete_path,
ssl: false,
})
},
_ =>
Some(DbConfig{
platform: scheme.to_owned(),
username: Some(reparse_relative.username.clone()),
password: reparse_relative.password.clone(),
host: Some(reparse_relative.host.clone()),
port: reparse_relative.port,
database: {
assert!(reparse_relative.path.len() == 1, "There should only be 1 path");
reparse_relative.path[0].to_owned()
},
ssl: false,
})
}
}
Err(e) => {
println!("Error parsing url: -[{}]-: {}", url, e);
None
}
}
}
pub fn get_url(&self)->String{
let mut url = String::new();
url.push_str(&self.platform.to_owned());
url.push_str("://");
if self.username.is_some(){
url.push_str(self.username.as_ref().unwrap());
}
if self.password.is_some(){
url.push_str(":");
url.push_str(self.password.as_ref().unwrap());
}
if self.host.is_some(){
url.push_str("@");
url.push_str(&self.host.as_ref().unwrap().serialize());
}
if self.port.is_some(){
url.push_str(":");
url.push_str(&format!("{}", self.port.as_ref().unwrap()));
}
url.push_str("/");
url.push_str(&self.database);
url
}
}
#[test]
fn test_config_url(){
let url = "postgres://postgres:p0stgr3s@localhost/bazaar_v6";
let config = DbConfig{
platform: "postgres".to_owned(),
username: Some("postgres".to_owned()),
password: Some("p0stgr3s".to_owned()),
host: Some(Host::Domain("localhost".to_owned())),
port: None,
ssl: false,
database: "bazaar_v6".to_owned(),
};
assert_eq!(config.get_url(), url.to_owned());
}
#[test]
fn test_config_from_url(){
let url = "postgres://postgres:p0stgr3s@localhost/bazaar_v6";
let config = DbConfig::from_url(url).unwrap();
assert_eq!(config.get_url(), url.to_owned());
}
#[test]
fn test_config_url_with_port(){
let url = "postgres://postgres:p0stgr3s@localhost:5432/bazaar_v6";
let config = DbConfig{
platform: "postgres".to_owned(),
username: Some("postgres".to_owned()),
password: Some("p0stgr3s".to_owned()),
host: Some(Host::Domain("localhost".to_owned())),
port: Some(5432),
database: "bazaar_v6".to_owned(),
ssl: false,
};
assert_eq!(config.get_url(), url.to_owned());
}
#[test]
fn test_config_sqlite_url_with_port(){
let url = "sqlite:///bazaar_v6.db";
let parsed_config = DbConfig::from_url(url).unwrap();
let expected_config = DbConfig{
platform: "sqlite".to_owned(),
username: None,
password: None,
host: None,
port: None,
database: "/bazaar_v6.db/".to_owned(),
ssl: false,
};
println!("{:?}",parsed_config);
assert_eq!(parsed_config, expected_config);
}
#[test]
fn test_config_sqlite_url_with_path(){
let url = "sqlite:///home/some/path/file.db";
let parsed_config = DbConfig::from_url(url).unwrap();
let expected_config = DbConfig{
platform: "sqlite".to_owned(),
username: None,
password: None,
host: None,
port: None,
database: "/home/some/path/file.db".to_owned(),
ssl: false,
};
println!("{:?}",parsed_config);
assert_eq!(parsed_config, expected_config);
}
#[test]
fn sqlite_in_memory(){
let url = "sqlite://:memory:";
let parsed_config = DbConfig::from_url(url).unwrap();
let expected_config = DbConfig{
platform: "sqlite".to_owned(),
username: None,
password: None,
host: None,
port: None,
database: ":memory:".to_owned(),
ssl: false,
};
println!("{:?}",parsed_config);
assert_eq!(parsed_config, expected_config);
}