deadpool_apexbase/config.rs
1use std::{convert::Infallible, path::PathBuf};
2
3use apexbase::storage::DurabilityLevel;
4
5use crate::{CreatePoolError, Manager, Pool, PoolBuilder, PoolConfig, Runtime};
6
7/// Configuration object for `deadpool-apexbase`.
8///
9/// # Example (from environment)
10///
11/// By enabling the `serde` feature you can read the configuration using the
12/// [`config`](https://crates.io/crates/config) crate as following:
13/// ```env
14/// APEXBASE__PATH=/tmp/apex_db
15/// APEXBASE__POOL__MAX_SIZE=16
16/// APEXBASE__POOL__TIMEOUTS__WAIT__SECS=5
17/// APEXBASE__POOL__TIMEOUTS__WAIT__NANOS=0
18/// ```
19/// ```rust,ignore
20/// #[derive(serde::Deserialize, serde::Serialize)]
21/// struct Config {
22/// apexbase: deadpool_apexbase::Config,
23/// }
24/// impl Config {
25/// pub fn from_env() -> Result<Self, config::ConfigError> {
26/// let mut cfg = config::Config::builder()
27/// .add_source(config::Environment::default().separator("__"))
28/// .build()?;
29/// cfg.try_deserialize()
30/// }
31/// }
32/// ```
33#[derive(Clone, Debug)]
34#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
35pub struct Config {
36 /// Path to the ApexBase database directory.
37 pub path: PathBuf,
38
39 /// Durability level for the database.
40 ///
41 /// Default: [`DurabilityLevel::Safe`]
42 #[cfg_attr(feature = "serde", serde(skip))]
43 pub durability: DurabilityLevel,
44
45 /// Whether to drop existing `.apex` files when opening.
46 ///
47 /// Default: `false`
48 #[cfg_attr(feature = "serde", serde(default))]
49 pub drop_if_exists: bool,
50
51 /// [`Pool`] configuration.
52 #[cfg_attr(feature = "serde", serde(default))]
53 pub pool: Option<PoolConfig>,
54}
55
56impl Default for Config {
57 fn default() -> Self {
58 Self {
59 path: PathBuf::from("apexbase.db"),
60 durability: DurabilityLevel::Safe,
61 drop_if_exists: false,
62 pool: None,
63 }
64 }
65}
66
67impl Config {
68 /// Create a new [`Config`] with the given `path` of the database directory.
69 #[must_use]
70 pub fn new(path: impl Into<PathBuf>) -> Self {
71 Self {
72 path: path.into(),
73 durability: DurabilityLevel::Safe,
74 drop_if_exists: false,
75 pool: None,
76 }
77 }
78
79 /// Creates a new [`Pool`] using this [`Config`].
80 ///
81 /// # Errors
82 ///
83 /// See [`CreatePoolError`] for details.
84 pub fn create_pool(&self, runtime: Runtime) -> Result<Pool, CreatePoolError> {
85 self.builder(runtime)
86 .map_err(CreatePoolError::Config)?
87 .build()
88 .map_err(CreatePoolError::Build)
89 }
90
91 /// Creates a new [`PoolBuilder`] using this [`Config`].
92 ///
93 /// # Errors
94 ///
95 /// See [`ConfigError`] for details.
96 pub fn builder(&self, runtime: Runtime) -> Result<PoolBuilder, ConfigError> {
97 let manager = Manager::from_config(self, runtime);
98 Ok(Pool::builder(manager)
99 .config(self.get_pool_config())
100 .runtime(runtime))
101 }
102
103 /// Returns [`deadpool::managed::PoolConfig`] which can be used to construct
104 /// a [`deadpool::managed::Pool`] instance.
105 #[must_use]
106 pub fn get_pool_config(&self) -> PoolConfig {
107 self.pool.unwrap_or_default()
108 }
109}
110
111/// This error is returned if there is something wrong with the ApexBase configuration.
112///
113/// This is just a type alias to [`Infallible`] at the moment as there
114/// is no validation happening at the configuration phase.
115pub type ConfigError = Infallible;