2024-11-25 03:36:24 +08:00
|
|
|
use crate::error::CustomResult;
|
2024-11-22 13:13:04 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::{env, fs};
|
2024-11-11 13:45:02 +08:00
|
|
|
|
2024-11-22 13:13:04 +08:00
|
|
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
2024-11-11 13:45:02 +08:00
|
|
|
pub struct Config {
|
2024-11-19 00:20:31 +08:00
|
|
|
pub info: Info,
|
|
|
|
pub sql_config: SqlConfig,
|
2024-11-11 13:45:02 +08:00
|
|
|
}
|
|
|
|
|
2024-11-22 13:13:04 +08:00
|
|
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
2024-11-11 13:45:02 +08:00
|
|
|
pub struct Info {
|
2024-11-19 00:20:31 +08:00
|
|
|
pub install: bool,
|
|
|
|
pub non_relational: bool,
|
2024-11-11 13:45:02 +08:00
|
|
|
}
|
|
|
|
|
2024-11-22 13:13:04 +08:00
|
|
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
2024-11-18 13:40:47 +08:00
|
|
|
pub struct SqlConfig {
|
2024-11-19 00:20:31 +08:00
|
|
|
pub db_type: String,
|
|
|
|
pub address: String,
|
|
|
|
pub port: u32,
|
|
|
|
pub user: String,
|
|
|
|
pub password: String,
|
|
|
|
pub db_name: String,
|
2024-11-11 13:45:02 +08:00
|
|
|
}
|
|
|
|
|
2024-11-22 13:13:04 +08:00
|
|
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
2024-11-18 13:40:47 +08:00
|
|
|
pub struct NoSqlConfig {
|
2024-11-19 00:20:31 +08:00
|
|
|
pub db_type: String,
|
|
|
|
pub address: String,
|
|
|
|
pub port: u32,
|
|
|
|
pub user: String,
|
|
|
|
pub password: String,
|
|
|
|
pub db_name: String,
|
2024-11-18 13:40:47 +08:00
|
|
|
}
|
|
|
|
|
2024-11-11 13:45:02 +08:00
|
|
|
impl Config {
|
2024-11-21 19:07:42 +08:00
|
|
|
pub fn read() -> CustomResult<Self> {
|
2024-11-22 13:13:04 +08:00
|
|
|
let path = Self::get_path()?;
|
2024-11-11 13:45:02 +08:00
|
|
|
Ok(toml::from_str(&fs::read_to_string(path)?)?)
|
|
|
|
}
|
2024-11-22 13:13:04 +08:00
|
|
|
pub fn write(config: Config) -> CustomResult<()> {
|
|
|
|
let path = Self::get_path()?;
|
2024-11-20 19:53:18 +08:00
|
|
|
fs::write(path, toml::to_string(&config)?)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-21 19:07:42 +08:00
|
|
|
pub fn get_path() -> CustomResult<PathBuf> {
|
2024-11-25 03:36:24 +08:00
|
|
|
Ok(env::current_dir()?.join("config.toml"))
|
2024-11-20 19:53:18 +08:00
|
|
|
}
|
2024-11-11 13:45:02 +08:00
|
|
|
}
|