xCommonLib/base/
config.rs

1use crate::utils::file_utils;
2use toml::Table;
3
4pub struct Config {
5    config_table: Table,
6    config_content: String,
7}
8
9impl Config {
10    pub async fn load(file_path: &str) -> Self {
11        let result = file_utils::read_file_as_string(file_path).await;
12        let content = result.expect("读取配置文件 dxmesh.toml 失败!");
13        let table: Table = toml::from_str(&content).expect("解析配置文件失败!");
14        Config {
15            config_table: table,
16            config_content: content,
17        }
18    }
19
20    pub fn load_from_string(content: &str) -> Self {
21        let table: Table = toml::from_str(&content).expect("解析配置文件失败!");
22        Config {
23            config_table: table,
24            config_content: String::default(),
25        }
26    }
27
28    pub fn get_config_content(&self) -> &str {
29        &self.config_content.as_str()
30    }
31
32    #[allow(dead_code)]
33    pub fn has_key(&self, key: &str) -> bool {
34        self.config_table.contains_key(key)
35    }
36
37    pub fn get_table(&self, table_name: &str) -> Option<&Table> {
38        let result = self.config_table.get(table_name);
39
40        if result.is_none() {
41            return None;
42        }
43
44        return result.unwrap().as_table();
45    }
46
47    pub fn get_str(&self, table_name: &str, key: &str) -> Option<&str> {
48        let table = self.get_table(table_name);
49
50        if table.is_none() {
51            return None;
52        }
53
54        let table = table.unwrap();
55
56        let value = table.get(key);
57
58        if value.is_none() {
59            return None;
60        }
61        value.unwrap().as_str()
62    }
63
64    pub fn get_bool(&self, table_name: &str, key: &str) -> Option<bool> {
65
66        let table = if table_name.is_empty() {
67            &self.config_table
68        } else {
69            let table = self.get_table(table_name);
70
71            if table.is_none() {
72                return None;
73            }
74
75            table.unwrap()
76        };
77
78        let value = table.get(key);
79
80        if value.is_none() {
81            return None;
82        }
83        value.unwrap().as_bool()
84    }
85
86    #[allow(dead_code)]
87    pub fn get_str_array(&self, table_name: &str, key: &str) -> Vec<&str> {
88        let table = self.get_table(table_name);
89
90        if table.is_none() {
91            return vec![];
92        }
93
94        let result = table.unwrap().get(key);
95
96        if result.is_none() {
97            return vec![];
98        }
99
100        let result = result.unwrap();
101
102        if result.is_array() {
103            let mut vec: Vec<&str> = vec![];
104            let arr = result.as_array().unwrap();
105            for ele in arr {
106                vec.push(ele.as_str().unwrap())
107            }
108            return vec;
109        }
110        vec![result.as_str().unwrap()]
111    }
112
113    pub fn get_i32(&self, table_name: &str, key: &str) -> Option<i32> {
114        let table = self.get_table(table_name);
115
116        if table.is_none() {
117            return None;
118        }
119
120        let result = table.unwrap().get(key);
121        match result {
122            Some(value) => {
123                if value.is_integer() {
124                    Some(value.as_integer().unwrap() as i32)
125                } else {
126                    let str_val = value.as_str().unwrap();
127                    let i32_val = str_val.parse::<i32>();
128
129                    return match i32_val {
130                        Ok(val) => Some(val),
131                        Err(_) => None,
132                    };
133                }
134            }
135            None => None,
136        }
137    }
138
139    pub fn get_i64(&self, table_name: &str, key: &str) -> Option<i64> {
140        let table = self.get_table(table_name);
141
142        if table.is_none() {
143            return None;
144        }
145
146        let result = table.unwrap().get(key);
147        match result {
148            Some(value) => {
149                if value.is_integer() {
150                    Some(value.as_integer().unwrap())
151                } else {
152                    let str_val = value.as_str().unwrap();
153                    let i64_val = str_val.parse::<i64>();
154                    return match i64_val {
155                        Ok(val) => Some(val),
156                        Err(_) => None,
157                    };
158                }
159            }
160            None => None,
161        }
162    }
163
164    pub fn get_f32(&self, table_name: &str, key: &str) -> Option<f32> {
165        let table = self.get_table(table_name);
166
167        if table.is_none() {
168            return None;
169        }
170
171        let result = table.unwrap().get(key);
172        match result {
173            Some(value) => {
174                if value.is_integer() {
175                    Some(value.as_integer().unwrap() as f32)
176                } else {
177                    let str_val = value.as_str().unwrap();
178                    let f32_val = str_val.parse::<f32>();
179                    return match f32_val {
180                        Ok(val) => Some(val),
181                        Err(_) => None,
182                    };
183                }
184            }
185            None => None,
186        }
187    }
188
189    pub fn get_f64(&self, table_name: &str, key: &str) -> Option<f64> {
190        let table = self.get_table(table_name);
191
192        if table.is_none() {
193            return None;
194        }
195
196        let result = table.unwrap().get(key);
197        match result {
198            Some(value) => {
199                if value.is_integer() {
200                    Some(value.as_integer().unwrap() as f64)
201                } else {
202                    let str_val = value.as_str().unwrap();
203                    let f64_val = str_val.parse::<f64>();
204                    return match f64_val {
205                        Ok(val) => Some(val),
206                        Err(_) => None,
207                    };
208                }
209            }
210            None => None,
211        }
212    }
213}