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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use crate::utils::file_utils;
use toml::Table;

pub struct Config {
    config_table: Table,
    config_content: String,
}

impl Config {
    pub async fn load(file_path: &str) -> Self {
        let result = file_utils::read_file_as_string(file_path).await;
        let content = result.expect("读取配置文件 dxmesh.toml 失败!");
        let table: Table = toml::from_str(&content).expect("解析配置文件失败!");
        Config {
            config_table: table,
            config_content: content,
        }
    }

    pub fn load_from_string(content: &str) -> Self {
        let table: Table = toml::from_str(&content).expect("解析配置文件失败!");
        Config {
            config_table: table,
            config_content: String::default(),
        }
    }

    pub fn get_config_content(&self) -> &str {
        &self.config_content.as_str()
    }

    #[allow(dead_code)]
    pub fn has_key(&self, key: &str) -> bool {
        self.config_table.contains_key(key)
    }

    pub fn get_table(&self, table_name: &str) -> Option<&Table> {
        let result = self.config_table.get(table_name);

        if result.is_none() {
            return None;
        }

        return result.unwrap().as_table();
    }

    pub fn get_str(&self, table_name: &str, key: &str) -> Option<&str> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return None;
        }

        let table = table.unwrap();

        let value = table.get(key);

        if value.is_none() {
            return None;
        }
        value.unwrap().as_str()
    }

    pub fn get_bool(&self, table_name: &str, key: &str) -> Option<bool> {

        let table = if table_name.is_empty() {
            &self.config_table
        } else {
            let table = self.get_table(table_name);

            if table.is_none() {
                return None;
            }

            table.unwrap()
        };

        let value = table.get(key);

        if value.is_none() {
            return None;
        }
        value.unwrap().as_bool()
    }

    #[allow(dead_code)]
    pub fn get_str_array(&self, table_name: &str, key: &str) -> Vec<&str> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return vec![];
        }

        let result = table.unwrap().get(key);

        if result.is_none() {
            return vec![];
        }

        let result = result.unwrap();

        if result.is_array() {
            let mut vec: Vec<&str> = vec![];
            let arr = result.as_array().unwrap();
            for ele in arr {
                vec.push(ele.as_str().unwrap())
            }
            return vec;
        }
        vec![result.as_str().unwrap()]
    }

    pub fn get_i32(&self, table_name: &str, key: &str) -> Option<i32> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return None;
        }

        let result = table.unwrap().get(key);
        match result {
            Some(value) => {
                if value.is_integer() {
                    Some(value.as_integer().unwrap() as i32)
                } else {
                    let str_val = value.as_str().unwrap();
                    let i32_val = str_val.parse::<i32>();

                    return match i32_val {
                        Ok(val) => Some(val),
                        Err(_) => None,
                    };
                }
            }
            None => None,
        }
    }

    pub fn get_i64(&self, table_name: &str, key: &str) -> Option<i64> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return None;
        }

        let result = table.unwrap().get(key);
        match result {
            Some(value) => {
                if value.is_integer() {
                    Some(value.as_integer().unwrap())
                } else {
                    let str_val = value.as_str().unwrap();
                    let i64_val = str_val.parse::<i64>();
                    return match i64_val {
                        Ok(val) => Some(val),
                        Err(_) => None,
                    };
                }
            }
            None => None,
        }
    }

    pub fn get_f32(&self, table_name: &str, key: &str) -> Option<f32> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return None;
        }

        let result = table.unwrap().get(key);
        match result {
            Some(value) => {
                if value.is_integer() {
                    Some(value.as_integer().unwrap() as f32)
                } else {
                    let str_val = value.as_str().unwrap();
                    let f32_val = str_val.parse::<f32>();
                    return match f32_val {
                        Ok(val) => Some(val),
                        Err(_) => None,
                    };
                }
            }
            None => None,
        }
    }

    pub fn get_f64(&self, table_name: &str, key: &str) -> Option<f64> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return None;
        }

        let result = table.unwrap().get(key);
        match result {
            Some(value) => {
                if value.is_integer() {
                    Some(value.as_integer().unwrap() as f64)
                } else {
                    let str_val = value.as_str().unwrap();
                    let f64_val = str_val.parse::<f64>();
                    return match f64_val {
                        Ok(val) => Some(val),
                        Err(_) => None,
                    };
                }
            }
            None => None,
        }
    }
}