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
131
132
133
134
135
use anyhow::{format_err, Result};
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::{path::PathBuf, sync::Arc};
use crate::config_loader::ConfigLoader;
struct ConfigInner<T: Send + Sync> {
value: T,
raw: Value,
}
pub struct Config<T: Send + Sync> {
inner: Arc<RwLock<ConfigInner<T>>>,
}
impl<T: Send + Sync> Clone for Config<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<T> Config<T>
where
T: for<'de> Deserialize<'de> + Serialize + Send + Sync,
{
pub fn load_json_file(path: impl Into<PathBuf>) -> ConfigLoader<T> {
ConfigLoader::new().and_overlay_json(path)
}
pub fn load_yaml_file(path: impl Into<PathBuf>) -> ConfigLoader<T> {
ConfigLoader::new().and_overlay_yaml(path)
}
pub fn get_raw<V>(&self, path: &str) -> Result<V>
where
V: for<'de> Deserialize<'de>,
{
let locked = self.read_inner();
let mut returned = &locked.raw;
for part in path.split('.') {
returned = &returned[part];
}
let returned = serde_json::from_value(returned.clone())?;
Ok(returned)
}
pub fn set_raw<V>(&self, path: &str, value: V) -> Result<()>
where
V: Serialize,
{
let mut parts = path.split('.').rev();
let attr = parts
.next()
.ok_or_else(|| format_err!("Invalid attribute string"))?;
let mut patch = json!({attr: serde_json::to_value(&value)?});
for part in parts {
patch = json!({ part: patch });
}
self.merge(patch)
}
pub fn get(&self) -> MappedRwLockReadGuard<T> {
RwLockReadGuard::map(self.inner.read(), |inner| &inner.value)
}
fn merge(&self, patch: Value) -> Result<()> {
self.merge_and_keep_locked(patch).map(drop)
}
fn merge_and_keep_locked(&self, patch: Value) -> Result<RwLockWriteGuard<ConfigInner<T>>> {
let mut locked = self.write_inner();
let mut new_raw = locked.raw.clone();
json_patch::merge(&mut new_raw, &patch);
let new_value = serde_json::from_value(new_raw.clone())?;
locked.raw = new_raw;
locked.value = new_value;
Ok(locked)
}
fn read_inner(&self) -> RwLockReadGuard<ConfigInner<T>> {
self.inner.read()
}
fn write_inner(&self) -> RwLockWriteGuard<ConfigInner<T>> {
self.inner.write()
}
pub fn replace(&self, value: T) -> Result<()> {
let raw = serde_json::to_value(&value)?;
let mut locked = self.write_inner();
locked.value = value;
locked.raw = raw;
Ok(())
}
}
impl Config<Value> {
pub fn empty() -> Self {
Self::new_with(json!({})).unwrap()
}
}
impl<T> Config<T>
where
T: for<'de> Deserialize<'de> + Serialize + Send + Sync,
{
pub fn new_with(value: T) -> Result<Self> {
let raw = serde_json::to_value(&value)?;
Ok(Self {
inner: Arc::new(RwLock::new(ConfigInner { value, raw })),
})
}
}
impl<T> Config<T>
where
T: for<'de> Deserialize<'de> + Serialize + Default + Send + Sync,
{
pub fn new_with_default() -> Result<Self> {
Self::new_with(Default::default())
}
pub fn load_default() -> ConfigLoader<T> {
ConfigLoader::new().with_factory(Default::default)
}
}