arachnid_cli/config/kinds/
database.rs

1/*
2    Appellation: database <config>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::config::{DatabaseLink, DatabaseUrl};
6
7const fn default_max_connections() -> u32 {
8    200
9}
10
11const fn default_pool_size() -> u32 {
12    15
13}
14
15#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize)]
16#[serde(default, deny_unknown_fields, rename_all = "snake_case")]
17pub struct DatabaseConfig {
18    #[serde(default = "default_max_connections")]
19    pub max_connections: u32,
20    #[serde(default = "default_pool_size")]
21    pub pool_size: u32,
22    #[serde(alias = "link", alias = "uri")]
23    pub url: DatabaseLink,
24}
25
26impl DatabaseConfig {
27    pub fn new() -> Self {
28        Self {
29            max_connections: default_max_connections(),
30            pool_size: default_pool_size(),
31            url: DatabaseLink::default(),
32        }
33    }
34    /// returns the maximum number of connections
35    pub const fn max_connections(&self) -> u32 {
36        self.max_connections
37    }
38    /// returns the pool size for the database
39    pub const fn pool_size(&self) -> u32 {
40        self.pool_size
41    }
42    /// returns a reference to the database url
43    pub fn url(&self) -> DatabaseUrl {
44        self.url.as_db_url()
45    }
46    /// consumes the instance to create another with the given maximum number of connections
47    pub fn with_max_connections(self, max_connections: u32) -> Self {
48        Self {
49            max_connections,
50            ..self
51        }
52    }
53    /// consumes the instance to create another with the given pool size
54    pub fn with_pool_size(self, pool_size: u32) -> Self {
55        Self { pool_size, ..self }
56    }
57    //// consumes the instance to create another with the given url
58    pub fn with_url(self, url: DatabaseLink) -> Self {
59        Self { url, ..self }
60    }
61    /// update the configured maximum number of connections
62    pub fn set_max_connections(&mut self, max_connections: u32) {
63        self.max_connections = max_connections;
64    }
65    /// update the pool size for the database
66    pub fn set_pool_size(&mut self, pool_size: u32) {
67        self.pool_size = pool_size;
68    }
69    /// update the database url
70    pub fn set_url(&mut self, url: DatabaseLink) {
71        self.url = url;
72    }
73}
74
75impl Default for DatabaseConfig {
76    fn default() -> Self {
77        Self::new()
78    }
79}
80
81impl core::fmt::Debug for DatabaseConfig {
82    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
83        f.write_str(&serde_json::to_string_pretty(self).unwrap())
84    }
85}
86
87impl core::fmt::Display for DatabaseConfig {
88    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
89        f.write_str(&serde_json::to_string(self).unwrap())
90    }
91}