From 448c69891e29761e4199e5415b1ea4107446256d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 29 Jan 2016 20:03:43 +0100 Subject: [PATCH 1/3] Reimplement IntoKeyValue for String * Double quotes should not be in the result * Regex allows quotes in strings now --- libimagutil/src/key_value_split.rs | 32 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/libimagutil/src/key_value_split.rs b/libimagutil/src/key_value_split.rs index b928820b..0efb024f 100644 --- a/libimagutil/src/key_value_split.rs +++ b/libimagutil/src/key_value_split.rs @@ -42,16 +42,28 @@ pub trait IntoKeyValue { impl IntoKeyValue for String { fn into_kv(self) -> Option> { - let r = "^(?P(.*))=((\"(?P(.*))\")|(\'(?P(.*)))\'|(?P[^\'\"](.*)[^\'\"]))$"; - 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([^=]*))=(.*)$"; + let r = Regex::new(r).unwrap(); + r.captures(&self[..]) + .and_then(|caps| caps.name("KEY")) + }; + + let value = { + let r = "(.*)=(\"(?P([^\"]*))\"|(?P(.*)))$"; + 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))) + }) }) } From db387139774939d48736607ddfa572208f1d0bd3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 29 Jan 2016 20:51:15 +0100 Subject: [PATCH 2/3] Adapt test: single quoted --- libimagutil/src/key_value_split.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libimagutil/src/key_value_split.rs b/libimagutil/src/key_value_split.rs index 0efb024f..1e455420 100644 --- a/libimagutil/src/key_value_split.rs +++ b/libimagutil/src/key_value_split.rs @@ -75,8 +75,7 @@ mod test { #[test] fn test_single_quoted() { - let s = String::from("foo='bar'").into_kv().unwrap(); - assert_eq!(KeyValue::new(String::from("foo"), String::from("bar")), s); + assert!(String::from("foo='bar'").into_kv().is_none()); } #[test] From e3daf7c85330b5c2d641fcf802a3e0a04edd2b09 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 29 Jan 2016 22:11:59 +0100 Subject: [PATCH 3/3] tests: Allow quotes in keys --- libimagutil/src/key_value_split.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libimagutil/src/key_value_split.rs b/libimagutil/src/key_value_split.rs index 1e455420..796f0c82 100644 --- a/libimagutil/src/key_value_split.rs +++ b/libimagutil/src/key_value_split.rs @@ -75,7 +75,8 @@ mod test { #[test] fn test_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] @@ -86,12 +87,14 @@ mod test { #[test] 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] 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]