summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2017-06-13 18:54:39 -0700
committerRyan Leckey <ryan@launchbadge.com>2017-06-13 18:54:39 -0700
commit312f32905f518fa59e1daf3fc8224dadf584b484 (patch)
tree4dd4e14168383dd818fe6add8ed0a5f4900d85b6 /src/config.rs
parent4573e911816ecba90565d33d219e6f04ee7e2f05 (diff)
Add more tests on files
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs63
1 files changed, 46 insertions, 17 deletions
diff --git a/src/config.rs b/src/config.rs
index 730b261..0d84906 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -42,6 +42,10 @@ pub struct Config {
}
impl Config {
+ pub fn new() -> Self {
+ Config::default()
+ }
+
/// Merge in a configuration property source.
pub fn merge<T>(&mut self, source: T) -> Result<()>
where T: 'static,
@@ -104,27 +108,11 @@ impl Config {
Ok(())
}
+ /// Deserialize the entire configuration.
pub fn deserialize<'de, T: Deserialize<'de>>(&self) -> Result<T> {
T::deserialize(self.cache.clone())
}
- pub fn get<'de, T: Deserialize<'de>>(&self, key: &'de str) -> Result<T> {
- // Parse the key into a path expression
- let expr: path::Expression = key.to_lowercase().parse()?;
-
- // Traverse the cache using the path to (possibly) retrieve a value
- let value = expr.get(&self.cache).cloned();
-
- match value {
- Some(value) => {
- // Deserialize the received value into the requested type
- T::deserialize(ValueWithKey::new(value, key))
- }
-
- None => Err(ConfigError::NotFound(key.into())),
- }
- }
-
pub fn set_default<T>(&mut self, key: &str, value: T) -> Result<()>
where T: Into<Value>
{
@@ -162,4 +150,45 @@ impl Config {
self.refresh()
}
+
+ pub fn get<'de, T: Deserialize<'de>>(&self, key: &'de str) -> Result<T> {
+ // Parse the key into a path expression
+ let expr: path::Expression = key.to_lowercase().parse()?;
+
+ // Traverse the cache using the path to (possibly) retrieve a value
+ let value = expr.get(&self.cache).cloned();
+
+ match value {
+ Some(value) => {
+ // Deserialize the received value into the requested type
+ T::deserialize(ValueWithKey::new(value, key))
+ }
+
+ None => Err(ConfigError::NotFound(key.into())),
+ }
+ }
+
+ pub fn get_str(&self, key: &str) -> Result<String> {
+ self.get(key).and_then(Value::into_str)
+ }
+
+ pub fn get_int(&self, key: &str) -> Result<i64> {
+ self.get(key).and_then(Value::into_int)
+ }
+
+ pub fn get_float(&self, key: &str) -> Result<f64> {
+ self.get(key).and_then(Value::into_float)
+ }
+
+ pub fn get_bool(&self, key: &str) -> Result<bool> {
+ self.get(key).and_then(Value::into_bool)
+ }
+
+ pub fn get_table(&self, key: &str) -> Result<HashMap<String, Value>> {
+ self.get(key).and_then(Value::into_table)
+ }
+
+ pub fn get_array(&self, key: &str) -> Result<Vec<Value>> {
+ self.get(key).and_then(Value::into_array)
+ }
}