Merge pull request #159 from matthiasbeyer/libimagutil/reimplement-kv-split

Libimagutil/reimplement kv split
This commit is contained in:
Matthias Beyer 2016-01-30 13:48:02 +01:00
commit fa5d1553dd
1 changed files with 27 additions and 13 deletions

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)))
})
}) })
} }
@ -64,7 +76,7 @@ mod test {
#[test] #[test]
fn test_single_quoted() { fn test_single_quoted() {
let s = String::from("foo='bar'").into_kv().unwrap(); let s = String::from("foo='bar'").into_kv().unwrap();
assert_eq!(KeyValue::new(String::from("foo"), String::from("bar")), s); assert_eq!(KeyValue::new(String::from("foo"), String::from("\'bar\'")), s);
} }
#[test] #[test]
@ -75,12 +87,14 @@ mod test {
#[test] #[test]
fn test_double_and_single_quoted() { fn test_double_and_single_quoted() {
assert!(String::from("foo=\"bar\'").into_kv().is_none()); let s = String::from("foo=\"bar\'").into_kv().unwrap();
assert_eq!(KeyValue::new(String::from("foo"), String::from("\"bar\'")), s);
} }
#[test] #[test]
fn test_single_and_double_quoted() { fn test_single_and_double_quoted() {
assert!(String::from("foo=\'bar\"").into_kv().is_none()); let s = String::from("foo=\'bar\"").into_kv().unwrap();
assert_eq!(KeyValue::new(String::from("foo"), String::from("\'bar\"")), s);
} }
#[test] #[test]