Ziya/src/config.rs
2025-07-08 14:57:51 +07:00

37 lines
975 B
Rust

use crate::err_with_loc;
use crate::error::app::AppError;
use crate::error::Result;
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageRedisConfig {
pub host: String,
pub port: u16,
pub pool_size: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
pub directory: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub storage_redis: StorageRedisConfig,
pub logging: LoggingConfig,
}
pub async fn load_config(path: impl AsRef<Path>) -> Result<Config> {
let config_str = std::fs::read_to_string(path).map_err(|e| {
err_with_loc!(AppError::Config(format!(
"failed_to_read_config_file: {}",
e
)))
})?;
let config: Config = toml::from_str(&config_str)
.map_err(|e| err_with_loc!(AppError::Config(format!("failed_to_parse_config: {}", e))))?;
Ok(config)
}