clia_url_qs/from_tuple_array.rs
1use anyhow::Result;
2use serde::Serialize;
3
4/// Format an array of tuples to the URL query string.
5///
6/// ```rust
7/// let params = [("foo", "bar"), ("baz", "quux")];
8/// assert_eq!(clia_url_qs::from_tuple_array(¶ms).unwrap(), "foo=bar&baz=quux");
9/// ```
10///
11/// If input is invalid, return error message.
12pub fn from_tuple_array<T: Serialize + ?Sized>(arr: &T) -> Result<String> {
13 serde_urlencoded::to_string(arr).map_err(|e| e.into())
14}
15
16#[cfg(test)]
17mod tests {
18 use super::*;
19
20 #[test]
21 fn test_from_tuple_array() {
22 let params = [("foo", "bar"), ("baz", "quux")];
23 assert_eq!(from_tuple_array(¶ms).unwrap(), "foo=bar&baz=quux");
24 }
25}