Struct QueryString

Source
pub struct QueryString { /* private fields */ }
Expand description

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

§Example

use query_string_builder::QueryString;

let qs = QueryString::dynamic()
            .with_value("q", "apple")
            .with_value("category", "fruits and vegetables");

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

Implementations§

Source§

impl QueryString

Source

pub fn simple() -> QueryStringSimple

Creates a new, empty query string builder.

§Example
use query_string_builder::QueryString;

let weight: &f32 = &99.9;

let qs = QueryString::simple()
            .with_value("q", "apple")
            .with_value("category", "fruits and vegetables")
            .with_opt_value("weight", Some(weight));

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

pub fn dynamic() -> Self

Creates a new, empty query string builder.

Source

pub fn with_value<K: ToString, V: ToString>(self, key: K, value: V) -> Self

Appends a key-value pair to the query string.

§Example
use query_string_builder::QueryString;

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

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_value<K: ToString, V: ToString>( self, key: K, value: Option<V>, ) -> Self

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

§Example
use query_string_builder::QueryString;

let qs = QueryString::dynamic()
            .with_opt_value("q", Some("🍎 apple"))
            .with_opt_value("f", None::<String>)
            .with_opt_value("category", Some("fruits and vegetables"))
            .with_opt_value("works", Some(true));

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: ToString, V: ToString>(&mut self, key: K, value: V) -> &Self

Appends a key-value pair to the query string.

§Example
use query_string_builder::QueryString;

let mut qs = QueryString::dynamic();
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: ToString, V: ToString>( &mut self, key: K, value: Option<V>, ) -> &Self

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

§Example
use query_string_builder::QueryString;

let mut qs = QueryString::dynamic();
qs.push_opt("q", None::<String>);
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)

Appends another query string builder’s values.

§Example
use query_string_builder::QueryString;

let mut qs = QueryString::dynamic().with_value("q", "apple");
let more = QueryString::dynamic().with_value("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) -> Self

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

§Example
use query_string_builder::QueryString;

let qs = QueryString::dynamic().with_value("q", "apple");
let more = QueryString::dynamic().with_value("q", "pear");

let qs = qs.append_into(more);

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

Trait Implementations§

Source§

impl Clone for QueryString

Source§

fn clone(&self) -> QueryString

Returns a copy of the value. Read more
1.0.0 · 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 Display for QueryString

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.