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 {
|
||||
|
||||
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)))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue