From 2b438ed9b53fee5689032f3b5fcdda8d15becd5f Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Thu, 22 Jun 2017 14:14:29 -0700 Subject: Add builder API to Config --- src/file/format/json.rs | 30 +++++++++++++++--------------- src/file/format/mod.rs | 6 +++++- src/file/format/toml.rs | 26 ++++++++++++++------------ src/file/format/yaml.rs | 34 ++++++++++++++++++---------------- 4 files changed, 52 insertions(+), 44 deletions(-) (limited to 'src/file/format') 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, Box> { +pub fn parse(uri: Option<&String>, + text: &str, + namespace: Option<&String>) + -> Result, Box> { // 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, Box> { + pub fn parse(&self, + uri: Option<&String>, + text: &str, + namespace: Option<&String>) + -> Result, Box> { 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, Box> { +pub fn parse(uri: Option<&String>, + text: &str, + namespace: Option<&String>) + -> Result, Box> { // 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, Box> { +pub fn parse(uri: Option<&String>, + text: &str, + namespace: Option<&String>) + -> Result, Box> { 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::().unwrap())), + yaml::Yaml::Real(ref value) => { + Value::new(uri, ValueKind::Float(value.parse::().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? _ => { -- cgit v1.2.3