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
impl QueryString
Sourcepub fn simple() -> QueryStringSimple
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"
);
Sourcepub fn with_value<K: ToString, V: ToString>(self, key: K, value: V) -> Self
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"
);
Sourcepub fn with_opt_value<K: ToString, V: ToString>(
self,
key: K,
value: Option<V>,
) -> Self
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"
);
Sourcepub fn push<K: ToString, V: ToString>(&mut self, key: K, value: V) -> &Self
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"
);
Sourcepub fn push_opt<K: ToString, V: ToString>(
&mut self,
key: K,
value: Option<V>,
) -> &Self
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"
);
Sourcepub fn append(&mut self, other: QueryString)
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"
);
Sourcepub fn append_into(self, other: QueryString) -> Self
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
impl Clone for QueryString
Source§fn clone(&self) -> QueryString
fn clone(&self) -> QueryString
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for QueryString
impl Debug for QueryString
Auto Trait Implementations§
impl Freeze for QueryString
impl RefUnwindSafe for QueryString
impl Send for QueryString
impl Sync for QueryString
impl Unpin for QueryString
impl UnwindSafe for QueryString
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