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 {
fn into_kv(self) -> Option<KeyValue<String, String>> {
let r = "^(?P<KEY>(.*))=((\"(?P<DOUBLE_QVAL>(.*))\")|(\'(?P<SINGLE_QVAL>(.*)))\'|(?P<VAL>[^\'\"](.*)[^\'\"]))$";
let regex = Regex::new(r).unwrap();
regex.captures(&self[..]).and_then(|cap| {
cap.name("KEY")
.map(|name| {
cap.name("SINGLE_QVAL")
.or(cap.name("DOUBLE_QVAL"))
.or(cap.name("VAL"))
.map(|value| KeyValue::new(String::from(name), String::from(value)))
}).unwrap_or(None)
let key = {
let r = "^(?P<KEY>([^=]*))=(.*)$";
let r = Regex::new(r).unwrap();
r.captures(&self[..])
.and_then(|caps| caps.name("KEY"))
};
let value = {
let r = "(.*)=(\"(?P<QVALUE>([^\"]*))\"|(?P<VALUE>(.*)))$";
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)))
})
})
}