1use std::collections::HashMap;
2use std::fs;
3use std::path::Path;
4
5use serde::Deserialize;
6use serde::Serialize;
7use thiserror::Error;
8
9use lezeh_common::types::ResultAnyError;
10
11#[derive(Debug, Serialize, Deserialize, Clone)]
12pub struct Config {
13 pub db_connection_by_name: HashMap<String, DbConnectionConfig>,
14}
15
16#[derive(Debug, Serialize, Deserialize, Clone)]
18pub struct DbConnectionConfig {
19 pub host: String,
20 pub port: u32,
21 pub database: String,
22 pub username: String,
23 pub password: Option<String>,
24}
25
26impl Config {
27 pub fn from(setting_path: impl AsRef<Path> + std::fmt::Display) -> ResultAnyError<Config> {
28 let config_str = fs::read_to_string(&setting_path).map_err(|err| {
29 return ConfigError::ReadConfigError {
30 config_path: setting_path.to_string(),
31 root_err: format!("{:#?}", err),
32 };
33 })?;
34
35 let config: Config = serde_yaml::from_str(&config_str).map_err(|err| {
36 return ConfigError::ConfigDeserializeError {
37 config_path: setting_path.to_string(),
38 root_err: format!("{:#?}", err),
39 };
40 })?;
41
42 return Ok(config);
43 }
44}
45
46#[derive(Error, Debug)]
47pub enum ConfigError {
48 #[error("Failed reading config {config_path} err {root_err}")]
49 ReadConfigError {
50 config_path: String,
51 root_err: String,
52 },
53
54 #[error("Could not deserialize config please check {config_path} err {root_err}")]
55 ConfigDeserializeError {
56 config_path: String,
57 root_err: String,
58 },
59}