#derive #macro #http

macro query_params

Rust macro to automatically implement the serialization to http query parameters for arbitrary structs

1 unstable release

Uses old Rust 2015

0.1.0 Aug 19, 2017

#9 in #query-params

Download history 61/week @ 2023-11-02 90/week @ 2023-11-09 60/week @ 2023-11-16 28/week @ 2023-11-23 17/week @ 2023-11-30 28/week @ 2023-12-07 27/week @ 2023-12-14 26/week @ 2023-12-21 17/week @ 2023-12-28 20/week @ 2024-01-04 27/week @ 2024-01-11 24/week @ 2024-01-18 23/week @ 2024-01-25 10/week @ 2024-02-01 53/week @ 2024-02-08 35/week @ 2024-02-15

127 downloads per month
Used in 2 crates (via apify-client)

MIT license

9KB
83 lines

Build Status Rust version

QueryParams Derive

Rust custom derive to automatically implement serialization to http query params for arbitrary structs. A simple #[derive(QueryParams)] will generate a function to_query_params for your struct.

How it Works

#[macro_use]
extern crate query_params;

#[derive(QueryParams)]
struct PullRequestsParametersApi {
    page: i32,
    sort: bool,
    direction: String,
    state: Vec<String>,
    // .. other interesting fields ..
} 

fn main() {
    let pr = PullRequestsParametersApi {
        page: 2,
        sort: true,
        direction: "asc",
        state: vec!["open".to_string(), "closed".to_string()],
    }

    println!("{}", pr.to_query_params()); // => ?page=2&sort=true&direction=asc&state=open,closed
}

Get Started

It's as simple as two steps:

  1. Add query_params to your Cargo.toml
  • manually

  • or with cargo-edit:

    cargo add derive_builder

  1. Annotate your struct with #[derive(QueryParams)]

Disclaimer ❗

  • Tuple structs and unit structs are not supported as they have no field names.

Documentation

Detailed explaination of all features and tips for troubleshooting.

Contribution

Feel free to make a pull request 😃

Dependencies

~1.5MB
~40K SLoC