rust_web_server/entry_point/config_file/
mod.rs1use std::{io};
2use std::io::{BufRead, Cursor};
3use crate::entry_point::command_line_args::{CommandLineArgument};
4use file_ext::FileExt;
5use crate::symbol::SYMBOL;
6
7pub fn read_config_file(
8 cursor: Cursor<&[u8]>,
9 mut prefix: String) -> Result<bool, String> {
10
11 let mut argument_list : Vec<String> = vec![];
12 let lines = cursor.lines().into_iter();
13 for boxed_line in lines {
14 let line = boxed_line.unwrap();
15 let without_comment = strip_comment(line);
16 let without_whitespaces = strip_whitespaces(without_comment.to_string());
17 let is_table = without_whitespaces.starts_with(SYMBOL.opening_square_bracket);
18 if is_table {
19 prefix = without_whitespaces
20 .replace(SYMBOL.opening_square_bracket, SYMBOL.empty_string)
21 .replace(SYMBOL.closing_square_bracket, SYMBOL.empty_string)
22 .to_string();
23 }
24
25 let boxed_split = without_whitespaces.split_once(SYMBOL.equals);
26 if boxed_split.is_none() { continue;
28 }
29
30 let arg: String;
31 let (unparsed_key, unparsed_value) = boxed_split.unwrap();
32 let value = unparsed_value
33 .replace(SYMBOL.single_quote, SYMBOL.empty_string)
34 .replace(SYMBOL.quotation_mark, SYMBOL.empty_string)
35 .replace(SYMBOL.closing_square_bracket, SYMBOL.empty_string)
36 .replace(SYMBOL.opening_square_bracket, SYMBOL.empty_string);
37 let key = unparsed_key
38 .replace(SYMBOL.underscore, SYMBOL.hyphen);
39
40
41 if prefix.chars().count() == 0 {
42 arg = [
43 SYMBOL.hyphen,
44 SYMBOL.hyphen,
45 &key,
46 SYMBOL.equals,
47 &value
48 ].join("");
49 } else {
50 arg = [
51 SYMBOL.hyphen,
52 SYMBOL.hyphen,
53 &prefix.to_string(),
54 SYMBOL.hyphen,
55 &key,
56 SYMBOL.equals,
57 &value].join("");
58 }
59
60 argument_list.push(arg);
61 }
62 let params = CommandLineArgument::get_command_line_arg_list();
63 CommandLineArgument::_parse(argument_list, params);
64
65 Ok(true)
66}
67
68fn strip_comment(line: String) -> String {
69 let boxed_split = line.split_once(SYMBOL.number_sign);
70 if boxed_split.is_none() {
71 return line;
72 }
73
74 let (without_comment, _) = boxed_split.unwrap();
75
76 without_comment.trim().to_string()
77}
78
79fn strip_whitespaces(line: String) -> String {
80 let without_whitespaces = line.replace(SYMBOL.whitespace, SYMBOL.empty_string);
81
82 without_whitespaces
83}
84
85pub fn override_environment_variables_from_config(filepath: Option<&str>) {
86 println!("\n Start of Config Section");
87
88 let path: &str;
89 if filepath.is_none() {
90 path = "/rws.config.toml";
91 } else {
92 path = filepath.unwrap();
93 }
94
95 let boxed_static_filepath = FileExt::get_static_filepath(path);
96 if boxed_static_filepath.is_err() {
97 eprintln!("{}", boxed_static_filepath.err().unwrap());
98 return;
99 }
100
101 let static_filepath = boxed_static_filepath.unwrap();
102 let boxed_content = std::fs::read_to_string(static_filepath);
103
104 if boxed_content.is_err() {
105 eprintln!(" Unable to parse rws.config.toml: {}", boxed_content.err().unwrap());
106 println!(" End of Config Section");
107 return;
108 } else {
109 let content = boxed_content.unwrap();
110 let cursor = io::Cursor::new(content.as_bytes());
111 let _ = read_config_file(cursor, SYMBOL.empty_string.to_string());
112 }
113
114 println!(" End of Config Section");
115}