scsys_config/services/
database.rs

1/*
2    Appellation: database <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5#[doc(inline)]
6pub use self::db_uri::*;
7
8pub(crate) mod db_uri;
9
10/// The [`StandardDatabaseConfig`] type is a configuration structure for a standard database
11/// connection.
12#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
13#[cfg_attr(
14    feature = "serde",
15    derive(serde::Deserialize, serde::Serialize),
16    serde(default, rename_all = "snake_case")
17)]
18pub struct StandardDatabaseConfig {
19    /// The database connection URI.
20    pub connection: DatabaseUriSchema,
21    pub max_connections: u32,
22    pub pool_size: u32,
23}
24
25impl StandardDatabaseConfig {
26    const DEFAULT_MAX_CONNECTIONS: u32 = 10;
27    const DEFAULT_DB_POOL_SIZE: u32 = 10;
28
29    /// returns a new instance with defaults
30    pub fn new() -> Self {
31        Self::from_schema(Default::default())
32    }
33    /// returns a new instance from the given connection schema
34    pub fn from_schema(connection: DatabaseUriSchema) -> Self {
35        Self {
36            connection,
37            max_connections: StandardDatabaseConfig::DEFAULT_MAX_CONNECTIONS,
38            pool_size: StandardDatabaseConfig::DEFAULT_DB_POOL_SIZE,
39        }
40    }
41    /// attempts to create a new instance of the configuration using the provide uri
42    pub fn from_uri(uri: &str) -> crate::Result<Self> {
43        use core::str::FromStr;
44        let schema = DatabaseUriSchema::from_str(uri)?;
45        // create a new instance from the default schema
46        let inst = Self::from_schema(schema);
47        Ok(inst)
48    }
49    /// Returns the database connection URI.
50    pub const fn connection(&self) -> &DatabaseUriSchema {
51        &self.connection
52    }
53
54    /// Returns the database connection URL.
55    pub const fn pool_size(&self) -> u32 {
56        self.pool_size
57    }
58}
59
60impl Default for StandardDatabaseConfig {
61    fn default() -> Self {
62        Self::new()
63    }
64}