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
use anyhow::Result;
use configparser::ini::Ini;
use std::io::prelude::*;
use std::{fs::OpenOptions, io::BufReader};
use crate::{CONFIG_FILE, SOURCE_DIR};
pub struct Config;
#[allow(dead_code)]
impl Config {
/// return the config path
pub fn path() -> Result<String> {
// Ok(CONFIG_FILE.as_str().to_string())
Ok(CONFIG_FILE.as_str().to_string())
}
/// create a new config if not exist
pub fn init() -> Result<()> {
// https://stackoverflow.com/questions/35636742/is-there-any-way-to-create-and-open-a-file-if-it-doesnt-exist-but-fail-otherwis
// write the config file
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(CONFIG_FILE.as_str())
.unwrap();
// read the default config file and write to the config file
let default_config = SOURCE_DIR.get_file("utils.conf").unwrap();
let default_config = default_config.contents_utf8().unwrap();
file.write_all(default_config.as_bytes()).unwrap();
Ok(())
}
/// read a fied from the config
///
/// read the config
/// #example
/// ```rust
/// let rr: Option<String>= config::Config::parse_key("port", "server")?;
/// print!("{:?}", rr);
/// ````
pub fn parse_key(key: &str, section: &str) -> Result<Option<String>> {
let raw_config_file = Self::load()?;
let mut config = Ini::new();
// You can easily load a file to get a clone of the map:
let _ = config.read(raw_config_file);
Ok(config.get(section, key))
}
/// read the confog file to string
fn load() -> Result<String> {
// read the config file
let config = OpenOptions::new()
.read(true)
.open(CONFIG_FILE.as_str())
.unwrap();
// read the config file
let mut reader = BufReader::new(config);
let mut buffer = String::new();
reader.read_to_string(&mut buffer).unwrap();
Ok(buffer)
}
}