2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2016-01-20 08:42:48 +00:00
|
|
|
use regex::Regex;
|
|
|
|
|
2016-01-20 09:37:24 +00:00
|
|
|
use std::convert::Into;
|
|
|
|
use std::convert::From;
|
|
|
|
|
|
|
|
use std::option::Option;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
pub struct KeyValue<K, V> {
|
|
|
|
k: K,
|
|
|
|
v: V,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V> KeyValue<K, V> {
|
|
|
|
|
|
|
|
pub fn new(k: K, v: V) -> KeyValue<K, V> {
|
|
|
|
KeyValue { k: k, v: v }
|
|
|
|
}
|
|
|
|
|
2016-01-20 09:45:13 +00:00
|
|
|
pub fn key(&self) -> &K {
|
|
|
|
&self.k
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn value(&self) -> &V {
|
|
|
|
&self.v
|
|
|
|
}
|
|
|
|
|
2016-01-20 09:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V> Into<(K, V)> for KeyValue<K, V> {
|
|
|
|
|
|
|
|
fn into(self) -> (K, V) {
|
|
|
|
(self.k, self.v)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait IntoKeyValue<K, V> {
|
|
|
|
fn into_kv(self) -> Option<KeyValue<K, V>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoKeyValue<String, String> for String {
|
|
|
|
|
|
|
|
fn into_kv(self) -> Option<KeyValue<String, String>> {
|
2016-01-29 19:03:43 +00:00
|
|
|
let key = {
|
2016-02-20 20:10:22 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref R: Regex = Regex::new("^(?P<KEY>([^=]*))=(.*)$").unwrap();
|
|
|
|
}
|
|
|
|
R.captures(&self[..])
|
2016-01-29 19:03:43 +00:00
|
|
|
.and_then(|caps| caps.name("KEY"))
|
|
|
|
};
|
|
|
|
|
|
|
|
let value = {
|
2016-02-20 20:10:22 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref R: Regex = Regex::new("(.*)=(\"(?P<QVALUE>([^\"]*))\"|(?P<VALUE>(.*)))$")
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
R.captures(&self[..])
|
2018-04-24 18:58:02 +00:00
|
|
|
.map(|c| {
|
|
|
|
c.name("VALUE")
|
|
|
|
.or_else(|| c.name("QVALUE"))
|
|
|
|
.map(|m| m.as_str())
|
|
|
|
.unwrap_or("")
|
|
|
|
})
|
2016-01-29 19:03:43 +00:00
|
|
|
};
|
|
|
|
|
2017-01-30 08:42:45 +00:00
|
|
|
key.and_then(|k| {
|
|
|
|
value.and_then(|v| Some(KeyValue::new(String::from(k.as_str()), String::from(v))))
|
|
|
|
})
|
2016-01-20 09:37:24 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 08:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2016-01-20 09:42:40 +00:00
|
|
|
use super::{KeyValue, IntoKeyValue};
|
2016-01-20 08:42:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_single_quoted() {
|
2016-01-29 21:11:59 +00:00
|
|
|
let s = String::from("foo='bar'").into_kv().unwrap();
|
|
|
|
assert_eq!(KeyValue::new(String::from("foo"), String::from("\'bar\'")), s);
|
2016-01-20 08:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_quoted() {
|
2016-01-20 09:42:40 +00:00
|
|
|
let s = String::from("foo=\"bar\"").into_kv().unwrap();
|
|
|
|
assert_eq!(KeyValue::new(String::from("foo"), String::from("bar")), s);
|
2016-01-20 08:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_and_single_quoted() {
|
2016-01-29 21:11:59 +00:00
|
|
|
let s = String::from("foo=\"bar\'").into_kv().unwrap();
|
|
|
|
assert_eq!(KeyValue::new(String::from("foo"), String::from("\"bar\'")), s);
|
2016-01-20 08:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_single_and_double_quoted() {
|
2016-01-29 21:11:59 +00:00
|
|
|
let s = String::from("foo=\'bar\"").into_kv().unwrap();
|
|
|
|
assert_eq!(KeyValue::new(String::from("foo"), String::from("\'bar\"")), s);
|
2016-01-20 08:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_quoted() {
|
2016-01-20 09:42:40 +00:00
|
|
|
let s = String::from("foo=bar").into_kv().unwrap();
|
|
|
|
assert_eq!(KeyValue::new(String::from("foo"), String::from("bar")), s);
|
2016-01-20 08:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|