Add format_uri function
This commit is contained in:
@@ -1,5 +1,39 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
/// Build a connection URI from parts
|
||||
fn format_uri(
|
||||
scheme: &str,
|
||||
username: &Option<String>,
|
||||
password: &Option<String>,
|
||||
host: &str,
|
||||
port: &Option<u16>,
|
||||
path: &Option<String>,
|
||||
) -> String {
|
||||
let mut url = format!("{scheme}://");
|
||||
|
||||
if let Some(username) = username {
|
||||
url.push_str(username);
|
||||
|
||||
if let Some(password) = password {
|
||||
url.push_str(&format!(":{password}"));
|
||||
}
|
||||
|
||||
url.push('@');
|
||||
}
|
||||
|
||||
url.push_str(host);
|
||||
|
||||
if let Some(port) = port {
|
||||
url.push_str(&format!(":{port}"));
|
||||
}
|
||||
|
||||
if let Some(path) = path {
|
||||
url.push_str(&format!("/{path}"));
|
||||
}
|
||||
|
||||
url
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct DatabaseConfig {
|
||||
#[serde(flatten)]
|
||||
@@ -39,31 +73,7 @@ impl DatabaseConnectionConfig {
|
||||
database,
|
||||
username,
|
||||
password,
|
||||
} => {
|
||||
let mut url = "postgres://".to_string();
|
||||
|
||||
if let Some(username) = username {
|
||||
url.push_str(username);
|
||||
|
||||
if let Some(password) = password {
|
||||
url.push_str(&format!(":{password}"));
|
||||
}
|
||||
|
||||
url.push('@');
|
||||
}
|
||||
|
||||
url.push_str(host);
|
||||
|
||||
if let Some(port) = port {
|
||||
url.push_str(&format!(":{port}"));
|
||||
}
|
||||
|
||||
if let Some(database) = database {
|
||||
url.push_str(&format!("/{database}"));
|
||||
}
|
||||
|
||||
url
|
||||
}
|
||||
} => format_uri("postgres", username, password, host, port, database),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user