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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::{de, section::OwnedSection, Configuration, ReloadableConfiguration, Section};
use serde::de::DeserializeOwned;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::Arc;
/// Represents [configuration](Configuration) binder for strongly-typed configurations.
pub trait Binder: Sized {
/// Creates and returns a structure reified from the configuration.
fn reify<T: DeserializeOwned>(&self) -> crate::Result<T>;
/// Creates and returns a structure reified from the configuration.
///
/// # Remarks
///
/// This function panics the reify operation fails.
fn reify_unchecked<T: DeserializeOwned>(&self) -> T {
match self.reify::<T>() {
Ok(data) => data,
Err(error) => panic!("{}", error),
}
}
/// Binds the configuration to the specified instance.
///
/// # Arguments
///
/// * `instance` - The instance to bind the configuration to
fn bind<T: DeserializeOwned>(&self, instance: &mut T) -> crate::Result;
/// Binds the configuration to the specified instance.
///
/// # Arguments
///
/// * `instance` - The instance to bind the configuration to
///
/// # Remarks
///
/// This function panics the bind operation fails.
fn bind_unchecked<T: DeserializeOwned>(&self, instance: &mut T) {
if let Err(error) = self.bind(instance) {
panic!("{}", error);
}
}
/// Binds the specified configuration section to the provided instance.
///
/// # Arguments
///
/// * `key` - The key of the configuration section to bind
/// * `instance` - The instance to bind the configuration to
fn bind_at<T: DeserializeOwned>(&self, key: impl AsRef<str>, instance: &mut T) -> crate::Result;
/// Binds the specified configuration section to the provided instance.
///
/// # Arguments
///
/// * `key` - The key of the configuration section to bind
/// * `instance` - The instance to bind the configuration to
///
/// # Remarks
///
/// This function panics the bind operation fails.
fn bind_at_unchecked<T: DeserializeOwned>(&self, key: impl AsRef<str>, instance: &mut T) {
if let Err(error) = self.bind_at(key, instance) {
panic!("{}", error);
}
}
/// Gets a typed value from the configuration.
///
/// # Arguments
///
/// * `key` - The key of the value to retrieve
fn get_value<T: FromStr>(&self, key: impl AsRef<str>) -> Result<Option<T>, T::Err>;
/// Gets an optional, typed value from the configuration.
///
/// # Arguments
///
/// * `key` - The key of the value to retrieve
fn get_value_or_default<T: FromStr + Default>(&self, key: impl AsRef<str>) -> Result<T, T::Err>;
}
macro_rules! binder {
($type:ty) => {
impl Binder for $type {
#[inline]
fn reify<T: DeserializeOwned>(&self) -> crate::Result<T> {
Ok(de::from::<T>(self.sections())?)
}
fn bind<T: DeserializeOwned>(&self, instance: &mut T) -> crate::Result {
Ok(de::bind(self.sections(), instance)?)
}
fn bind_at<T: DeserializeOwned>(&self, key: impl AsRef<str>, instance: &mut T) -> crate::Result {
let section = self.section(key.as_ref());
if section.exists() {
Ok(de::bind(section.sections(), instance)?)
} else {
Ok(())
}
}
fn get_value<T: FromStr>(&self, key: impl AsRef<str>) -> Result<Option<T>, T::Err> {
let section = self.section(key.as_ref());
let value = if section.exists() {
Some(T::from_str(section.value())?)
} else {
None
};
Ok(value)
}
fn get_value_or_default<T: FromStr + Default>(&self, key: impl AsRef<str>) -> Result<T, T::Err> {
let section = self.section(key.as_ref());
let value = if section.exists() {
T::from_str(section.value())?
} else {
T::default()
};
Ok(value)
}
}
};
}
binder!(Configuration);
binder!(Arc<Configuration>);
binder!(Rc<Configuration>);
binder!(Section<'_>);
binder!(OwnedSection);
impl Binder for ReloadableConfiguration {
#[inline]
fn reify<T: DeserializeOwned>(&self) -> crate::Result<T> {
self.reify()
}
#[inline]
fn bind<T: DeserializeOwned>(&self, instance: &mut T) -> crate::Result {
self.bind(instance)
}
#[inline]
fn bind_at<T: DeserializeOwned>(&self, key: impl AsRef<str>, instance: &mut T) -> crate::Result {
self.bind_at(key, instance)
}
#[inline]
fn get_value<T: FromStr>(&self, key: impl AsRef<str>) -> Result<Option<T>, T::Err> {
self.get_value(key)
}
#[inline]
fn get_value_or_default<T: FromStr + Default>(&self, key: impl AsRef<str>) -> Result<T, T::Err> {
self.get_value_or_default(key)
}
}