use crate::config::{DatabaseLink, DatabaseUrl};
const fn default_max_connections() -> u32 {
200
}
const fn default_pool_size() -> u32 {
15
}
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize)]
#[serde(default, deny_unknown_fields, rename_all = "snake_case")]
pub struct DatabaseConfig {
#[serde(default = "default_max_connections")]
pub max_connections: u32,
#[serde(default = "default_pool_size")]
pub pool_size: u32,
#[serde(alias = "link", alias = "uri")]
pub url: DatabaseLink,
}
impl DatabaseConfig {
pub fn new() -> Self {
Self {
max_connections: default_max_connections(),
pool_size: default_pool_size(),
url: DatabaseLink::default(),
}
}
pub const fn max_connections(&self) -> u32 {
self.max_connections
}
pub const fn pool_size(&self) -> u32 {
self.pool_size
}
pub fn url(&self) -> DatabaseUrl {
self.url.as_db_url()
}
pub fn with_max_connections(self, max_connections: u32) -> Self {
Self {
max_connections,
..self
}
}
pub fn with_pool_size(self, pool_size: u32) -> Self {
Self { pool_size, ..self }
}
pub fn with_url(self, url: DatabaseLink) -> Self {
Self { url, ..self }
}
pub fn set_max_connections(&mut self, max_connections: u32) {
self.max_connections = max_connections;
}
pub fn set_pool_size(&mut self, pool_size: u32) {
self.pool_size = pool_size;
}
pub fn set_url(&mut self, url: DatabaseLink) {
self.url = url;
}
}
impl Default for DatabaseConfig {
fn default() -> Self {
Self::new()
}
}
impl core::fmt::Debug for DatabaseConfig {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(&serde_json::to_string_pretty(self).unwrap())
}
}
impl core::fmt::Display for DatabaseConfig {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(&serde_json::to_string(self).unwrap())
}
}