2024-11-11 13:45:02 +08:00
|
|
|
// config/mod.rs
|
2024-11-12 20:25:43 +08:00
|
|
|
/*
|
|
|
|
配置文件结构和操作
|
|
|
|
*/
|
|
|
|
|
2024-11-11 13:45:02 +08:00
|
|
|
use serde::Deserialize;
|
2024-11-12 20:25:43 +08:00
|
|
|
use std::{env, fs};
|
2024-11-11 13:45:02 +08:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Config {
|
|
|
|
pub info: Info,
|
2024-11-12 20:25:43 +08:00
|
|
|
pub db_config: DbConfig,
|
2024-11-11 13:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Info {
|
|
|
|
pub install: bool,
|
2024-11-12 20:25:43 +08:00
|
|
|
pub non_relational: bool,
|
2024-11-11 13:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2024-11-12 20:25:43 +08:00
|
|
|
pub struct DbConfig {
|
2024-11-11 13:45:02 +08:00
|
|
|
pub db_type: String,
|
|
|
|
pub address: String,
|
|
|
|
pub prot: u32,
|
|
|
|
pub user: String,
|
|
|
|
pub password: String,
|
|
|
|
pub db_name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
/// 读取配置文件
|
2024-11-12 20:25:43 +08:00
|
|
|
pub fn read() -> Result<Self, Box<dyn std::error::Error>> {
|
|
|
|
let path = env::current_dir()?
|
|
|
|
.join("config.toml");
|
2024-11-11 13:45:02 +08:00
|
|
|
Ok(toml::from_str(&fs::read_to_string(path)?)?)
|
|
|
|
}
|
|
|
|
}
|