nexus_common/config/file/
loader.rs1use crate::types::DynError;
2use async_trait::async_trait;
3use serde::de::DeserializeOwned;
4use std::fmt::Debug;
5use std::path::Path;
6use tokio::fs;
7
8#[async_trait]
9pub trait ConfigLoader<T>
10where
11 T: DeserializeOwned + Send + Sync + Debug,
12{
13 fn try_from_str(value: &str) -> Result<T, DynError> {
15 let config_toml: T = toml::from_str(value)?;
16 Ok(config_toml)
17 }
18
19 async fn load(path: impl AsRef<Path> + Send) -> Result<T, DynError> {
21 let config_file_path = path.as_ref();
22
23 let s = fs::read_to_string(config_file_path)
25 .await
26 .map_err(|e| format!("!Failed to read config file {config_file_path:?}: {e}"))?;
27
28 let config = Self::try_from_str(&s)
30 .map_err(|e| format!("Failed to parse config file {config_file_path:?}: {e}"))?;
31
32 Ok(config)
33 }
34}