rust_fetch/
utils.rs

1use std::{collections::HashMap, str::FromStr};
2
3use reqwest::header::{HeaderMap, HeaderName};
4
5use crate::{
6    error::{FetchError, FetchResult},
7    FetchHeaders,
8};
9
10/// A quick macro to generate a HashMap<String, String>
11///
12/// # Example 1
13/// ```rust
14///     // The type declaration is for the example only and is not needed.
15///     let style1: std::collections::HashMap<String, String> = rust_fetch::map_string!{item1 : "item", item2 : "item"};
16/// ```
17///
18/// # Example 2
19/// ```rust
20///     // The type declaration is for the example only and is not needed.
21///     let style2: std::collections::HashMap<String, String> = rust_fetch::map_string!{"item1" => "item", "item2" => "item"};
22/// ```
23///
24#[macro_export]
25macro_rules! map_string {
26    ($($key:ident : $value:expr),* $(,)?) => {{
27        #[allow(unused_mut)]
28        let mut h_map: ::std::collections::HashMap<String, String> = ::std::collections::HashMap::new();
29        $(
30            h_map.insert(stringify!($key).to_string(), $value.to_string());
31        )*
32        h_map
33    }};
34
35    ($($key:expr => $value:expr),* $(,)?) => {{
36        let mut h_map: ::std::collections::HashMap<String, String> = ::std::collections::HashMap::new();
37        $(
38            h_map.insert($key.to_string(), $value.to_string());
39        )*
40        h_map
41    }};
42}
43
44pub fn reqwest_headers_to_map(header: &HeaderMap) -> FetchResult<FetchHeaders> {
45    let mut to_return: FetchHeaders = HashMap::new();
46    for (key, value) in header {
47        to_return.insert(
48            key.to_string(),
49            value
50                .to_str()
51                .map_err(|e| FetchError::Unknown(anyhow::anyhow!(e)))?
52                .to_owned(),
53        );
54    }
55
56    return Ok(to_return);
57}
58
59pub fn map_to_reqwest_headers(map: &FetchHeaders) -> FetchResult<HeaderMap> {
60    let mut headers = reqwest::header::HeaderMap::new();
61    for (key, value) in map {
62        headers.insert(
63            HeaderName::from_str(key)
64                .map_err(|_| FetchError::HeaderParseError(key.to_owned(), value.to_owned()))?,
65            value
66                .parse()
67                .map_err(|_| FetchError::HeaderParseError(key.to_owned(), value.to_owned()))?,
68        );
69    }
70    Ok(headers)
71}
72
73#[cfg(test)]
74mod utils_tests {
75    use super::map_to_reqwest_headers;
76
77    #[test]
78    fn test_map_macro_ident() {
79        let map = map_string! {testas : "test", test2 : "test2"};
80
81        assert_eq!(2, map.len());
82    }
83
84    #[test]
85    fn test_map_macro_expr() {
86        let map = map_string! {"test" => "testing123", "test2" => "testing123"};
87
88        assert_eq!(2, map.len());
89    }
90
91    #[test]
92    fn test_no_args_macro() {
93        let map = map_string! {};
94        assert_eq!(0, map.len());
95    }
96
97    #[test]
98    fn test_map_to_reqwest_headers() {
99        let header_map = map_string! {
100            value1 : "value",
101            value2 : "value",
102            value3 : "value"
103        };
104        let headers = map_to_reqwest_headers(&header_map);
105        assert_ne!(true, headers.is_err());
106    }
107}
108