summaryrefslogtreecommitdiffstats
path: root/src/file/format
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2017-06-22 14:14:29 -0700
committerRyan Leckey <ryan@launchbadge.com>2017-06-22 14:30:51 -0700
commit2b438ed9b53fee5689032f3b5fcdda8d15becd5f (patch)
tree48374e66a6594c7d11d19fe38f7e23df731b52be /src/file/format
parent04d3ee8f70337e4899932b92262fbeec2dbb1bd9 (diff)
Add builder API to Config
Diffstat (limited to 'src/file/format')
-rw-r--r--src/file/format/json.rs30
-rw-r--r--src/file/format/mod.rs6
-rw-r--r--src/file/format/toml.rs26
-rw-r--r--src/file/format/yaml.rs34
4 files changed, 52 insertions, 44 deletions
diff --git a/src/file/format/json.rs b/src/file/format/json.rs
index 4a94943..290e17d 100644
--- a/src/file/format/json.rs
+++ b/src/file/format/json.rs
@@ -4,25 +4,27 @@ 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,
+ namespace: Option<&String>)
+ -> 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::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()
- }
- });
+ _ => serde_json::Map::new(),
+ });
};
// TODO: Have a proper error fire if the root of a file is ever not a Table
@@ -70,8 +72,6 @@ fn from_json_value(uri: Option<&String>, value: &serde_json::Value) -> Value {
Value::new(uri, ValueKind::Array(l))
}
- serde_json::Value::Null => {
- Value::new(uri, ValueKind::Nil)
- }
+ serde_json::Value::Null => Value::new(uri, ValueKind::Nil),
}
}
diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs
index 287b9b4..3fc15a6 100644
--- a/src/file/format/mod.rs
+++ b/src/file/format/mod.rs
@@ -55,7 +55,11 @@ impl FileFormat {
// TODO: pub(crate)
#[doc(hidden)]
#[allow(unused_variables)]
- pub fn parse(&self, uri: Option<&String>, text: &str, namespace: Option<&String>) -> Result<HashMap<String, Value>, Box<Error>> {
+ pub fn parse(&self,
+ uri: Option<&String>,
+ text: &str,
+ namespace: Option<&String>)
+ -> Result<HashMap<String, Value>, Box<Error>> {
match *self {
#[cfg(feature = "toml")]
FileFormat::Toml => toml::parse(uri, text, namespace),
diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs
index 633f39e..2df1fca 100644
--- a/src/file/format/toml.rs
+++ b/src/file/format/toml.rs
@@ -4,25 +4,27 @@ 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,
+ namespace: Option<&String>)
+ -> 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()
- }
- }
+ toml::Value::Table(ref mut table) => {
+ if let Some(toml::Value::Table(table)) =
+ table.remove(namespace) {
+ table
+ } else {
+ BTreeMap::new()
+ }
+ }
- _ => {
- BTreeMap::new()
- }
- });
+ _ => BTreeMap::new(),
+ });
}
// TODO: Have a proper error fire if the root of a file is ever not a Table
diff --git a/src/file/format/yaml.rs b/src/file/format/yaml.rs
index 7c3dd71..b1afce7 100644
--- a/src/file/format/yaml.rs
+++ b/src/file/format/yaml.rs
@@ -6,7 +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,
+ namespace: Option<&String>)
+ -> Result<HashMap<String, Value>, Box<Error>> {
let mut docs = yaml::YamlLoader::load_from_str(text)?;
// Designate root
@@ -21,18 +24,17 @@ pub fn parse(uri: Option<&String>, text: &str, namespace: Option<&String>) -> Re
// 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()
- }
- }
+ 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()
- }
- });
+ _ => BTreeMap::new(),
+ });
};
// TODO: Have a proper error fire if the root of a file is ever not a Table
@@ -47,7 +49,9 @@ pub fn parse(uri: Option<&String>, text: &str, namespace: Option<&String>) -> Re
fn from_yaml_value(uri: Option<&String>, value: &yaml::Yaml) -> Value {
match *value {
yaml::Yaml::String(ref value) => Value::new(uri, ValueKind::String(value.clone())),
- yaml::Yaml::Real(ref value) => Value::new(uri, ValueKind::Float(value.parse::<f64>().unwrap())),
+ yaml::Yaml::Real(ref value) => {
+ Value::new(uri, ValueKind::Float(value.parse::<f64>().unwrap()))
+ }
yaml::Yaml::Integer(value) => Value::new(uri, ValueKind::Integer(value)),
yaml::Yaml::Boolean(value) => Value::new(uri, ValueKind::Boolean(value)),
yaml::Yaml::Hash(ref table) => {
@@ -70,9 +74,7 @@ fn from_yaml_value(uri: Option<&String>, value: &yaml::Yaml) -> Value {
Value::new(uri, ValueKind::Array(l))
}
- yaml::Yaml::Null => {
- Value::new(uri, ValueKind::Nil)
- }
+ yaml::Yaml::Null => Value::new(uri, ValueKind::Nil),
// TODO: how should we BadValue?
_ => {