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