summaryrefslogtreecommitdiffstats
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
parent4573e911816ecba90565d33d219e6f04ee7e2f05 (diff)
Add more tests on files
-rw-r--r--src/config.rs63
-rw-r--r--src/file/mod.rs29
-rw-r--r--tests/file.rs22
-rw-r--r--tests/file_toml.rs71
4 files changed, 162 insertions, 23 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)
+ }
}
diff --git a/src/file/mod.rs b/src/file/mod.rs
index dbb80b6..7ab77d8 100644
--- a/src/file/mod.rs
+++ b/src/file/mod.rs
@@ -47,12 +47,12 @@ impl File<source::file::FileSourceFile> {
}
impl<T: FileSource> File<T> {
- pub fn required(&mut self, required: bool) -> &mut Self {
+ pub fn required(mut self, required: bool) -> Self {
self.required = required;
self
}
- pub fn namespace(&mut self, namespace: &str) -> &mut Self {
+ pub fn namespace(mut self, namespace: &str) -> Self {
self.namespace = Some(namespace.into());
self
}
@@ -61,16 +61,33 @@ impl<T: FileSource> File<T> {
impl<T: FileSource> Source for File<T> {
fn collect(&self) -> Result<HashMap<String, Value>> {
// Coerce the file contents to a string
- let (uri, contents) = self.source.resolve(self.format).map_err(|err| {
+ let (uri, contents) = match self.source.resolve(self.format).map_err(|err| {
ConfigError::Foreign(err)
- })?;
+ }) {
+ Ok((uri, contents)) => (uri, contents),
+
+ Err(error) => {
+ if !self.required {
+ return Ok(HashMap::new());
+ }
+
+ return Err(error);
+ }
+ };
// Parse the string using the given format
- self.format.unwrap().parse(uri.as_ref(), &contents, self.namespace.as_ref()).map_err(|cause| {
+ let result = self.format.unwrap().parse(uri.as_ref(), &contents, self.namespace.as_ref()).map_err(|cause| {
ConfigError::FileParse {
uri: uri,
cause: cause
}
- })
+ });
+
+ if result.is_err() && !self.required {
+ // Ignore fails and just go with it if its not required
+ Ok(HashMap::new())
+ } else {
+ result
+ }
}
}
diff --git a/tests/file.rs b/tests/file.rs
new file mode 100644
index 0000000..a1b7506
--- /dev/null
+++ b/tests/file.rs
@@ -0,0 +1,22 @@
+extern crate config;
+
+use config::*;
+
+#[test]
+fn test_file_not_required() {
+ let mut c = Config::default();
+ let res = c.merge(File::new("tests/NoSettings", FileFormat::Yaml).required(false));
+
+ assert!(res.is_ok());
+}
+
+#[test]
+fn test_file_required_not_found() {
+ let mut c = Config::default();
+ let res = c.merge(File::new("tests/NoSettings", FileFormat::Yaml));
+
+ assert!(res.is_err());
+ assert_eq!(res.unwrap_err().to_string(),
+ "configuration file \"tests/NoSettings\" not found"
+ .to_string());
+}
diff --git a/tests/file_toml.rs b/tests/file_toml.rs
new file mode 100644
index 0000000..a46e808
--- /dev/null
+++ b/tests/file_toml.rs
@@ -0,0 +1,71 @@
+extern crate config;
+extern crate serde;
+extern crate float_cmp;
+
+#[macro_use]
+extern crate serde_derive;
+
+use std::collections::HashMap;
+use float_cmp::ApproxEqUlps;
+use config::*;
+
+#[derive(Debug, Deserialize)]
+struct Place {
+ name: String,
+ longitude: f64,
+ latitude: f64,
+ favorite: bool,
+ telephone: Option<String>,
+ reviews: u64,
+ creator: HashMap<String, Value>,
+ rating: Option<f32>,
+}
+
+#[derive(Debug, Deserialize)]
+struct Settings {
+ debug: f64,
+ production: Option<String>,
+ place: Place,
+ #[serde(rename = "arr")]
+ elements: Vec<String>,
+}
+
+fn make() -> Config {
+ let mut c = Config::default();
+ c.merge(File::new("tests/Settings", FileFormat::Toml))
+ .unwrap();
+
+ c
+}
+
+#[test]
+fn test_file() {
+ let c = make();
+
+ // Deserialize the entire file as single struct
+ let s: Settings = c.deserialize().unwrap();
+
+ assert!(s.debug.approx_eq_ulps(&1.0, 2));
+ assert_eq!(s.production, Some("false".to_string()));
+ assert_eq!(s.place.name, "Torre di Pisa");
+ assert!(s.place.longitude.approx_eq_ulps(&43.7224985, 2));
+ assert!(s.place.latitude.approx_eq_ulps(&10.3970522, 2));
+ assert_eq!(s.place.favorite, false);
+ assert_eq!(s.place.reviews, 3866);
+ assert_eq!(s.place.rating, Some(4.5));
+ assert_eq!(s.place.telephone, None);
+ assert_eq!(s.elements.len(), 10);
+ assert_eq!(s.elements[3], "4".to_string());
+ assert_eq!(s.place.creator["name"].clone().into_str().unwrap(), "John Smith".to_string());
+}
+
+#[test]
+fn test_error_parse() {
+ let mut c = Config::default();
+ let res = c.merge(File::new("tests/Settings-invalid", FileFormat::Toml));
+
+ assert!(res.is_err());
+ assert_eq!(res.unwrap_err().to_string(),
+ "invalid number at line 2 in tests/Settings-invalid.toml"
+ .to_string());
+}