15 releases

0.3.0-beta.0 Aug 8, 2024
0.2.1 Mar 6, 2023
0.2.0 Feb 1, 2023
0.1.0 Dec 28, 2022
0.0.7 Nov 22, 2020

#227 in Encoding

Download history 684/week @ 2024-08-19 504/week @ 2024-08-26 361/week @ 2024-09-02 226/week @ 2024-09-09 339/week @ 2024-09-16 618/week @ 2024-09-23 464/week @ 2024-09-30 458/week @ 2024-10-07 587/week @ 2024-10-14 754/week @ 2024-10-21 1020/week @ 2024-10-28 952/week @ 2024-11-04 587/week @ 2024-11-11 619/week @ 2024-11-18 732/week @ 2024-11-25 696/week @ 2024-12-02

2,645 downloads per month
Used in 7 crates (6 directly)

MIT/Apache

90KB
2K SLoC

Serde-querystring



A query string parser for rust with support for different parsing methods.

Install

# Cargo.toml
[dependencies]
serde-querystring = "0.3.0-beta.0"

Usage

You can use the parsers provided in this crate directly, examples are available in each parser's tests.

use serde_querystring::DuplicateQS;

let parsed = DuplicateQS::parse(b"foo=bar&foo=baz");
let values = parsed.values(b"foo"); // Will give you a vector of b"bar" and b"baz"

Or you can use serde(with serde feature, enabled by default)

use serde::Deserialize;
use serde_querystring::{from_str, ParseMode, DuplicateQS};

#[derive(Deserialize)]
struct MyStruct{
  foo: Vec<String> // Or (String, u32) tuple
}

let parsed: MyStruct = from_str("foo=bar&foo=2022", ParseMode::Duplicate).unwrap();
// or
let parsed: MyStruct = DuplicateQS::parse(b"foo=bar&foo=baz").deserialize().unwrap();

There are also crates for actix_web(serde-querystring-actix) and axum(serde-querystring-axum) which provide extractors for their frameworks and can be used without directly relying on the core crate.

Parsers

Simple Mode

Simply parses key=value pairs, accepting only one value per key. In case a key is repeated, we only collect the last value.

use serde_querystring::{UrlEncodedQS, ParseMode, from_str};

UrlEncodedQS::parse(b"key=value");
// or
let res: MyStruct = from_str("foo=bar&key=value", ParseMode::UrlEncoded).unwrap();

Repeated key mode

Supports vectors or values by repeating a key.

use serde_querystring::{DuplicateQS, ParseMode, from_str};

DuplicateQS::parse(b"foo=bar&foo=bar2&foo=bar3");
// or
let res: MyStruct = from_str("foo=bar&foo=bar2&foo=bar3", ParseMode::Duplicate).unwrap();

Delimiter mode

Supports vectors or values by using a delimiter byte(ex. b'|').

use serde_querystring::{DelimiterQS, ParseMode, from_str};

DelimiterQS::parse(b"foo=bar|bar2|bar3", b'|');
// or
let res: MyStruct = from_str("foo=bar|bar2|bar3", ParseMode::Delimiter(b'|')).unwrap();

Brackets mode

Supports vectors or values by using a brackets and subkeys.

use serde_querystring::{BracketsQS, ParseMode, from_str};

BracketsQS::parse(b"foo[1]=bar&foo[2]=bar&foo[3]=bar");
// or
let res: MyStruct = from_str("foo[1]=bar&foo[2]=bar&foo[3]=bar", ParseMode::Brackets).unwrap();

Credit

We use some lines of code from form_urlencoded to parse percent encoded chars.

License

This project is licensed under either of

at your option.

Dependencies

~120–340KB