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 parameterAll 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
impl Query
Sourcepub fn parse<'a, C: QueryCollector<'a>>(
query: &'a [u8],
limit: usize,
) -> Result<C, Error>
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
C: Collection type implementing QueryCollector
§Arguments
query: Raw bytes of the query string (handles optional leading?automatically, so?a=1anda=1are 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""));Sourcepub fn parse_into<'a, C: QueryCollector<'a>>(
result: &mut C,
query: &'a [u8],
limit: usize,
) -> Result<(), Error>
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
C: Collection type implementing QueryCollector
§Arguments
result: Mutable reference to existing collectionquery: Raw bytes of the query stringlimit: 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 bytesAuto 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more