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;
17#[cfg(feature = "email")]
18use br_email::{Config as EmailConfig, Mail};
19use serde::Deserialize;
20use std::path::Path;
21
22#[derive(Clone)]
23pub struct Tools {
24    #[cfg(any(
25        feature = "mysql",
26        feature = "mssql",
27        feature = "sqlite",
28        feature = "pgsql"
29    ))]
30    pub db: Db,
31    #[cfg(feature = "cache")]
32    pub cache: Cache,
33    #[cfg(feature = "email")]
34    pub email: Mail,
35}
36impl Tools {
37    pub fn new(config: ToolsConfig) -> Result<Self, String> {
38        Ok(Self {
39            #[cfg(any(
40                feature = "mysql",
41                feature = "mssql",
42                feature = "sqlite",
43                feature = "pgsql"
44            ))]
45            db: Db::new(config.db)?,
46            #[cfg(feature = "cache")]
47            cache: Cache::new(config.cache),
48            #[cfg(feature = "email")]
49            email: Mail::new_new(config.email)?,
50        })
51    }
52}
53#[derive(Clone, Debug, Deserialize)]
54pub struct ToolsConfig {
55    #[cfg(any(
56        feature = "mysql",
57        feature = "mssql",
58        feature = "sqlite",
59        feature = "pgsql"
60    ))]
61    pub db: DbConfig,
62    #[cfg(feature = "cache")]
63    pub cache: CacheConfig,
64    #[cfg(feature = "email")]
65    pub email: EmailConfig,
66}
67impl ToolsConfig {
68    #[must_use]
69    pub fn create(config_file: &Path) -> Self {
70        Self {
71            #[cfg(any(
72                feature = "mysql",
73                feature = "mssql",
74                feature = "sqlite",
75                feature = "pgsql"
76            ))]
77            db: DbConfig::create(config_file.to_path_buf(), true),
78            #[cfg(feature = "cache")]
79            cache: CacheConfig::create(config_file.to_path_buf(), true),
80            #[cfg(feature = "email")]
81            email: EmailConfig::create(config_file.to_path_buf(), true),
82        }
83    }
84}