key_value_split.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // imag - the personal information management suite for the commandline
  3. // Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; version
  8. // 2.1 of the License.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. //
  19. use regex::Regex;
  20. use std::convert::Into;
  21. use std::convert::From;
  22. use std::option::Option;
  23. #[derive(Clone, Copy, Debug, PartialEq)]
  24. pub struct KeyValue<K, V> {
  25. k: K,
  26. v: V,
  27. }
  28. impl<K, V> KeyValue<K, V> {
  29. pub fn new(k: K, v: V) -> KeyValue<K, V> {
  30. KeyValue { k: k, v: v }
  31. }
  32. pub fn key(&self) -> &K {
  33. &self.k
  34. }
  35. pub fn value(&self) -> &V {
  36. &self.v
  37. }
  38. }
  39. impl<K, V> Into<(K, V)> for KeyValue<K, V> {
  40. fn into(self) -> (K, V) {
  41. (self.k, self.v)
  42. }
  43. }
  44. pub trait IntoKeyValue<K, V> {
  45. fn into_kv(self) -> Option<KeyValue<K, V>>;
  46. }
  47. impl IntoKeyValue<String, String> for String {
  48. fn into_kv(self) -> Option<KeyValue<String, String>> {
  49. let key = {
  50. lazy_static! {
  51. static ref R: Regex = Regex::new("^(?P<KEY>([^=]*))=(.*)$").unwrap();
  52. }
  53. R.captures(&self[..])
  54. .and_then(|caps| caps.name("KEY"))
  55. };
  56. let value = {
  57. lazy_static! {
  58. static ref R: Regex = Regex::new("(.*)=(\"(?P<QVALUE>([^\"]*))\"|(?P<VALUE>(.*)))$")
  59. .unwrap();
  60. }
  61. R.captures(&self[..])
  62. .map(|caps| caps.name("VALUE").or(caps.name("QVALUE")).unwrap_or(""))
  63. };
  64. key.and_then(|k| value.and_then(|v| Some(KeyValue::new(String::from(k), String::from(v)))))
  65. }
  66. }
  67. #[cfg(test)]
  68. mod test {
  69. use super::{KeyValue, IntoKeyValue};
  70. #[test]
  71. fn test_single_quoted() {
  72. let s = String::from("foo='bar'").into_kv().unwrap();
  73. assert_eq!(KeyValue::new(String::from("foo"), String::from("\'bar\'")), s);
  74. }
  75. #[test]
  76. fn test_double_quoted() {
  77. let s = String::from("foo=\"bar\"").into_kv().unwrap();
  78. assert_eq!(KeyValue::new(String::from("foo"), String::from("bar")), s);
  79. }
  80. #[test]
  81. fn test_double_and_single_quoted() {
  82. let s = String::from("foo=\"bar\'").into_kv().unwrap();
  83. assert_eq!(KeyValue::new(String::from("foo"), String::from("\"bar\'")), s);
  84. }
  85. #[test]
  86. fn test_single_and_double_quoted() {
  87. let s = String::from("foo=\'bar\"").into_kv().unwrap();
  88. assert_eq!(KeyValue::new(String::from("foo"), String::from("\'bar\"")), s);
  89. }
  90. #[test]
  91. fn test_not_quoted() {
  92. let s = String::from("foo=bar").into_kv().unwrap();
  93. assert_eq!(KeyValue::new(String::from("foo"), String::from("bar")), s);
  94. }
  95. }