Add key-value-splitter helper
This commit is contained in:
parent
9b77ae1348
commit
77204c8e22
2 changed files with 54 additions and 0 deletions
53
libimagutil/src/key_value_split.rs
Normal file
53
libimagutil/src/key_value_split.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
use regex::Regex;
|
||||
|
||||
pub fn split_into_key_value(s: String) -> Option<(String, String)> {
|
||||
let r = "^(?P<KEY>(.*))=((\"(?P<DOUBLE_QVAL>(.*))\")|(\'(?P<SINGLE_QVAL>(.*)))\'|(?P<VAL>[^\'\"](.*)[^\'\"]))$";
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
extern crate regex;
|
||||
|
||||
pub mod key_value_split;
|
||||
pub mod variants;
|
||||
|
|
Loading…
Reference in a new issue