From 115fe07e2c11aa72e91a5ce9b028ed1c1ff7d806 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Thu, 2 Feb 2017 12:03:55 -0800 Subject: Add support for Table/Array and deep merging of configuration values --- src/value.rs | 65 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 25 deletions(-) (limited to 'src/value.rs') diff --git a/src/value.rs b/src/value.rs index b66c721..5228d91 100644 --- a/src/value.rs +++ b/src/value.rs @@ -6,26 +6,27 @@ use std::borrow::Cow; /// /// Has an underlying or native type that comes from the configuration source /// but will be coerced into the requested type. -#[derive(Debug, Clone)] -pub enum Value<'a> { - String(Cow<'a, str>), +#[derive(Debug, Clone, PartialEq)] +pub enum Value { + String(String), Integer(i64), Float(f64), Boolean(bool), - Table(HashMap>), - Array(Vec>), + Table(HashMap), + Array(Vec), } -impl<'a> Value<'a> { +impl Value { /// Gets the underlying value as a string, performing a conversion only if neccessary. - pub fn as_str(&'a self) -> Option> { + #[allow(needless_lifetimes)] + pub fn as_str<'a>(&'a self) -> Option> { match *self { - Value::String(ref value) => Some(Cow::Borrowed(&*value)), - Value::Integer(value) => Some(value.to_string().into()), - Value::Float(value) => Some(value.to_string().into()), - Value::Boolean(value) => Some(value.to_string().into()), + Value::String(ref value) => Some(Cow::Borrowed(value)), + Value::Integer(value) => Some(Cow::Owned(value.to_string())), + Value::Float(value) => Some(Cow::Owned(value.to_string())), + Value::Boolean(value) => Some(Cow::Owned(value.to_string())), - _ => unimplemented!(), + _ => None, } } @@ -44,7 +45,7 @@ impl<'a> Value<'a> { } } - _ => unimplemented!(), + _ => None, } } @@ -56,7 +57,7 @@ impl<'a> Value<'a> { Value::Boolean(value) => Some(if value { 1 } else { 0 }), Value::Float(value) => Some(value.round() as i64), - _ => unimplemented!(), + _ => None, } } @@ -68,7 +69,15 @@ impl<'a> Value<'a> { Value::Integer(value) => Some(value as f64), Value::Boolean(value) => Some(if value { 1.0 } else { 0.0 }), - _ => unimplemented!(), + _ => None, + } + } + + /// Gets the underlying type as a map; only works if the type is actually a map. + pub fn as_map(&self) -> Option<&HashMap> { + match *self { + Value::Table(ref value) => Some(value), + _ => None } } } @@ -76,32 +85,38 @@ impl<'a> Value<'a> { // Generalized construction from type into variant is needed // for setting configuration values -impl<'a> From for Value<'a> { - fn from(value: String) -> Value<'a> { +impl From for Value { + fn from(value: String) -> Value { Value::String(value.into()) } } -impl<'a> From<&'a str> for Value<'a> { - fn from(value: &'a str) -> Value<'a> { +impl<'a> From<&'a str> for Value { + fn from(value: &'a str) -> Value { Value::String(value.into()) } } -impl<'a> From for Value<'a> { - fn from(value: i64) -> Value<'a> { +impl From for Value { + fn from(value: i64) -> Value { Value::Integer(value) } } -impl<'a> From for Value<'a> { - fn from(value: f64) -> Value<'a> { +impl From for Value { + fn from(value: f64) -> Value { Value::Float(value) } } -impl<'a> From for Value<'a> { - fn from(value: bool) -> Value<'a> { +impl From for Value { + fn from(value: bool) -> Value { Value::Boolean(value) } } + +impl From> for Value { + fn from(value: HashMap) -> Value { + Value::Table(value) + } +} -- cgit v1.2.3