br_addon/
tools.rs

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