#[cfg(feature = "cache")]
use br_cache::{Cache, Config as CacheConfig};
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "pgsql"))]
use br_db::config::Config as DbConfig;
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "pgsql"))]
use br_db::Db;
#[cfg(feature = "kafka")]
use br_kafka::Config as KafkaConfig;
#[cfg(feature = "kafka")]
use br_kafka::Kafka;
#[cfg(feature = "email")]
use br_email::{Mail, Config as EmailConfig};
use serde::Deserialize;
use std::path::{Path};

/// 配置文件
#[derive(Clone)]
pub struct Tools {
    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "pgsql"))]
    pub db: Db,
    #[cfg(feature = "cache")]
    pub cache: Cache,
    #[cfg(feature = "kafka")]
    pub kafka: Kafka,
    #[cfg(feature = "email")]
    pub email: Mail,
}
impl Tools {
    pub fn new(config: ToolsConfig) -> Result<Self, String> {
        Ok(Self {
            #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "pgsql"))]
            db: Db::new(config.db)?,
            #[cfg(feature = "cache")]
            cache: Cache::new(config.cache),
            #[cfg(feature = "kafka")]
            kafka: Kafka::new(config.kafka)?,
            #[cfg(feature = "email")]
            email: Mail::new_new(config.email)?,
        })
    }
}
#[derive(Clone, Debug, Deserialize)]
pub struct ToolsConfig {
    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "pgsql"))]
    pub db: DbConfig,
    #[cfg(feature = "cache")]
    pub cache: CacheConfig,
    #[cfg(feature = "kafka")]
    pub kafka: KafkaConfig,
    #[cfg(feature = "email")]
    pub email: EmailConfig,
}
impl ToolsConfig {
    #[must_use]
    pub fn create(config_file: &Path) -> Self {
        Self {
            #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "pgsql"))]
            db: DbConfig::create(config_file.to_path_buf(), true),
            #[cfg(feature = "cache")]
            cache: CacheConfig::create(config_file.to_path_buf(), true),
            #[cfg(feature = "kafka")]
            kafka: KafkaConfig::create(config_file.to_path_buf(), true),
            #[cfg(feature = "email")]
            email: EmailConfig::create(config_file.to_path_buf(), true),
        }
    }
}
