1
2use std::fs::File;
3use std::io::prelude::*;
4use toml;
5
6pub static mut CONFIG: Option<&mut Conf> = None;
8
9#[derive(Debug)]
10pub struct Conf {
11 file_path: String,
12 settings: toml::Value,
13}
14
15impl Conf{
16 fn new(file_path: String, settings: toml::Value) -> Conf{
17 Conf{file_path, settings}
18 }
19
20 pub fn file_path(&self) -> String {
21 String::from(&self.file_path)
22 }
23 pub fn settings(&self) -> &toml::Value {
24 &self.settings
25 }
26}
27
28pub unsafe fn init(file_path: String) -> std::io::Result<()> {
30
31 let mut file = File::open(&file_path)?;
32 let mut contents = String::new();
33 file.read_to_string(&mut contents)?;
34 let value = contents.parse::<toml::Value>().unwrap();
35
36 let c:Conf = Conf::new(file_path.to_string(), value);
37
38 unsafe {
39 let bc = Box::new(c);
41 CONFIG = Some(Box::leak(bc));
42
43 }
49
50 Ok(())
51}
52
53pub unsafe fn get_str(key1: &str, key2: &str) -> String {
58 let cf = CONFIG.as_ref().unwrap();
59 cf.settings()[key1][key2].as_str().unwrap().to_string()
60}
61
62pub unsafe fn get_int(key1: &str, key2: &str) -> i64 {
64 let cf = CONFIG.as_ref().unwrap();
65 cf.settings()[key1][key2].as_integer().unwrap()
66}
67
68pub unsafe fn get_float(key1: &str, key2: &str) -> f64 {
70 let cf = CONFIG.as_ref().unwrap();
71 cf.settings()[key1][key2].as_float().unwrap()
72}
73
74pub unsafe fn get_bool(key1: &str, key2: &str) -> bool {
76 let cf = CONFIG.as_ref().unwrap();
77 cf.settings()[key1][key2].as_bool().unwrap()
78}