Struct query_string_builder::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::new()
            .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 new() -> Self

Creates a new, empty query string builder.

source

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

Appends a key-value pair to the query string.

§Example
use query_string_builder::QueryString;

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

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

pub fn with_opt_value<K: Into<String>, V: Into<String>>( 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::new()
            .with_opt_value("q", Some("🍎 apple"))
            .with_opt_value("f", None::<String>)
            .with_opt_value("category", Some("fruits and vegetables"));

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

pub fn push<K: Into<String>, V: Into<String>>( &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::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: Into<String>, V: Into<String>>( &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::new();
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::new().with_value("q", "apple");
let more = QueryString::new().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::new().with_value("q", "apple");
let more = QueryString::new().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 Default for QueryString

source§

fn default() -> QueryString

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§

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> ToOwned for T
where T: Clone,

§

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§

default 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>,

§

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>,

§

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.