From 06ad52157db7c785a84303ed85147863e8a8c45e Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 29 Jun 2017 14:04:54 -0700 Subject: [PATCH] Use Into from 'toml' instead of 'IntoValue'. --- lib/Cargo.toml | 2 +- lib/src/config/builder.rs | 11 ++--- lib/src/config/config.rs | 12 ++--- lib/src/config/mod.rs | 1 - lib/src/config/toml_ext.rs | 96 +++++--------------------------------- 5 files changed, 23 insertions(+), 99 deletions(-) diff --git a/lib/Cargo.toml b/lib/Cargo.toml index bec8a188..f25b4848 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -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" diff --git a/lib/src/config/builder.rs b/lib/src/config/builder.rs index 9bc6d84b..8eaaf114 100644 --- a/lib/src/config/builder.rs +++ b/lib/src/config/builder.rs @@ -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`, `HashMap, V: IntoValue>`, - /// and all integer and float types. + /// `Into` including `&str`, `String`, `Vec>`, + /// `HashMap, V: Into>`, 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(mut self, name: &str, value: V) -> Self { - self.extras.insert(name.into(), value.into_value()); + pub fn extra>(mut self, name: &str, value: V) -> Self { + self.extras.insert(name.into(), value.into()); self } diff --git a/lib/src/config/config.rs b/lib/src/config/config.rs index 67ae2491..25fc480e 100644 --- a/lib/src/config/config.rs +++ b/lib/src/config/config.rs @@ -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); diff --git a/lib/src/config/mod.rs b/lib/src/config/mod.rs index 657cf77d..d2199248 100644 --- a/lib/src/config/mod.rs +++ b/lib/src/config/mod.rs @@ -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; diff --git a/lib/src/config/toml_ext.rs b/lib/src/config/toml_ext.rs index a52e46d9..ed60b2e4 100644 --- a/lib/src/config/toml_ext.rs +++ b/lib/src/config/toml_ext.rs @@ -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 { return Err("value is empty") } - let value = if let Ok(int) = i64::from_str(string) { + let value = if let Ok(int) = string.parse::() { Value::Integer(int) - } else if let Ok(float) = f64::from_str(string) { + } else if let Ok(float) = string.parse::() { Value::Float(float) - } else if let Ok(boolean) = bool::from_str(string) { + } else if let Ok(boolean) = string.parse::() { 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 { 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 IntoValue for Vec { - #[inline(always)] - fn into_value(self) -> Value { - Value::Array(self.into_iter().map(|v| v.into_value()).collect()) - } -} - -impl, V: IntoValue> IntoValue for BTreeMap { - fn into_value(self) -> Value { - let table = self.into_iter() - .map(|(s, v)| (s.into(), v.into_value())) - .collect(); - - Value::Table(table) - } -} - -impl + Hash + Eq, V: IntoValue> IntoValue for HashMap { - 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 })); }