From 10fa3e3dafd3b44623b863344e562e80209e53fb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 31 Jan 2016 12:54:59 +0100 Subject: [PATCH] util: Add parsing ints and floats --- imag-store/src/util.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/imag-store/src/util.rs b/imag-store/src/util.rs index 0c033535..d339dc6f 100644 --- a/imag-store/src/util.rs +++ b/imag-store/src/util.rs @@ -76,6 +76,8 @@ fn insert_key_into(current: String, } fn parse_value(value: String) -> Value { + use std::str::FromStr; + fn is_ary(v: &String) -> bool { v.chars().next() == Some('[') && v.chars().last() == Some(']') && v.len() >= 3 } @@ -91,8 +93,22 @@ fn parse_value(value: String) -> Value { let sub = &value[1..(value.len()-1)]; Value::Array(sub.split(",").map(|v| parse_value(String::from(v))).collect()) } else { - debug!("Building String out of: {:?}...", value); - Value::String(value) + FromStr::from_str(&value[..]) + .map(|i: i64| { + debug!("Building Integer out of: {:?}...", value); + Value::Integer(i) + }) + .unwrap_or_else(|_| { + FromStr::from_str(&value[..]) + .map(|f: f64| { + debug!("Building Float out of: {:?}...", value); + Value::Float(f) + }) + .unwrap_or_else(|_| { + debug!("Building String out of: {:?}...", value); + Value::String(value) + }) + }) } }