Skip to main content

orion_infra/config/
toml_impl.rs

1use std::path::Path;
2
3use orion_conf::{
4    TomlIO,
5    error::{ConfIOReason, OrionConfResult},
6};
7use orion_error::StructError;
8
9use crate::config::backup_clean;
10
11use super::ConfigLifecycle;
12
13impl<T> ConfigLifecycle for T
14where
15    T: TomlIO<T> + serde::Serialize + serde::de::DeserializeOwned,
16{
17    fn load(path: &str) -> OrionConfResult<Self>
18    where
19        Self: Sized,
20    {
21        T::load_toml(Path::new(path))
22    }
23
24    fn init(&self, path: &str) -> OrionConfResult<()>
25    where
26        Self: Sized,
27    {
28        Self::safe_clean(path)?;
29        self.save(path)
30    }
31
32    fn safe_clean(path: &str) -> OrionConfResult<()> {
33        backup_clean(path).map_err(|err| StructError::from(ConfIOReason::Other(err.to_string())))
34    }
35
36    fn save(&self, path: &str) -> OrionConfResult<()> {
37        self.save_toml(Path::new(path))
38    }
39}