use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ConnectionConfig { pub id: String, pub name: String, pub host: String, pub port: u16, pub user: String, pub password: String, pub database: String, pub ssl_mode: Option, pub color: Option, pub environment: Option, } impl ConnectionConfig { pub fn connection_url(&self) -> String { self.connection_url_for_db(&self.database) } pub fn connection_url_for_db(&self, database: &str) -> String { let ssl = self.ssl_mode.as_deref().unwrap_or("prefer"); format!( "postgres://{}:{}@{}:{}/{}?sslmode={}", urlencoded(&self.user), urlencoded(&self.password), self.host, self.port, urlencoded(database), ssl ) } } fn urlencoded(s: &str) -> String { s.chars() .map(|c| match c { ':' | '/' | '?' | '#' | '[' | ']' | '@' | '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | ';' | '=' | '%' | ' ' => { format!("%{:02X}", c as u8) } _ => c.to_string(), }) .collect() }