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
use toml::Value;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
const DEFAULT_KEYWORD: &str = ".default.";
#[derive(Clone, Debug)]
pub struct Config {
project: String,
coll_name: String,
config_file_path: String,
primary_key: String,
data_path: String,
}
impl Config {
pub fn new_from_path(path: &Path) -> Config {
let config_file_path = String::from(path.to_str().unwrap());
let mut file = File::open(path).expect("Couldn't find the configuration file!");
let mut file_content = String::new();
file.read_to_string(&mut file_content).unwrap();
let parsed: Value = file_content.parse().unwrap();
let project = match parsed["project"].as_str().unwrap() {
DEFAULT_KEYWORD => match option_env!("CARGO_PKG_NAME") {
Some(x) => x,
None => panic!("Error finding project name!"),
},
x => x,
}
.to_string();
let coll_name = match parsed["core"]["collection"].as_str().unwrap() {
DEFAULT_KEYWORD => "main",
x => x,
}
.to_string();
let primary_key = match parsed["core"]["primary_key"].as_str().unwrap() {
DEFAULT_KEYWORD => "k_hash",
x => x,
}
.to_string();
let data_path = match parsed["core"]["data_path"].as_str().unwrap() {
DEFAULT_KEYWORD => format!(
"{}/data",
Path::new(&config_file_path)
.parent()
.unwrap()
.to_str()
.unwrap()
),
x => x.to_string(),
};
Config {
project,
coll_name,
config_file_path,
primary_key,
data_path,
}
}
pub fn coll_prefix(&self) -> String {
format!("{:x}", md5::compute(self.coll_name.clone()))
.get(0..6)
.unwrap()
.to_string()
}
pub fn data_path(&self) -> &str {
&self.data_path
}
pub fn coll_name(&self) -> &str {
&self.coll_name
}
}