1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! config mod

use config::Config;
use serde::de::DeserializeOwned;

use super::{data::AppResult, server::application::AppConfig};

pub mod log;
pub mod registry;
pub mod security;
pub mod server;

/// Config 扩展
pub trait ConfigExt {
    /// 获取配置
    fn get_or_default<T: DeserializeOwned + Default>(&self, key: &str) -> T;
}

impl ConfigExt for Config {
    fn get_or_default<T: DeserializeOwned + Default>(&self, key: &str) -> T {
        match self.get(key) {
            Ok(v) => v,
            Err(_) => T::default(),
        }
    }
}

/// 可应用的配置
#[async_trait]
pub trait UpdateableConfig {
    /// 应用更新
    async fn apply(&self, application: &AppConfig) -> AppResult<()>;
}