-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.rs
130 lines (118 loc) · 3.49 KB
/
config.rs
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
extern crate elektra;
use elektra::{CopyOption, KeySet, LookupOption, ReadableKey, StringKey, WriteableKey, KDB};
use std::collections::HashMap;
use std::str::FromStr;
pub struct Config<'a> {
kdb: KDB,
parent_key: StringKey<'a>,
ks: KeySet,
}
unsafe impl<'a> Send for Config<'a> {}
impl Config<'_> {
pub fn new(parent: &str) -> Self {
let mut s = Self {
kdb: KDB::open(KeySet::with_capacity(0)).unwrap(),
parent_key: StringKey::new(parent).unwrap(),
ks: KeySet::with_capacity(100),
};
s.sync();
s
}
pub fn sync(&mut self) {
let res = self.kdb.get(&mut self.ks, &mut self.parent_key);
match res {
Ok(_success) => (),
Err(kdb_error) => {
panic!("Set config failed: {}", kdb_error.to_error_message());
}
}
}
#[cfg(test)]
pub fn cut(&mut self, name: &str) {
let cut_key =
StringKey::new(&format!("user:/{}/{}", self.parent_key.name(), name)).unwrap();
self.ks.cut(&cut_key);
}
pub fn add(&mut self, name: &str, value: &str) {
let mut new_key =
StringKey::new(&format!("user:/{}/{}", self.parent_key.name(), name)).unwrap();
new_key.set_value(value);
self.ks.append_key(new_key);
}
pub fn set(&mut self, name: &str, value: &str) {
self.add(name, value);
let res = self.kdb.set(&mut self.ks, &mut self.parent_key);
match res {
Ok(_success) => (),
Err(kdb_error) => {
panic!("Set config failed: {}", kdb_error.to_error_message());
}
}
}
pub fn get_hash_map_vec_u8(&mut self, name: &str) -> HashMap<Vec<u8>, String> {
let mut lookup_key = self.parent_key.duplicate(CopyOption::KEY_CP_NAME);
lookup_key.add_name(name).unwrap_or_else(|_| {
panic!("Could not add '{}' to '{}'!", name, self.parent_key.name())
});
let mut ret = HashMap::new();
for (_i, key) in self.ks.iter_mut().enumerate() {
if key.is_directly_below(&lookup_key) {
ret.insert(
key.value()
.replace(&['(', ')', ',', '[', ']'][..], "")
.split_whitespace()
.map(|s| s.parse().unwrap())
.collect(),
key.basename().to_string(),
);
}
}
ret
}
pub fn get_bool(&mut self, name: &str) -> bool {
let mut lookup_key = self.parent_key.duplicate(CopyOption::KEY_CP_NAME);
lookup_key.add_name(name).unwrap_or_else(|_| {
panic!("Could not add '{}' to '{}'!", name, self.parent_key.name())
});
if let Some(found_key) = self.ks.lookup(lookup_key, LookupOption::KDB_O_NONE) {
return found_key.value() == "1";
}
false
}
pub fn get_option<T: FromStr>(&mut self, name: &str) -> Option<T> {
let mut lookup_key = self.parent_key.duplicate(CopyOption::KEY_CP_NAME);
lookup_key.add_name(name).unwrap_or_else(|_| {
panic!("Could not add '{}' to '{}'!", name, self.parent_key.name())
});
if let Some(found_key) = self.ks.lookup(lookup_key, LookupOption::KDB_O_NONE) {
if let Ok(ret) = found_key.value().parse::<T>() {
return Some(ret);
}
}
None
}
pub fn get<T: FromStr>(&mut self, name: &str) -> T {
let mut lookup_key = self.parent_key.duplicate(CopyOption::KEY_CP_NAME);
lookup_key.add_name(name).unwrap_or_else(|_| {
panic!("Could not add '{}' to '{}'!", name, self.parent_key.name())
});
if let Some(found_key) = self.ks.lookup(lookup_key, LookupOption::KDB_O_NONE) {
if let Ok(ret) = found_key.value().parse::<T>() {
ret
} else {
panic!(
"Could not convert '{}' to type '{}' from key '{}'!",
found_key.value(),
std::any::type_name::<T>(),
found_key.name()
);
}
} else {
panic!(
"Did not find the key '{}/{}'!",
self.parent_key.name(),
name
);
}
}
}