Skip to main content

Query

Struct Query 

Source
pub struct Query;
Expand description

Zero-copy URL query string parser.

Provides high-performance parsing of URL query strings without allocating new strings for parameter names and values.

Can be used to parse form data (application/x-www-form-urlencoded), but there is currently no decoder support (%20, %40, etc.), due to zero-copy & zero-alloc. URL decoding may be added in future versions via optional features.

§Examples

use maker_web::query::Query;
use std::collections::HashMap;

// Parse into Vec (preserves order)
let query = b"name=john&age=25&city";
let vec_params: Vec<(&[u8], &[u8])> = Query::parse(query, 10).unwrap();
assert_eq!(vec_params.len(), 3);

// Parse into HashMap (deduplicates)
let hash_params: HashMap<&[u8], &[u8]> = Query::parse(query, 10).unwrap();
assert_eq!(hash_params.len(), 3);

// Handle limits
let result = Query::parse::<Vec<(&[u8], &[u8])>>(b"a=1&b=2", 1);
assert!(result.is_err()); // Exceeds limit of 1 parameter

All possible formats:

use maker_web::query::Query;

let query = b"debug&name=&=Qwe&key=sda&&";
let vec_params: Vec<(&[u8], &[u8])> = Query::parse(query, 10).unwrap();

assert_eq!(vec_params.len(), 5);
assert!(vec_params[0] == (b"debug", b""));
assert!(vec_params[1] == (b"name", b""));
assert!(vec_params[2] == (b"", b"Qwe"));
assert!(vec_params[3] == (b"key", b"sda"));
assert!(vec_params[4] == (b"", b""));
assert!(vec_params.get(5).is_none());

Implementations§

Source§

impl Query

Source

pub fn parse<'a, C: QueryCollector<'a>>( query: &'a [u8], limit: usize, ) -> Result<C, Error>

Parses a URL query string into a new collection.

Helper method for parsing query strings into custom collections. Used internally by Query::parse_into for flexible parameter handling.

§Type Parameters
§Arguments
  • query: Raw bytes of the query string (handles optional leading ? automatically, so ?a=1 and a=1 are equivalent)
  • limit: Maximum number of parameters to parse
§Examples
use maker_web::query::Query;
use std::collections::HashMap;

// Parse into Vec (preserves order)
let params: Vec<(&[u8], &[u8])> = Query::parse(b"name=john&age=25", 10).unwrap();
assert_eq!(params.len(), 2);

// Parse into HashMap (deduplicates keys)
let params: HashMap<&[u8], &[u8]> = Query::parse(b"key=1&key=2", 10).unwrap();
assert_eq!(params.len(), 1); // only last value remains

// Handle empty values and missing '='
let params: Vec<(&[u8], &[u8])> = Query::parse(b"flag&empty=", 10).unwrap();
assert!(params[0] == (b"flag", b""));
assert!(params[1] == (b"empty", b""));
Source

pub fn parse_into<'a, C: QueryCollector<'a>>( result: &mut C, query: &'a [u8], limit: usize, ) -> Result<(), Error>

Parses a URL query string into an existing collection.

This method allows reusing collection instances and provides more control over the parsing process.

§Type Parameters
§Arguments
  • result: Mutable reference to existing collection
  • query: Raw bytes of the query string
  • limit: Maximum number of parameters to parse
§Examples
use maker_web::query::Query;

// Reuse collection for multiple parses
let mut collector = Vec::new();

Query::parse_into(&mut collector, b"a=1&b=2", 10).unwrap();
assert_eq!(collector.len(), 2);

Query::parse_into(&mut collector, b"c=3&d=4", 10).unwrap();
assert_eq!(collector.len(), 4); // parameters are appended

// Handle limits
let mut collector = Vec::new();
let result = Query::parse_into(&mut collector, b"a=1&b=2&c=3", 2);
assert!(result.is_err()); // limit exceeded after 2 parameters

// Parse form data with URL-encoded values (no decoding)
let mut collector = Vec::new();
Query::parse_into(&mut collector, b"email=user%40example.com", 10).unwrap();
assert_eq!(collector[0].1, b"user%40example.com"); // raw bytes

Auto Trait Implementations§

§

impl Freeze for Query

§

impl RefUnwindSafe for Query

§

impl Send for Query

§

impl Sync for Query

§

impl Unpin for Query

§

impl UnsafeUnpin for Query

§

impl UnwindSafe for Query

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.