1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::{io};
use std::io::{BufRead, Cursor};
use crate::entry_point::command_line_args::{CommandLineArgument};
use file_ext::FileExt;
use crate::symbol::SYMBOL;

pub fn read_config_file(
    cursor: Cursor<&[u8]>,
    mut prefix: String) -> Result<bool, String> {

    let mut argument_list : Vec<String> = vec![];
    let lines = cursor.lines().into_iter();
    for boxed_line in lines {
        let line = boxed_line.unwrap();
        let without_comment = strip_comment(line);
        let without_whitespaces = strip_whitespaces(without_comment.to_string());
        let is_table = without_whitespaces.starts_with(SYMBOL.opening_square_bracket);
        if is_table {
            prefix = without_whitespaces
                .replace(SYMBOL.opening_square_bracket, SYMBOL.empty_string)
                .replace(SYMBOL.closing_square_bracket, SYMBOL.empty_string)
                .to_string();
        }

        let boxed_split = without_whitespaces.split_once(SYMBOL.equals);
        if boxed_split.is_none() { // empty line as an example
            continue;
        }

        let arg: String;
        let (unparsed_key, unparsed_value) = boxed_split.unwrap();
        let value = unparsed_value
            .replace(SYMBOL.single_quote, SYMBOL.empty_string)
            .replace(SYMBOL.quotation_mark, SYMBOL.empty_string)
            .replace(SYMBOL.closing_square_bracket, SYMBOL.empty_string)
            .replace(SYMBOL.opening_square_bracket, SYMBOL.empty_string);
        let key = unparsed_key
            .replace(SYMBOL.underscore, SYMBOL.hyphen);


        if prefix.chars().count() == 0 {
            arg = [
                SYMBOL.hyphen,
                SYMBOL.hyphen,
                &key,
                SYMBOL.equals,
                &value
            ].join("");
        } else {
            arg = [
                SYMBOL.hyphen,
                SYMBOL.hyphen,
                &prefix.to_string(),
                SYMBOL.hyphen,
                &key,
                SYMBOL.equals,
                &value].join("");
        }

        argument_list.push(arg);
    }
    let params = CommandLineArgument::get_command_line_arg_list();
    CommandLineArgument::_parse(argument_list, params);

    Ok(true)
}

fn strip_comment(line: String) -> String {
    let boxed_split = line.split_once(SYMBOL.number_sign);
    if boxed_split.is_none() {
        return line;
    }

    let (without_comment, _) = boxed_split.unwrap();

    without_comment.trim().to_string()
}

fn strip_whitespaces(line: String) -> String {
    let without_whitespaces = line.replace(SYMBOL.whitespace, SYMBOL.empty_string);

    without_whitespaces
}

pub fn override_environment_variables_from_config(filepath: Option<&str>) {
    println!("\n  Start of Config Section");

    let path: &str;
    if filepath.is_none() {
        path = "/rws.config.toml";
    } else {
        path = filepath.unwrap();
    }

    let boxed_static_filepath = FileExt::get_static_filepath(path);
    if boxed_static_filepath.is_err() {
        eprintln!("{}", boxed_static_filepath.err().unwrap());
        return;
    }

    let static_filepath = boxed_static_filepath.unwrap();
    let boxed_content = std::fs::read_to_string(static_filepath);

    if boxed_content.is_err() {
        eprintln!("    Unable to parse rws.config.toml: {}", boxed_content.err().unwrap());
        println!("  End of Config Section");
        return;
    } else {
        let content = boxed_content.unwrap();
        let cursor = io::Cursor::new(content.as_bytes());
        let _ = read_config_file(cursor, SYMBOL.empty_string.to_string());
    }

    println!("  End of Config Section");
}