diff options
author | Alex Crichton <alex@alexcrichton.com> | 2017-06-20 10:17:08 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-20 10:17:08 -0500 |
commit | 708468ece748e2aebefb80ac026100c7ed6f556f (patch) | |
tree | a04124f3eb6a5aa2a28d6c61b3a26d54d4bbb8df /src | |
parent | 6a3fba6e827b2ace0ea9f914725628064fc37ef5 (diff) | |
parent | 6163e708849e5afe171e157bdbc21420ea2a86f1 (diff) | |
download | milf-rs-708468ece748e2aebefb80ac026100c7ed6f556f.tar.gz milf-rs-708468ece748e2aebefb80ac026100c7ed6f556f.zip |
Merge pull request #190 from SergioBenitez/master
Add exhaustive From<T> impls for Value
Diffstat (limited to 'src')
-rw-r--r-- | src/value.rs | 66 |
1 files changed, 40 insertions, 26 deletions
diff --git a/src/value.rs b/src/value.rs index 83be066..5efdb92 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,6 +1,7 @@ //! Definition of a TOML value -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashMap}; +use std::hash::Hash; use std::fmt; use std::ops; use std::str::FromStr; @@ -219,48 +220,61 @@ impl<I> ops::IndexMut<I> for Value where I: Index { } } -impl From<String> for Value { - fn from(val: String) -> Value { - Value::String(val) +impl<'a> From<&'a str> for Value { + #[inline] + fn from(val: &'a str) -> Value { + Value::String(val.to_string()) } } -impl From<i64> for Value { - fn from(val: i64) -> Value { - Value::Integer(val) +impl<V: Into<Value>> From<Vec<V>> for Value { + fn from(val: Vec<V>) -> Value { + Value::Array(val.into_iter().map(|v| v.into()).collect()) } } -impl From<f64> for Value { - fn from(val: f64) -> Value { - Value::Float(val) - } -} +impl<S: Into<String>, V: Into<Value>> From<BTreeMap<S, V>> for Value { + fn from(val: BTreeMap<S, V>) -> Value { + let table = val.into_iter() + .map(|(s, v)| (s.into(), v.into())) + .collect(); -impl From<bool> for Value { - fn from(val: bool) -> Value { - Value::Boolean(val) + Value::Table(table) } } -impl From<Array> for Value { - fn from(val: Array) -> Value { - Value::Array(val) - } -} +impl<S: Into<String> + Hash + Eq, V: Into<Value>> From<HashMap<S, V>> for Value { + fn from(val: HashMap<S, V>) -> Value { + let table = val.into_iter() + .map(|(s, v)| (s.into(), v.into())) + .collect(); -impl From<Table> for Value { - fn from(val: Table) -> Value { - Value::Table(val) + Value::Table(table) } } -impl From<Datetime> for Value { - fn from(val: Datetime) -> Value { - Value::Datetime(val) +macro_rules! impl_into_value { + ($variant:ident : $T:ty) => { + impl From<$T> for Value { + #[inline] + fn from(val: $T) -> Value { + Value::$variant(val.into()) + } + } } } +impl_into_value!(String: String); +impl_into_value!(Integer: i64); +impl_into_value!(Integer: i32); +impl_into_value!(Integer: i8); +impl_into_value!(Integer: u8); +impl_into_value!(Integer: u32); +impl_into_value!(Float: f64); +impl_into_value!(Float: f32); +impl_into_value!(Boolean: bool); +impl_into_value!(Datetime: Datetime); + /// Types that can be used to index a `toml::Value` /// /// Currently this is implemented for `usize` to index arrays and `str` to index |