macro query-params-macro

A procedural macro for easy use of arbitrary structs as query parameters in Hyper

2 releases

0.0.4 Oct 18, 2023
0.0.3 Oct 18, 2023
0.0.2 Oct 17, 2023
Download history 14/week @ 2024-07-14 6/week @ 2024-07-21 47/week @ 2024-07-28 3/week @ 2024-08-04 8/week @ 2024-08-25 16/week @ 2024-09-01 1/week @ 2024-09-08 5/week @ 2024-09-15 45/week @ 2024-09-22 12/week @ 2024-09-29 35/week @ 2024-10-06 18/week @ 2024-10-13 7/week @ 2024-10-20

61 downloads per month
Used in 2 crates (via to-query-params)

MIT license

13KB
172 lines

to-query-params

A procedural macro and trait for converting arbitrary structs into Vec<(String, String)> for use as query parameters, originally intended for ease of use with the Hyper HTTP framework.

Query parameters can be url-encoded with the urlencoding crate by calling the to_encoded_params method, while the default to_query_params method does not url-encode parameters.

badge License: MIT

Usage:

use query_params::{ToQueryParams, QueryParams};

 // Eq and PartialEq are just for assertions
 #[derive(QueryParams, Debug, PartialEq, Eq)]
 struct ProductRequest {
     #[query(required)] // fields that aren't Option<T> must be marked as required
     id: i32,
     #[query(required, rename = "type")]
     product_type: String,
     min_price: Option<i32>,
     max_price: Option<i32>,
 }

 pub fn main() {
     let request = ProductRequest {
         id: 999,
         product_type: "accessory".to_string(),
         min_price: None,
         max_price: Some(100),
     };

     let expected = vec![
         ("id".into(), "999".into()), 
         ("type".into(), "accessory".into()), 
         ("max_price".into(), "100".into())
     ];
     
     let query_params = request.to_query_params();

     assert_eq!(expected, query_params);
 }

lib.rs:

QueryParams is a procedural macro for deriving a Hyper-centric representation of that struct as query parameters that can be easily appended to query parameters in the Hyper framework. This crate is only meant to be tested and re-exported by the QueryParams crate, and is not meant for direct consumption.

Dependencies

~265–720KB
~17K SLoC