summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2017-06-22 15:57:14 -0700
committerRyan Leckey <ryan@launchbadge.com>2017-06-22 15:57:14 -0700
commit159bb52c595384fed44a2c669198d50f2a758a9a (patch)
tree6553ceed3e172115fe8e682bbfc286bf8fd0f004
parenta9eecf075cf01f15ef05fea3f1edaa189446d055 (diff)
Remove `namespace` option for File
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/file/format/json.rs25
-rw-r--r--src/file/format/mod.rs9
-rw-r--r--src/file/format/toml.rs25
-rw-r--r--src/file/format/yaml.rs26
-rw-r--r--src/file/mod.rs25
6 files changed, 18 insertions, 93 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e0181c1..4463df4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## 0.6.0 – Upcoming
+ - Remove `namespace` option for File
- Add builder pattern to condense configuration
```
diff --git a/src/file/format/json.rs b/src/file/format/json.rs
index 290e17d..ed6c787 100644
--- a/src/file/format/json.rs
+++ b/src/file/format/json.rs
@@ -4,31 +4,10 @@ use std::collections::HashMap;
use std::error::Error;
use value::{Value, ValueKind};
-pub fn parse(uri: Option<&String>,
- text: &str,
- namespace: Option<&String>)
- -> Result<HashMap<String, Value>, Box<Error>> {
+pub fn parse(uri: Option<&String>, text: &str) -> Result<HashMap<String, Value>, Box<Error>> {
// Parse a JSON object value from the text
- let mut root: serde_json::Value = serde_json::from_str(text)?;
-
- // Limit to namespace
- if let Some(namespace) = namespace {
- root = serde_json::Value::Object(match root {
- serde_json::Value::Object(ref mut table) => {
- if let Some(serde_json::Value::Object(table)) =
- table.remove(namespace) {
- table
- } else {
- serde_json::Map::new()
- }
- }
-
- _ => serde_json::Map::new(),
- });
- };
-
// TODO: Have a proper error fire if the root of a file is ever not a Table
- let value = from_json_value(uri, &root);
+ let value = from_json_value(uri, &serde_json::from_str(text)?);
match value.kind {
ValueKind::Table(map) => Ok(map),
diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs
index 3fc15a6..1988513 100644
--- a/src/file/format/mod.rs
+++ b/src/file/format/mod.rs
@@ -57,18 +57,17 @@ impl FileFormat {
#[allow(unused_variables)]
pub fn parse(&self,
uri: Option<&String>,
- text: &str,
- namespace: Option<&String>)
+ text: &str)
-> Result<HashMap<String, Value>, Box<Error>> {
match *self {
#[cfg(feature = "toml")]
- FileFormat::Toml => toml::parse(uri, text, namespace),
+ FileFormat::Toml => toml::parse(uri, text),
#[cfg(feature = "json")]
- FileFormat::Json => json::parse(uri, text, namespace),
+ FileFormat::Json => json::parse(uri, text),
#[cfg(feature = "yaml")]
- FileFormat::Yaml => yaml::parse(uri, text, namespace),
+ FileFormat::Yaml => yaml::parse(uri, text),
}
}
}
diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs
index 2df1fca..6c835a4 100644
--- a/src/file/format/toml.rs
+++ b/src/file/format/toml.rs
@@ -4,31 +4,10 @@ use std::collections::{HashMap, BTreeMap};
use std::error::Error;
use value::{Value, ValueKind};
-pub fn parse(uri: Option<&String>,
- text: &str,
- namespace: Option<&String>)
- -> Result<HashMap<String, Value>, Box<Error>> {
+pub fn parse(uri: Option<&String>, text: &str) -> Result<HashMap<String, Value>, Box<Error>> {
// Parse a TOML value from the provided text
- let mut root: toml::Value = toml::from_str(text)?;
-
- // Limit to namespace
- if let Some(namespace) = namespace {
- root = toml::Value::Table(match root {
- toml::Value::Table(ref mut table) => {
- if let Some(toml::Value::Table(table)) =
- table.remove(namespace) {
- table
- } else {
- BTreeMap::new()
- }
- }
-
- _ => BTreeMap::new(),
- });
- }
-
// TODO: Have a proper error fire if the root of a file is ever not a Table
- let value = from_toml_value(uri, &root);
+ let value = from_toml_value(uri, &toml::from_str(text)?);
match value.kind {
ValueKind::Table(map) => Ok(map),
diff --git a/src/file/format/yaml.rs b/src/file/format/yaml.rs
index b1afce7..1cba0d3 100644
--- a/src/file/format/yaml.rs
+++ b/src/file/format/yaml.rs
@@ -6,14 +6,10 @@ use std::collections::{BTreeMap, HashMap};
use std::mem;
use value::{Value, ValueKind};
-pub fn parse(uri: Option<&String>,
- text: &str,
- namespace: Option<&String>)
- -> Result<HashMap<String, Value>, Box<Error>> {
+pub fn parse(uri: Option<&String>, text: &str) -> Result<HashMap<String, Value>, Box<Error>> {
+ // Parse a YAML object from file
let mut docs = yaml::YamlLoader::load_from_str(text)?;
-
- // Designate root
- let mut root = match docs.len() {
+ let root = match docs.len() {
0 => yaml::Yaml::Hash(BTreeMap::new()),
1 => mem::replace(&mut docs[0], yaml::Yaml::Null),
n => {
@@ -21,22 +17,6 @@ pub fn parse(uri: Option<&String>,
}
};
- // Limit to namespace
- if let Some(namespace) = namespace {
- root = yaml::Yaml::Hash(match root {
- yaml::Yaml::Hash(ref mut table) => {
- if let Some(yaml::Yaml::Hash(table)) =
- table.remove(&yaml::Yaml::String(namespace.clone())) {
- table
- } else {
- BTreeMap::new()
- }
- }
-
- _ => BTreeMap::new(),
- });
- };
-
// TODO: Have a proper error fire if the root of a file is ever not a Table
let value = from_yaml_value(uri, &root);
match value.kind {
diff --git a/src/file/mod.rs b/src/file/mod.rs
index 671f579..6c45cb7 100644
--- a/src/file/mod.rs
+++ b/src/file/mod.rs
@@ -16,9 +16,6 @@ pub struct File<T>
{
source: T,
- /// Namespace to restrict configuration from the file
- namespace: Option<String>,
-
/// Format of file (which dictates what driver to use).
format: Option<FileFormat>,
@@ -31,7 +28,6 @@ impl File<source::string::FileSourceString> {
File {
format: Some(format),
required: true,
- namespace: None,
source: s.into(),
}
}
@@ -42,7 +38,6 @@ impl File<source::file::FileSourceFile> {
File {
format: Some(format),
required: true,
- namespace: None,
source: source::file::FileSourceFile::new(name),
}
}
@@ -53,7 +48,6 @@ impl File<source::file::FileSourceFile> {
File {
format: None,
required: true,
- namespace: None,
source: source::file::FileSourceFile::new(name),
}
}
@@ -64,11 +58,6 @@ impl<T: FileSource> File<T> {
self.required = required;
self
}
-
- pub fn namespace(mut self, namespace: &str) -> Self {
- self.namespace = Some(namespace.into());
- self
- }
}
impl<T: FileSource> Source for File<T>
@@ -96,13 +85,11 @@ impl<T: FileSource> Source for File<T>
};
// Parse the string using the given format
- format
- .parse(uri.as_ref(), &contents, self.namespace.as_ref())
- .map_err(|cause| {
- ConfigError::FileParse {
- uri: uri,
- cause: cause,
- }
- })
+ format.parse(uri.as_ref(), &contents).map_err(|cause| {
+ ConfigError::FileParse {
+ uri: uri,
+ cause: cause,
+ }
+ })
}
}