Skip to main content

orion_infra/config/
traits.rs

1use log::warn;
2use orion_conf::error::OrionConfResult;
3
4/// 配置标准操作接口
5pub trait ConfigLifecycle {
6    /// 尝试加载配置文件(安全方法)
7    fn try_load(path: &str) -> Option<Self>
8    where
9        Self: Sized,
10    {
11        if std::path::Path::new(path).exists() {
12            match Self::load(path) {
13                Ok(conf) => Some(conf),
14                Err(e) => {
15                    warn!("load conf error: {e}");
16                    None
17                }
18            }
19        } else {
20            None
21        }
22    }
23
24    /// 强制加载配置文件
25    fn load(path: &str) -> OrionConfResult<Self>
26    where
27        Self: Sized;
28
29    /// 初始化配置文件
30    fn init(&self, path: &str) -> OrionConfResult<()>
31    where
32        Self: Sized;
33
34    /// 安全清理配置文件
35    fn safe_clean(path: &str) -> OrionConfResult<()>;
36    fn save(&self, path: &str) -> OrionConfResult<()>;
37}