1. toml依赖
toml = "0.8"
2. 代码
由于项目还未完成,部分配置(如数据库等)还未增加,后续更新增加
use log::info;
use serde::Deserialize;
use std::{
fs, sync::LazyLock
};
use crate::init::constant::*;
//创建全局静态配置文件
static CONFIG: LazyLock<Config> = LazyLock::new(init_config);
//懒加载初始化,约定优于配置
fn init_config() -> Config {
let contents =
fs::read_to_string(CONFIG_FILE).expect("Something went wrong reading the config file");
let mut config_input: Config =
toml::from_str(&contents).expect(&format!("load {} file failed", CONFIG_FILE));
config_input.server = match config_input.server {
None => Some(Server::default()),
mut ser => {
ser.as_mut().map(|server| {
server.ip = server.ip.clone().or_else(|| Server::default().ip);
server.port = server.port.or_else(|| Server::default().port);
server.name = server.name.clone().or_else(|| Server::default().name);
});
ser
}
};
config_input.nacos = match config_input.nacos {
None => Some(Nacos::default()),
mut nac => {
nac.as_mut().map(|nacos| {
nacos.ip = nacos.ip.clone().or_else(|| Nacos::default().ip);
nacos.port = nacos.port.or_else(|| Nacos::default().port);
nacos.group_name = nacos
.group_name
.clone()
.or_else(|| Nacos::default().group_name);
});
nac
}
};
info!("init config complete");
config_input
}
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct Config {
pub server: Option<Server>,
pub nacos: Option<Nacos>,
}
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct Server {
pub ip: Option<String>,
pub port: Option<u16>,
pub name: Option<String>,
}
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct Nacos {
pub ip: Option<String>,
pub port: Option<u16>,
pub group_name: Option<String>,
}
impl Default for Config {
fn default() -> Self {
Self {
server: Some(Server::default()),
nacos: Some(Nacos::default()),
}
}
}
impl Default for Server {
fn default() -> Self {
Self {
ip: Some(SERVER_DEFAULT_LISTEN.to_owned()),
port: Some(SERVER_DEFAULT_PORT),
name: Some(SERVER_DEFAULT_NAME.to_owned()),
}
}
}
impl Default for Nacos {
fn default() -> Self {
Self {
ip: Some(LOCAL_HOST.to_owned()),
port: Some(NACOS_DEFAULT_PORT),
group_name: Some(NACOS_DEFAULT_GROUP.to_owned()),
}
}
}
impl Config {
//获取全局引用
pub fn global() -> &'static Config{
&CONFIG
}
//下面的函数可以让我们链式调用获取属性值
//如 let server_ip = Config::global().server_ip();
pub fn server_ip(&self) -> String {
match &self.server {
None => Server::default().ip.unwrap(),
Some(server) => match &server.ip {
None => Server::default().ip.unwrap(),
Some(ip) => ip.to_string(),
},
}
}
pub fn server_port(&self) -> u16 {
match &self.server {
None => Server::default().port.unwrap(),
Some(server) => match server.port {
None => Server::default().port.unwrap(),
Some(port) => port,
},
}
}
pub fn server_name(&self) -> String {
match &self.server {
None => Server::default().name.unwrap(),
Some(server) => match &server.name {
None => Server::default().name.unwrap(),
Some(name) => name.to_string(),
},
}
}
pub fn nacos_ip(&self) -> String {
match &self.nacos {
None => Nacos::default().ip.unwrap(),
Some(nacos) => match &nacos.ip {
None => Nacos::default().ip.unwrap(),
Some(ip) => ip.to_string(),
},
}
}
pub fn nacos_port(&self) -> u16 {
match &self.nacos {
None => Nacos::default().port.unwrap(),
Some(nacos) => match nacos.port {
None => Nacos::default().port.unwrap(),
Some(port) => port,
},
}
}
pub fn nacos_group(&self) -> String {
match &self.nacos {
None => Nacos::default().group_name.unwrap(),
Some(nacos) => match &nacos.group_name {
None => Nacos::default().group_name.unwrap(),
Some(group_name) => group_name.to_string(),
},
}
}
}
标签:配置文件,default,ip,Some,nacos,server,port,rust
From: https://www.cnblogs.com/jiajie6591/p/18644839