Share address and TLS types across backend configurations

This commit is contained in:
2026-11-30 18:39:52 +08:00
parent 69513d9ffb
commit 8707eb87b6
6 changed files with 119 additions and 56 deletions
+8 -41
View File
@@ -1,46 +1,13 @@
use std::{fmt, path::Path};
use std::path::Path;
use serde::{Deserialize, Serialize};
use url::Url;
use crate::{ConfigError, driver::SqlDriver};
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Tls {
#[default]
VerifyFull,
Disabled,
}
/// A database address. Debug output includes the full value, which may contain credentials.
#[derive(Clone, Deserialize, Serialize)]
#[serde(transparent)]
pub struct Address(String);
impl From<&str> for Address {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl From<String> for Address {
fn from(value: String) -> Self {
Self(value)
}
}
impl fmt::Debug for Address {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, formatter)
}
}
impl Address {
pub fn connection_string(&self) -> &str {
&self.0
}
}
use crate::{
ConfigError,
driver::SqlDriver,
transport::{Address, Tls},
};
/// A SQL target with a primary connection and optional read replicas.
#[derive(Clone, Debug, Deserialize, Serialize)]
@@ -153,7 +120,7 @@ fn resolve_address(
Tls::Disabled => tls_disabled,
};
url.query_pairs_mut().append_pair(tls_key, tls_mode);
Ok(Address(url.into()))
Ok(Address::from(String::from(url)))
}
fn sqlite_url(path: &str, read_only: bool) -> Result<Address, ConfigError> {
@@ -167,5 +134,5 @@ fn sqlite_url(path: &str, read_only: bool) -> Result<Address, ConfigError> {
.strip_prefix("file:")
.ok_or(ConfigError::SqlitePath)?;
let mode = if read_only { "ro" } else { "rwc" };
Ok(Address(format!("sqlite:{file}?mode={mode}")))
Ok(Address::from(format!("sqlite:{file}?mode={mode}")))
}