Add format_uri function

This commit is contained in:
2026-06-27 15:25:52 -04:00
parent 4ecbf6da15
commit d74479851f

View File

@@ -1,5 +1,39 @@
use serde::Deserialize; 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)] #[derive(Debug, Clone, Deserialize)]
pub struct DatabaseConfig { pub struct DatabaseConfig {
#[serde(flatten)] #[serde(flatten)]
@@ -39,31 +73,7 @@ impl DatabaseConnectionConfig {
database, database,
username, username,
password, password,
} => { } => format_uri("postgres", username, password, host, port, database),
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
}
} }
} }
} }