refactor: ♻️ toml parser
This commit is contained in:
parent
da01d37a93
commit
a8fe214308
3 changed files with 42 additions and 64 deletions
81
src/lib.rs
81
src/lib.rs
|
@ -1,6 +1,10 @@
|
|||
use std::{fs::File, io::Read};
|
||||
|
||||
pub mod database;
|
||||
pub mod utils;
|
||||
|
||||
use utils::naive_toml_parser;
|
||||
|
||||
const DATABASE_CONFIG_FILE_LOCATION: &str = "./configs/database_config.toml";
|
||||
const SERVER_CONFIG_FILE_LOCATION: &str = "./configs/server_config.toml";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DatabaseConfig {
|
||||
|
@ -12,53 +16,15 @@ pub struct DatabaseConfig {
|
|||
}
|
||||
impl Default for DatabaseConfig {
|
||||
fn default() -> Self {
|
||||
let mut database_config_file = File::open("./configs/database_config.toml").unwrap();
|
||||
let mut database_configs = String::default();
|
||||
database_config_file
|
||||
.read_to_string(&mut database_configs)
|
||||
.unwrap();
|
||||
let database_configs = database_configs
|
||||
.lines()
|
||||
.map(|line| line.trim_end())
|
||||
.collect::<Vec<&str>>();
|
||||
let (header, mut database_configs) = naive_toml_parser(DATABASE_CONFIG_FILE_LOCATION);
|
||||
|
||||
if database_configs[0] == "[database_config]" {
|
||||
if header == "[database_config]" {
|
||||
Self {
|
||||
address: database_configs[1]
|
||||
.split_once('=')
|
||||
.unwrap()
|
||||
.1
|
||||
.replace('"', "")
|
||||
.trim()
|
||||
.to_string(),
|
||||
username: database_configs[2]
|
||||
.split_once('=')
|
||||
.unwrap()
|
||||
.1
|
||||
.replace('"', "")
|
||||
.trim()
|
||||
.to_string(),
|
||||
password: database_configs[3]
|
||||
.split_once('=')
|
||||
.unwrap()
|
||||
.1
|
||||
.replace('"', "")
|
||||
.trim()
|
||||
.to_string(),
|
||||
namespace: database_configs[4]
|
||||
.split_once('=')
|
||||
.unwrap()
|
||||
.1
|
||||
.replace('"', "")
|
||||
.trim()
|
||||
.to_string(),
|
||||
database: database_configs[5]
|
||||
.split_once('=')
|
||||
.unwrap()
|
||||
.1
|
||||
.replace('"', "")
|
||||
.trim()
|
||||
.to_string(),
|
||||
database: database_configs.pop().unwrap(),
|
||||
namespace: database_configs.pop().unwrap(),
|
||||
password: database_configs.pop().unwrap(),
|
||||
username: database_configs.pop().unwrap(),
|
||||
address: database_configs.pop().unwrap(),
|
||||
}
|
||||
} else {
|
||||
panic!("Database Config File Must Include [database_config] at the First Line")
|
||||
|
@ -73,24 +39,11 @@ pub struct ServerConfig {
|
|||
|
||||
impl Default for ServerConfig {
|
||||
fn default() -> Self {
|
||||
let mut server_config_file = File::open("./configs/server_config.toml").unwrap();
|
||||
let mut server_configs = String::default();
|
||||
server_config_file
|
||||
.read_to_string(&mut server_configs)
|
||||
.unwrap();
|
||||
let server_configs = server_configs
|
||||
.lines()
|
||||
.map(|line| line.trim_end())
|
||||
.collect::<Vec<&str>>();
|
||||
if server_configs[0] == "[server_config]" {
|
||||
let (header, mut server_configs) = naive_toml_parser(SERVER_CONFIG_FILE_LOCATION);
|
||||
|
||||
if header == "[server_config]" {
|
||||
Self {
|
||||
address: server_configs[1]
|
||||
.split_once('=')
|
||||
.unwrap()
|
||||
.1
|
||||
.replace('"', "")
|
||||
.trim()
|
||||
.to_string(),
|
||||
address: server_configs.pop().unwrap(),
|
||||
}
|
||||
} else {
|
||||
panic!("Server Config File Must Include [server_config] at the First Line")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue