Reimplement IntoKeyValue for String
* Double quotes should not be in the result * Regex allows quotes in strings now
This commit is contained in:
parent
33b6a89a02
commit
448c69891e
1 changed files with 22 additions and 10 deletions
|
@ -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)))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue