Struct BracketsQS

Source
pub struct BracketsQS<'a> { /* private fields */ }
Expand description

A querystring parser with support for vectors/lists, maps and enums(for serde) by the use of brackets(like qs or PHP).

§Note

Keys are decoded when calling the parse method, but values are lazily decoded when you call the value method for their keys. Sub keys/Sub values(The part of the key after bracket opening) is visited when calling the sub_values method, to limit unnecessary allocations and parsing(and stack overflows from too many levels).

§Example

use serde_querystring::BracketsQS;

let slice = b"foo[bar]=baz&foo[bar]=buzz&foo[foobar]=qux&foo=bar";
let parser = BracketsQS::parse(slice);

// `values` method returns ALL the direct values as a vector.
assert_eq!(
    parser.values(b"foo"),
    Some(vec![Some("bar".as_bytes().into())])
);

// `sub_values` method can be used for maps and optionally returns a new `BracketsQS` struct
let foo_values = parser.sub_values(b"foo");
assert!(foo_values.is_some());

let foo_values = foo_values.unwrap();
assert_eq!(
    foo_values.values(b"bar"),
    Some(vec![
        Some("baz".as_bytes().into()),
        Some("buzz".as_bytes().into())
    ])
);

assert_eq!(
    foo_values.values(b"foobar"),
    Some(vec![Some("qux".as_bytes().into())])
)

Implementations§

Source§

impl<'a> BracketsQS<'a>

Source

pub fn deserialize<T: Deserialize<'a>>(self) -> Result<T, Error>

Deserialize the parsed slice into T

Source§

impl<'a> BracketsQS<'a>

Source

pub fn parse(slice: &'a [u8]) -> Self

Parse a slice of bytes into a BracketsQS

Source

pub fn keys(&self) -> Vec<&Cow<'a, [u8]>>

Returns a vector containing all the keys in querystring.

Source

pub fn sub_values(&self, key: &'a [u8]) -> Option<BracketsQS<'_>>

Parses all the subkeys for this key and optionally returns a new BracketsQS if the key exists

Source

pub fn values(&self, key: &'a [u8]) -> Option<Vec<Option<Cow<'a, [u8]>>>>

Returns a vector containing all the values assigned to a key.

It returns None if the key doesn’t exist in the querystring, the resulting vector may contain None if the key had assignments without a value, ex &key&

§Note

Percent decoding the value is done on-the-fly every time this function is called.

Source

pub fn value(&self, key: &'a [u8]) -> Option<Option<Cow<'a, [u8]>>>

Returns the last direct value assigned to a key.

It returns None if the key doesn’t exist in the querystring, and returns Some(None) if the last assignment to a key doesn’t have a value, ex "&key&"

§Note

Percent decoding the value is done on-the-fly every time this function is called.

Auto Trait Implementations§

§

impl<'a> Freeze for BracketsQS<'a>

§

impl<'a> RefUnwindSafe for BracketsQS<'a>

§

impl<'a> Send for BracketsQS<'a>

§

impl<'a> Sync for BracketsQS<'a>

§

impl<'a> Unpin for BracketsQS<'a>

§

impl<'a> UnwindSafe for BracketsQS<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.