Crates.io | to-query-params |
lib.rs | to-query-params |
version | 0.0.4 |
source | src |
created_at | 2023-10-17 22:11:35.916341 |
updated_at | 2023-10-18 11:40:36.821658 |
description | A procedural macro and trait for easy use of arbitrary structs as query parameters in Hyper |
homepage | |
repository | https://github.com/Brendan-Blanchard/to-query-params |
max_upload_size | |
id | 1006173 |
size | 11,164 |
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.
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);
}