arachnid_cli/config/kinds/
database.rs1use 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 pub const fn max_connections(&self) -> u32 {
36 self.max_connections
37 }
38 pub const fn pool_size(&self) -> u32 {
40 self.pool_size
41 }
42 pub fn url(&self) -> DatabaseUrl {
44 self.url.as_db_url()
45 }
46 pub fn with_max_connections(self, max_connections: u32) -> Self {
48 Self {
49 max_connections,
50 ..self
51 }
52 }
53 pub fn with_pool_size(self, pool_size: u32) -> Self {
55 Self { pool_size, ..self }
56 }
57 pub fn with_url(self, url: DatabaseLink) -> Self {
59 Self { url, ..self }
60 }
61 pub fn set_max_connections(&mut self, max_connections: u32) {
63 self.max_connections = max_connections;
64 }
65 pub fn set_pool_size(&mut self, pool_size: u32) {
67 self.pool_size = pool_size;
68 }
69 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}