Skip to main content

QueryString

Struct QueryString 

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

A zero-allocation query string builder for percent encoding key-value pairs.

This builder borrows all keys and values. Building performs a single Vec allocation for the pair list; rendering percent-encodes each value on the fly without allocating intermediate strings.

If you need a builder without a lifetime parameter — e.g. to store it in a struct or return it from a function that owns the values — use QueryStringOwned or convert via into_owned.

§Example

use query_string_builder::QueryString;

let tasty = true;
let qs = QueryString::new()
    .with("q", "apple")
    .with("tasty", &tasty)
    .with_opt("category", Some("fruits and vegetables"));

assert_eq!(
    format!("https://example.com/{qs}"),
    "https://example.com/?q=apple&tasty=true&category=fruits%20and%20vegetables"
);

§Borrowing footgun

Because the builder borrows its values, temporaries created inline do not live long enough — bind them to a variable first:

use query_string_builder::QueryString;

let qs = QueryString::new().with("answer", &42.to_string()); // temporary dropped here
println!("{qs}");
use query_string_builder::QueryString;

let answer = 42.to_string();
let qs = QueryString::new().with("answer", &answer);
assert_eq!(qs.to_string(), "?answer=42");

Implementations§

Source§

impl<'a> QueryString<'a>

Source

pub fn new() -> Self

Creates a new, empty query string builder.

Source

pub fn with<K, V>(self, key: K, value: V) -> Self
where K: IntoPart<'a>, V: IntoPart<'a>,

Appends a key-value pair to the query string.

§Example
use query_string_builder::QueryString;

let answer = 42;
let qs = QueryString::new()
    .with("q", "🍎 apple")
    .with("category", "fruits and vegetables")
    .with("answer", &answer);

assert_eq!(
    format!("https://example.com/{qs}"),
    "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables&answer=42"
);
Source

pub fn with_opt<K, V>(self, key: K, value: Option<V>) -> Self
where K: IntoPart<'a>, V: IntoPart<'a>,

Appends a key-value pair to the query string if the value exists.

§Example
use query_string_builder::QueryString;

let works = true;
let qs = QueryString::new()
    .with_opt("q", Some("🍎 apple"))
    .with_opt("f", None::<&str>)
    .with_opt("category", Some("fruits and vegetables"))
    .with_opt("works", Some(&works));

assert_eq!(
    format!("https://example.com/{qs}"),
    "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables&works=true"
);
Source

pub fn push<K, V>(&mut self, key: K, value: V) -> &mut Self
where K: IntoPart<'a>, V: IntoPart<'a>,

Appends a key-value pair to the query string.

§Example
use query_string_builder::QueryString;

let mut qs = QueryString::new();
qs.push("q", "apple");
qs.push("category", "fruits and vegetables");

assert_eq!(
    format!("https://example.com/{qs}"),
    "https://example.com/?q=apple&category=fruits%20and%20vegetables"
);
Source

pub fn push_opt<K, V>(&mut self, key: K, value: Option<V>) -> &mut Self
where K: IntoPart<'a>, V: IntoPart<'a>,

Appends a key-value pair to the query string if the value exists.

§Example
use query_string_builder::QueryString;

let mut qs = QueryString::new();
qs.push_opt("q", None::<&str>);
qs.push_opt("q", Some("🍎 apple"));

assert_eq!(
    format!("https://example.com/{qs}"),
    "https://example.com/?q=%F0%9F%8D%8E%20apple"
);
Source

pub fn len(&self) -> usize

Determines the number of key-value pairs currently in the builder.

Source

pub fn is_empty(&self) -> bool

Determines if the builder is currently empty.

Source

pub fn append(&mut self, other: QueryString<'a>)

Appends another query string builder’s values.

§Example
use query_string_builder::QueryString;

let mut qs = QueryString::new().with("q", "apple");
let more = QueryString::new().with("q", "pear");

qs.append(more);

assert_eq!(
    format!("https://example.com/{qs}"),
    "https://example.com/?q=apple&q=pear"
);
Source

pub fn append_into(self, other: QueryString<'a>) -> Self

Appends another query string builder’s values, consuming both types.

§Example
use query_string_builder::QueryString;

let qs = QueryString::new().with("q", "apple");
let more = QueryString::new().with("q", "pear");

let qs = qs.append_into(more);

assert_eq!(
    format!("https://example.com/{qs}"),
    "https://example.com/?q=apple&q=pear"
);
Source

pub fn into_owned(self) -> QueryStringOwned

Converts this borrowing builder into a QueryStringOwned by rendering each key and value to an owned String.

Useful for building cheaply with borrows and then storing or returning the result past the borrows’ lifetimes.

§Example
use query_string_builder::{QueryString, QueryStringOwned};

let qs: QueryStringOwned = {
    let q = String::from("apple");
    QueryString::new().with("q", &q).into_owned()
};

assert_eq!(qs.to_string(), "?q=apple");

Trait Implementations§

Source§

impl<'a> Clone for QueryString<'a>

Source§

fn clone(&self) -> QueryString<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for QueryString<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for QueryString<'a>

Source§

fn default() -> QueryString<'a>

Returns the “default value” for a type. Read more
Source§

impl Display for QueryString<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for QueryString<'a>

§

impl<'a> !Send for QueryString<'a>

§

impl<'a> !Sync for QueryString<'a>

§

impl<'a> !UnwindSafe for QueryString<'a>

§

impl<'a> Freeze for QueryString<'a>

§

impl<'a> Unpin for QueryString<'a>

§

impl<'a> UnsafeUnpin for QueryString<'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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.