Use Into<Value> from 'toml' instead of 'IntoValue'.

This commit is contained in:
Sergio Benitez 2017-06-29 14:04:54 -07:00
parent ef4c2fd989
commit 06ad52157d
5 changed files with 23 additions and 99 deletions

View File

@ -21,7 +21,7 @@ tls = ["rustls", "hyper-rustls"]
yansi = { version = "0.3", features = ["nightly"] }
log = "0.3"
url = "1"
toml = "0.4"
toml = "0.4.2"
num_cpus = "1"
state = "0.3.1"
time = "0.1"

View File

@ -2,7 +2,6 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use config::{Result, Config, Value, Environment, Limits, LoggingLevel};
use config::toml_ext::IntoValue;
/// Structure following the builder pattern for building `Config` structures.
#[derive(Clone)]
@ -245,9 +244,9 @@ impl ConfigBuilder {
/// Adds an extra configuration parameter with `name` and `value` to the
/// configuration being built. The value can be any type that implements
/// [IntoValue](/rocket/config/trait.IntoValue.html) including `&str`,
/// `String`, `Vec<V: IntoValue>`, `HashMap<S: Into<String>, V: IntoValue>`,
/// and all integer and float types.
/// `Into<Value>` including `&str`, `String`, `Vec<V: Into<Value>>`,
/// `HashMap<S: Into<String>, V: Into<Value>>`, and most integer and float
/// types.
///
/// # Example
///
@ -262,8 +261,8 @@ impl ConfigBuilder {
/// assert_eq!(config.get_float("pi"), Ok(3.14));
/// assert_eq!(config.get_str("custom_dir"), Ok("/a/b/c"));
/// ```
pub fn extra<V: IntoValue>(mut self, name: &str, value: V) -> Self {
self.extras.insert(name.into(), value.into_value());
pub fn extra<V: Into<Value>>(mut self, name: &str, value: V) -> Self {
self.extras.insert(name.into(), value.into());
self
}

View File

@ -537,7 +537,7 @@ impl Config {
///
/// ```rust
/// use std::collections::HashMap;
/// use rocket::config::{Config, Environment, IntoValue};
/// use rocket::config::{Config, Environment};
///
/// # use rocket::config::ConfigError;
/// # fn config_test() -> Result<(), ConfigError> {
@ -545,8 +545,8 @@ impl Config {
///
/// // Create the `extras` map.
/// let mut extras = HashMap::new();
/// extras.insert("another_port".to_string(), 1044.into_value());
/// extras.insert("templates".to_string(), "my_dir".into_value());
/// extras.insert("another_port".to_string(), 1044.into());
/// extras.insert("templates".to_string(), "my_dir".into());
///
/// config.set_extras(extras);
/// # Ok(())
@ -564,7 +564,7 @@ impl Config {
///
/// ```rust
/// use std::collections::HashMap;
/// use rocket::config::{Config, Environment, IntoValue};
/// use rocket::config::{Config, Environment};
///
/// # use rocket::config::ConfigError;
/// # fn config_test() -> Result<(), ConfigError> {
@ -573,8 +573,8 @@ impl Config {
///
/// // Add a couple of extras to the config.
/// let mut extras = HashMap::new();
/// extras.insert("another_port".to_string(), 1044.into_value());
/// extras.insert("templates".to_string(), "my_dir".into_value());
/// extras.insert("another_port".to_string(), 1044.into());
/// extras.insert("templates".to_string(), "my_dir".into());
/// config.set_extras(extras);
///
/// assert_eq!(config.extras().count(), 2);

View File

@ -210,7 +210,6 @@ pub use self::error::ConfigError;
pub use self::environment::Environment;
pub use self::config::Config;
pub use self::builder::ConfigBuilder;
pub use self::toml_ext::IntoValue;
pub use logger::LoggingLevel;
pub(crate) use self::toml_ext::LoggedValue;

View File

@ -1,7 +1,5 @@
use std::fmt;
use std::collections::{HashMap, BTreeMap};
use std::hash::Hash;
use std::str::FromStr;
use std::collections::BTreeMap;
use config::Value;
@ -10,11 +8,11 @@ pub fn parse_simple_toml_value(string: &str) -> Result<Value, &'static str> {
return Err("value is empty")
}
let value = if let Ok(int) = i64::from_str(string) {
let value = if let Ok(int) = string.parse::<i64>() {
Value::Integer(int)
} else if let Ok(float) = f64::from_str(string) {
} else if let Ok(float) = string.parse::<f64>() {
Value::Float(float)
} else if let Ok(boolean) = bool::from_str(string) {
} else if let Ok(boolean) = string.parse::<bool>() {
Value::Boolean(boolean)
} else if string.starts_with('{') {
if !string.ends_with('}') {
@ -64,76 +62,6 @@ pub fn parse_simple_toml_value(string: &str) -> Result<Value, &'static str> {
Ok(value)
}
/// Conversion trait from standard types into TOML `Value`s.
pub trait IntoValue {
/// Converts `self` into a TOML `Value`.
fn into_value(self) -> Value;
}
impl<'a> IntoValue for &'a str {
#[inline(always)]
fn into_value(self) -> Value {
Value::String(self.to_string())
}
}
impl IntoValue for Value {
#[inline(always)]
fn into_value(self) -> Value {
self
}
}
impl<V: IntoValue> IntoValue for Vec<V> {
#[inline(always)]
fn into_value(self) -> Value {
Value::Array(self.into_iter().map(|v| v.into_value()).collect())
}
}
impl<S: Into<String>, V: IntoValue> IntoValue for BTreeMap<S, V> {
fn into_value(self) -> Value {
let table = self.into_iter()
.map(|(s, v)| (s.into(), v.into_value()))
.collect();
Value::Table(table)
}
}
impl<S: Into<String> + Hash + Eq, V: IntoValue> IntoValue for HashMap<S, V> {
fn into_value(self) -> Value {
let table = self.into_iter()
.map(|(s, v)| (s.into(), v.into_value()))
.collect();
Value::Table(table)
}
}
macro_rules! impl_into_value {
($variant:ident : $t:ty) => ( impl_into_value!($variant: $t,); );
($variant:ident : $t:ty, $($extra:tt)*) => (
impl IntoValue for $t {
fn into_value(self) -> Value {
Value::$variant(self $($extra)*)
}
}
)
}
impl_into_value!(String: String);
impl_into_value!(Integer: i64);
impl_into_value!(Integer: isize, as i64);
impl_into_value!(Integer: i32, as i64);
impl_into_value!(Integer: i8, as i64);
impl_into_value!(Integer: u8, as i64);
impl_into_value!(Integer: u32, as i64);
impl_into_value!(Boolean: bool);
impl_into_value!(Float: f64);
impl_into_value!(Float: f32, as f64);
/// A simple wrapper over a `Value` reference with a custom implementation of
/// `Display`. This is used to log config values at initialization.
pub(crate) struct LoggedValue<'a>(pub &'a Value);
@ -163,7 +91,6 @@ impl<'a> fmt::Display for LoggedValue<'a> {
mod test {
use std::collections::BTreeMap;
use super::parse_simple_toml_value;
use super::IntoValue;
use super::Value::*;
macro_rules! assert_parse {
@ -186,22 +113,21 @@ mod test {
assert_parse!("\"hi\"", String("hi".into()));
assert_parse!("[]", Array(Vec::new()));
assert_parse!("[1]", vec![1].into_value());
assert_parse!("[1, 2, 3]", vec![1, 2, 3].into_value());
assert_parse!("[1.32, 2]",
vec![1.32.into_value(), 2.into_value()].into_value());
assert_parse!("[1]", vec![1].into());
assert_parse!("[1, 2, 3]", vec![1, 2, 3].into());
assert_parse!("[1.32, 2]", Array(vec![1.32.into(), 2.into()]));
assert_parse!("{}", Table(BTreeMap::new()));
assert_parse!("{a=b}", Table({
let mut map = BTreeMap::new();
map.insert("a".into(), "b".into_value());
map.insert("a".into(), "b".into());
map
}));
assert_parse!("{v=1, on=true,pi=3.14}", Table({
let mut map = BTreeMap::new();
map.insert("v".into(), 1.into_value());
map.insert("on".into(), true.into_value());
map.insert("pi".into(), 3.14.into_value());
map.insert("v".into(), 1.into());
map.insert("on".into(), true.into());
map.insert("pi".into(), 3.14.into());
map
}));
}