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-18 19:14:37 +08:00
|
|
|
use std::{ env, fs};
|
2024-11-11 13:45:02 +08:00
|
|
|
|
2024-11-18 19:14:37 +08:00
|
|
|
#[derive(Deserialize,Debug,Clone)]
|
2024-11-11 13:45:02 +08:00
|
|
|
pub struct Config {
|
2024-11-13 00:05:49 +08:00
|
|
|
pub info: Info, // 配置信息
|
2024-11-18 13:40:47 +08:00
|
|
|
pub sql_config: SqlConfig, // 关系型数据库配置
|
|
|
|
// pub no_sql_config:NoSqlConfig, 非关系型数据库配置
|
2024-11-11 13:45:02 +08:00
|
|
|
}
|
|
|
|
|
2024-11-18 19:14:37 +08:00
|
|
|
#[derive(Deserialize,Debug,Clone)]
|
2024-11-11 13:45:02 +08:00
|
|
|
pub struct Info {
|
2024-11-13 00:05:49 +08:00
|
|
|
pub install: bool, // 是否安装
|
|
|
|
pub non_relational: bool, // 是否非关系型
|
2024-11-11 13:45:02 +08:00
|
|
|
}
|
|
|
|
|
2024-11-18 19:14:37 +08:00
|
|
|
#[derive(Deserialize,Debug,Clone)]
|
2024-11-18 13:40:47 +08:00
|
|
|
pub struct SqlConfig {
|
2024-11-13 00:05:49 +08:00
|
|
|
pub db_type: String, // 数据库类型
|
|
|
|
pub address: String, // 地址
|
2024-11-18 19:14:37 +08:00
|
|
|
pub port: u32, // 端口
|
2024-11-13 00:05:49 +08:00
|
|
|
pub user: String, // 用户名
|
|
|
|
pub password: String, // 密码
|
|
|
|
pub db_name: String, // 数据库名称
|
2024-11-11 13:45:02 +08:00
|
|
|
}
|
|
|
|
|
2024-11-18 19:14:37 +08:00
|
|
|
#[derive(Deserialize,Debug,Clone)]
|
2024-11-18 13:40:47 +08:00
|
|
|
pub struct NoSqlConfig {
|
|
|
|
pub db_type: String, // 数据库类型
|
|
|
|
pub address: String, // 地址
|
2024-11-18 19:14:37 +08:00
|
|
|
pub port: u32, // 端口
|
2024-11-18 13:40:47 +08:00
|
|
|
pub user: String, // 用户名
|
|
|
|
pub password: String, // 密码
|
|
|
|
pub db_name: String, // 数据库名称
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-11 13:45:02 +08:00
|
|
|
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()?
|
2024-11-16 23:03:55 +08:00
|
|
|
.join("assets")
|
2024-11-12 20:25:43 +08:00
|
|
|
.join("config.toml");
|
2024-11-11 13:45:02 +08:00
|
|
|
Ok(toml::from_str(&fs::read_to_string(path)?)?)
|
|
|
|
}
|
|
|
|
}
|