#macro-derive #http #derive #macro

macro query-params-derive

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

3 unstable releases

Uses old Rust 2015

0.1.1 Jan 11, 2020
0.1.0 Jan 11, 2020
0.0.1 Jan 11, 2020

#1956 in Development tools

MIT and GPL-3.0 licenses

12KB
150 lines

query-params-serialize

Build Status

Serde serializer for query parameters.


lib.rs:

Transform an arbitrary structs to a http query params

This crate generate a function for serialize the fields of an arbitrary structs into a http query params String by the usage of a procedural macro with custom derive. The query params String return has for purpose to be use with any rust client http lib.

Getting Start

Add query_params as a dependency to you Cargo.toml.

Overview

#[macro_use]
extern crate query_params;
extern crate query_params_trait;

use query_params_trait::QueryParams;

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

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

pr.query_params();

What that generate

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

// Code generate
impl PullRequestsParametersApi {
    fn query_params(&self) -> String {
        let mut buf = String::from("?");
        
        // Stuff to fill buf with the struct fields content
        
        return buf
    }
    // expect "?page=2&sort=true&direction=asc&state=open,closed" with the example above
}  

Dependencies

~1.5MB
~35K SLoC