Skip to main content

br_addon/
tools.rs

1#[cfg(feature = "cache")]
2use br_cache::{Cache, Config as CacheConfig};
3#[cfg(any(
4    feature = "mysql",
5    feature = "mssql",
6    feature = "sqlite",
7    feature = "pgsql"
8))]
9use br_db::config::Config as DbConfig;
10#[cfg(any(
11    feature = "mysql",
12    feature = "mssql",
13    feature = "sqlite",
14    feature = "pgsql"
15))]
16use br_db::Db;
17use serde::Deserialize;
18use std::path::Path;
19
20#[derive(Clone)]
21pub struct Tools {
22    #[cfg(any(
23        feature = "mysql",
24        feature = "mssql",
25        feature = "sqlite",
26        feature = "pgsql"
27    ))]
28    pub db: Db,
29    #[cfg(feature = "cache")]
30    pub cache: Cache,
31}
32impl Tools {
33    pub fn new(config: ToolsConfig) -> Result<Self, String> {
34        Ok(Self {
35            #[cfg(any(
36                feature = "mysql",
37                feature = "mssql",
38                feature = "sqlite",
39                feature = "pgsql"
40            ))]
41            db: Db::new(config.db)?,
42            #[cfg(feature = "cache")]
43            cache: Cache::new(config.cache),
44        })
45    }
46}
47#[derive(Clone, Debug, Deserialize)]
48pub struct ToolsConfig {
49    #[cfg(any(
50        feature = "mysql",
51        feature = "mssql",
52        feature = "sqlite",
53        feature = "pgsql"
54    ))]
55    pub db: DbConfig,
56    #[cfg(feature = "cache")]
57    pub cache: CacheConfig,
58}
59impl ToolsConfig {
60    #[must_use]
61    pub fn create(config_file: &Path) -> Self {
62        Self {
63            #[cfg(any(
64                feature = "mysql",
65                feature = "mssql",
66                feature = "sqlite",
67                feature = "pgsql"
68            ))]
69            db: DbConfig::create(config_file.to_path_buf(), true),
70            #[cfg(feature = "cache")]
71            cache: CacheConfig::create(config_file.to_path_buf(), true),
72        }
73    }
74}