Crates.io | query_map |
lib.rs | query_map |
version | 0.7.0 |
source | src |
created_at | 2022-02-15 03:47:02.74708 |
updated_at | 2023-07-15 21:58:41.604631 |
description | VectorMap is a generic wrapper around HashMap |
homepage | |
repository | https://github.com/calavera/query-map-rs |
max_upload_size | |
id | 532528 |
size | 31,764 |
QueryMap is a generic wrapper around HashMap<String, Vec
QueryMap can normalize HashMap structures with single value elements into structures with value vector elements.
cargo install query_map
Create a QueryMap from a HashMap:
use std::collections::HashMap;
use query_map::QueryMap;
let mut data = HashMap::new();
data.insert("foo".into(), vec!["bar".into()]);
let map: QueryMap = QueryMap::from(data);
assert_eq!("bar", map.first("foo").unwrap());
assert_eq!(None, map.first("bar"));
Create a QueryMap from a Serde Value (requires serde
feature):
use query_map::QueryMap;
#[derive(Deserialize)]
struct Test {
data: QueryMap,
}
let json = serde_json::json!({
"data": {
"foo": "bar"
}
});
let test: Test = serde_json::from_value(json).unwrap();
assert_eq!("bar", test.data.first("foo").unwrap());
Create a QueryMap from a query string (requires url-query
feature):
use query_map::QueryMap;
let data = "foo=bar&baz=quux&foo=qux";
let map = data.parse::<QueryMap>().unwrap();
let got = map.all("foo").unwrap();
assert_eq!(vec!["bar", "qux"], got);