clia_url_qs/
from_hashmap.rs

1use anyhow::Result;
2use serde::Serialize;
3
4/// Format an array of tuples to the URL query string.
5///
6/// ```rust
7/// let mut params = HashMap::new();
8/// params.insert("foo", "bar");
9/// params.insert("baz", "quux");
10///
11/// let res = from_hashmap(&params).unwrap();
12/// ```
13///
14/// If input is invalid, return error message.
15pub fn from_hashmap<T: Serialize + ?Sized>(arr: &T) -> Result<String> {
16    serde_urlencoded::to_string(arr).map_err(|e| e.into())
17}
18
19#[cfg(test)]
20mod tests {
21    use std::collections::HashMap;
22
23    use super::*;
24
25    #[test]
26    fn test_from_hashmap() {
27        let mut params = HashMap::new();
28        params.insert("foo", "bar");
29        params.insert("baz", "quux");
30
31        let res = from_hashmap(&params).unwrap();
32        print!("{res}");
33        // assert_eq!(from_hashmap(&params).unwrap(), "foo=bar&baz=quux");
34    }
35}