refactor: ♻️ toml parser

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-11-29 20:31:02 +03:00
parent da01d37a93
commit a8fe214308
3 changed files with 42 additions and 64 deletions

View file

@ -13,6 +13,7 @@ static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);
pub async fn establish_connection() { pub async fn establish_connection() {
let database_config = DatabaseConfig::default(); let database_config = DatabaseConfig::default();
DB.connect::<Ws>(database_config.address).await.unwrap(); DB.connect::<Ws>(database_config.address).await.unwrap();
DB.signin(Root { DB.signin(Root {
username: &database_config.username, username: &database_config.username,

View file

@ -1,6 +1,10 @@
use std::{fs::File, io::Read};
pub mod database; 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)] #[derive(Debug)]
pub struct DatabaseConfig { pub struct DatabaseConfig {
@ -12,53 +16,15 @@ pub struct DatabaseConfig {
} }
impl Default for DatabaseConfig { impl Default for DatabaseConfig {
fn default() -> Self { fn default() -> Self {
let mut database_config_file = File::open("./configs/database_config.toml").unwrap(); let (header, mut database_configs) = naive_toml_parser(DATABASE_CONFIG_FILE_LOCATION);
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>>();
if database_configs[0] == "[database_config]" { if header == "[database_config]" {
Self { Self {
address: database_configs[1] database: database_configs.pop().unwrap(),
.split_once('=') namespace: database_configs.pop().unwrap(),
.unwrap() password: database_configs.pop().unwrap(),
.1 username: database_configs.pop().unwrap(),
.replace('"', "") address: database_configs.pop().unwrap(),
.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(),
} }
} else { } else {
panic!("Database Config File Must Include [database_config] at the First Line") panic!("Database Config File Must Include [database_config] at the First Line")
@ -73,24 +39,11 @@ pub struct ServerConfig {
impl Default for ServerConfig { impl Default for ServerConfig {
fn default() -> Self { fn default() -> Self {
let mut server_config_file = File::open("./configs/server_config.toml").unwrap(); let (header, mut server_configs) = naive_toml_parser(SERVER_CONFIG_FILE_LOCATION);
let mut server_configs = String::default();
server_config_file if header == "[server_config]" {
.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]" {
Self { Self {
address: server_configs[1] address: server_configs.pop().unwrap(),
.split_once('=')
.unwrap()
.1
.replace('"', "")
.trim()
.to_string(),
} }
} else { } else {
panic!("Server Config File Must Include [server_config] at the First Line") panic!("Server Config File Must Include [server_config] at the First Line")

24
src/utils.rs Normal file
View file

@ -0,0 +1,24 @@
use std::{fs::File, io::Read};
pub fn naive_toml_parser(file_location: &str) -> (String, Vec<String>) {
let mut toml_file = File::open(file_location).unwrap();
let mut toml_ingredients = String::default();
toml_file.read_to_string(&mut toml_ingredients).unwrap();
let mut toml_ingredients = toml_ingredients.lines().collect::<Vec<&str>>();
let header = toml_ingredients.remove(0).trim_end().to_string();
let parsed = toml_ingredients
.iter()
.map(|ingredient| {
ingredient
.split_once('=')
.unwrap()
.1
.replace('"', "")
.trim()
.to_string()
})
.collect();
(header, parsed)
}