Reimplement IntoKeyValue for String

* Double quotes should not be in the result
* Regex allows quotes in strings now
This commit is contained in:
Matthias Beyer 2016-01-29 20:03:43 +01:00
parent 33b6a89a02
commit 448c69891e

View file

@ -42,16 +42,28 @@ pub trait IntoKeyValue<K, V> {
impl IntoKeyValue<String, String> for String { impl IntoKeyValue<String, String> for String {
fn into_kv(self) -> Option<KeyValue<String, String>> { fn into_kv(self) -> Option<KeyValue<String, String>> {
let r = "^(?P<KEY>(.*))=((\"(?P<DOUBLE_QVAL>(.*))\")|(\'(?P<SINGLE_QVAL>(.*)))\'|(?P<VAL>[^\'\"](.*)[^\'\"]))$"; let key = {
let regex = Regex::new(r).unwrap(); let r = "^(?P<KEY>([^=]*))=(.*)$";
regex.captures(&self[..]).and_then(|cap| { let r = Regex::new(r).unwrap();
cap.name("KEY") r.captures(&self[..])
.map(|name| { .and_then(|caps| caps.name("KEY"))
cap.name("SINGLE_QVAL") };
.or(cap.name("DOUBLE_QVAL"))
.or(cap.name("VAL")) let value = {
.map(|value| KeyValue::new(String::from(name), String::from(value))) let r = "(.*)=(\"(?P<QVALUE>([^\"]*))\"|(?P<VALUE>(.*)))$";
}).unwrap_or(None) let r = Regex::new(r).unwrap();
r.captures(&self[..])
.map(|caps| {
caps.name("VALUE")
.or(caps.name("QVALUE"))
.unwrap_or("")
})
};
key.and_then(|k| {
value.and_then(|v| {
Some(KeyValue::new(String::from(k), String::from(v)))
})
}) })
} }