From 29c58a2fa95d5504809279a2c04519e0b8e87e28 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 20 Jan 2016 09:42:17 +0100 Subject: [PATCH 1/6] Add dep: regex = 0.1.47 --- libimagutil/Cargo.lock | 39 +++++++++++++++++++++++++++++++++++++++ libimagutil/Cargo.toml | 4 ++++ 2 files changed, 43 insertions(+) diff --git a/libimagutil/Cargo.lock b/libimagutil/Cargo.lock index bd20cbfd..6cf7752c 100644 --- a/libimagutil/Cargo.lock +++ b/libimagutil/Cargo.lock @@ -1,4 +1,43 @@ [root] name = "libimagutil" version = "0.1.0" +dependencies = [ + "regex 0.1.47 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aho-corasick" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "memchr" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/libimagutil/Cargo.toml b/libimagutil/Cargo.toml index 708e43e7..341d5aa1 100644 --- a/libimagutil/Cargo.toml +++ b/libimagutil/Cargo.toml @@ -2,3 +2,7 @@ name = "libimagutil" version = "0.1.0" authors = ["Matthias Beyer "] + +[dependencies] +regex = "0.1.47" + From 9b77ae13486169be554f1d392abe11deadd5cff7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 20 Jan 2016 09:42:37 +0100 Subject: [PATCH 2/6] Use regex --- libimagutil/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index 0797cbab..0fb164de 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -1 +1,3 @@ +extern crate regex; + pub mod variants; From 77204c8e22d7243c5a261ff080ebf66277214f6f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 20 Jan 2016 09:42:48 +0100 Subject: [PATCH 3/6] Add key-value-splitter helper --- libimagutil/src/key_value_split.rs | 53 ++++++++++++++++++++++++++++++ libimagutil/src/lib.rs | 1 + 2 files changed, 54 insertions(+) create mode 100644 libimagutil/src/key_value_split.rs diff --git a/libimagutil/src/key_value_split.rs b/libimagutil/src/key_value_split.rs new file mode 100644 index 00000000..a00bd2db --- /dev/null +++ b/libimagutil/src/key_value_split.rs @@ -0,0 +1,53 @@ +use regex::Regex; + +pub fn split_into_key_value(s: String) -> Option<(String, String)> { + let r = "^(?P(.*))=((\"(?P(.*))\")|(\'(?P(.*)))\'|(?P[^\'\"](.*)[^\'\"]))$"; + let regex = Regex::new(r).unwrap(); + regex.captures(&s[..]).and_then(|cap| { + cap.name("KEY") + .map(|name| { + cap.name("SINGLE_QVAL") + .or(cap.name("DOUBLE_QVAL")) + .or(cap.name("VAL")) + .map(|value| (String::from(name), String::from(value))) + }).unwrap_or(None) + }) +} + +#[cfg(test)] +mod test { + use super::split_into_key_value; + + #[test] + fn test_single_quoted() { + let s = String::from("foo='bar'"); + assert_eq!(Some((String::from("foo"), String::from("bar"))), split_into_key_value(s)); + } + + #[test] + fn test_double_quoted() { + let s = String::from("foo=\"bar\""); + assert_eq!(Some((String::from("foo"), String::from("bar"))), split_into_key_value(s)); + } + + #[test] + fn test_double_and_single_quoted() { + let s = String::from("foo=\"bar\'"); + assert!(split_into_key_value(s).is_none()); + } + + #[test] + fn test_single_and_double_quoted() { + let s = String::from("foo=\'bar\""); + assert!(split_into_key_value(s).is_none()); + } + + #[test] + fn test_not_quoted() { + let s = String::from("foo=bar"); + assert_eq!(Some((String::from("foo"), String::from("bar"))), split_into_key_value(s)); + } + +} + + diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index 0fb164de..d26e1d7e 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -1,3 +1,4 @@ extern crate regex; +pub mod key_value_split; pub mod variants; From 2c5d61c4566ccb16189d7d817bc2a3259d37e486 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 20 Jan 2016 10:37:24 +0100 Subject: [PATCH 4/6] Split String->key-value with types --- libimagutil/src/key_value_split.rs | 59 ++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/libimagutil/src/key_value_split.rs b/libimagutil/src/key_value_split.rs index a00bd2db..32fd30a9 100644 --- a/libimagutil/src/key_value_split.rs +++ b/libimagutil/src/key_value_split.rs @@ -1,17 +1,52 @@ use regex::Regex; -pub fn split_into_key_value(s: String) -> Option<(String, String)> { - let r = "^(?P(.*))=((\"(?P(.*))\")|(\'(?P(.*)))\'|(?P[^\'\"](.*)[^\'\"]))$"; - let regex = Regex::new(r).unwrap(); - regex.captures(&s[..]).and_then(|cap| { - cap.name("KEY") - .map(|name| { - cap.name("SINGLE_QVAL") - .or(cap.name("DOUBLE_QVAL")) - .or(cap.name("VAL")) - .map(|value| (String::from(name), String::from(value))) - }).unwrap_or(None) - }) +use std::convert::Into; +use std::convert::From; + +use std::option::Option; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct KeyValue { + k: K, + v: V, +} + +impl KeyValue { + + pub fn new(k: K, v: V) -> KeyValue { + KeyValue { k: k, v: v } + } + +} + +impl Into<(K, V)> for KeyValue { + + fn into(self) -> (K, V) { + (self.k, self.v) + } + +} + +pub trait IntoKeyValue { + fn into_kv(self) -> Option>; +} + +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) + }) + } + } #[cfg(test)] From 78e104706f03bf4a2475b6b9dda083bf06a95282 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 20 Jan 2016 10:42:40 +0100 Subject: [PATCH 5/6] Adapt tests --- libimagutil/src/key_value_split.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/libimagutil/src/key_value_split.rs b/libimagutil/src/key_value_split.rs index 32fd30a9..567118bb 100644 --- a/libimagutil/src/key_value_split.rs +++ b/libimagutil/src/key_value_split.rs @@ -51,36 +51,34 @@ impl IntoKeyValue for String { #[cfg(test)] mod test { - use super::split_into_key_value; + use super::{KeyValue, IntoKeyValue}; #[test] fn test_single_quoted() { - let s = String::from("foo='bar'"); - assert_eq!(Some((String::from("foo"), String::from("bar"))), split_into_key_value(s)); + let s = String::from("foo='bar'").into_kv().unwrap(); + assert_eq!(KeyValue::new(String::from("foo"), String::from("bar")), s); } #[test] fn test_double_quoted() { - let s = String::from("foo=\"bar\""); - assert_eq!(Some((String::from("foo"), String::from("bar"))), split_into_key_value(s)); + let s = String::from("foo=\"bar\"").into_kv().unwrap(); + assert_eq!(KeyValue::new(String::from("foo"), String::from("bar")), s); } #[test] fn test_double_and_single_quoted() { - let s = String::from("foo=\"bar\'"); - assert!(split_into_key_value(s).is_none()); + assert!(String::from("foo=\"bar\'").into_kv().is_none()); } #[test] fn test_single_and_double_quoted() { - let s = String::from("foo=\'bar\""); - assert!(split_into_key_value(s).is_none()); + assert!(String::from("foo=\'bar\"").into_kv().is_none()); } #[test] fn test_not_quoted() { - let s = String::from("foo=bar"); - assert_eq!(Some((String::from("foo"), String::from("bar"))), split_into_key_value(s)); + let s = String::from("foo=bar").into_kv().unwrap(); + assert_eq!(KeyValue::new(String::from("foo"), String::from("bar")), s); } } From e86403978d3c1452dd55063dfb85a7b03c0d3523 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 20 Jan 2016 10:45:13 +0100 Subject: [PATCH 6/6] Add getter for key/value --- libimagutil/src/key_value_split.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libimagutil/src/key_value_split.rs b/libimagutil/src/key_value_split.rs index 567118bb..b928820b 100644 --- a/libimagutil/src/key_value_split.rs +++ b/libimagutil/src/key_value_split.rs @@ -17,6 +17,14 @@ impl KeyValue { KeyValue { k: k, v: v } } + pub fn key(&self) -> &K { + &self.k + } + + pub fn value(&self) -> &V { + &self.v + } + } impl Into<(K, V)> for KeyValue {