Skip to main content

QueryBuilder

Struct QueryBuilder 

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

Standalone query-string builder with typed Display values.

Produces key=value pairs separated by &, with form-encoding applied to both key and value. Use merge_into to append the query string to an existing URL.

§Examples

use api_bones::url::QueryBuilder;

let qs = QueryBuilder::new()
    .param("limit", 20u32)
    .param("sort", "desc")
    .build();
assert_eq!(qs, "limit=20&sort=desc");

Implementations§

Source§

impl QueryBuilder

Source

pub fn new() -> Self

Create an empty QueryBuilder.

Source

pub fn param(self, key: impl Into<String>, value: impl ToString) -> Self

Append a typed query parameter.

The value is converted to a string via Display.

§Examples
use api_bones::url::QueryBuilder;

let qs = QueryBuilder::new().param("active", true).build();
assert_eq!(qs, "active=true");
Source

pub fn maybe_param( self, key: impl Into<String>, value: Option<impl ToString>, ) -> Self

Append an optional parameter — skipped if value is None.

§Examples
use api_bones::url::QueryBuilder;

let qs = QueryBuilder::new()
    .param("a", 1u32)
    .maybe_param("b", None::<&str>)
    .build();
assert_eq!(qs, "a=1");
Source

pub fn build(&self) -> String

Build the query string (without leading ?).

Returns an empty string when no parameters have been added.

Source

pub fn merge_into(&self, url: &str) -> String

Append the query string to url, using ? if there is no existing query, or & if one already exists.

Returns url unchanged when there are no params.

§Examples
use api_bones::url::QueryBuilder;

let qs = QueryBuilder::new().param("page", 2u32);
assert_eq!(qs.merge_into("https://example.com"), "https://example.com?page=2");
assert_eq!(qs.merge_into("https://example.com?limit=20"), "https://example.com?limit=20&page=2");
Source

pub fn set(self, key: impl Into<String>, value: impl ToString) -> Self

Append a key=value pair — alias for param.

§Examples
use api_bones::url::QueryBuilder;

let qs = QueryBuilder::new().set("limit", 10u32).set("sort", "asc").build();
assert_eq!(qs, "limit=10&sort=asc");
Source

pub fn set_opt( self, key: impl Into<String>, value: Option<impl ToString>, ) -> Self

Append an optional key=value pair — skipped when value is None.

Alias for maybe_param.

§Examples
use api_bones::url::QueryBuilder;

let qs = QueryBuilder::new()
    .set("a", 1u32)
    .set_opt("b", None::<&str>)
    .set_opt("c", Some("yes"))
    .build();
assert_eq!(qs, "a=1&c=yes");
Source

pub fn extend_from_struct<T: Serialize>(self, value: &T) -> Result<Self, Error>

Flatten a serializable struct’s top-level fields as query parameters.

The struct is serialized to a JSON object; each field whose value is not null is appended as a key=value pair. Nested objects and arrays are serialized as their JSON representation.

Returns an error when value cannot be serialized or is not a JSON object.

§Examples
use api_bones::url::QueryBuilder;
use serde::Serialize;

#[derive(Serialize)]
struct Params {
    page: u32,
    sort: &'static str,
    filter: Option<&'static str>,
}

let params = Params { page: 2, sort: "desc", filter: None };
let qs = QueryBuilder::new()
    .extend_from_struct(&params)
    .unwrap()
    .build();
assert_eq!(qs, "page=2&sort=desc");
Source

pub fn merge_into_url(&self, url: &str) -> String

Append the query string to url — alias for merge_into.

Uses ? if the URL has no existing query string, or & otherwise. Returns url unchanged when there are no params.

§Examples
use api_bones::url::QueryBuilder;

let qs = QueryBuilder::new().set("page", 3u32);
assert_eq!(qs.merge_into_url("https://api.example.com/items"), "https://api.example.com/items?page=3");
assert_eq!(qs.merge_into_url("https://api.example.com/items?limit=10"), "https://api.example.com/items?limit=10&page=3");
Source

pub fn is_empty(&self) -> bool

Return true when no parameters have been added.

Trait Implementations§

Source§

impl Clone for QueryBuilder

Source§

fn clone(&self) -> QueryBuilder

Returns a duplicate 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 QueryBuilder

Source§

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

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

impl Default for QueryBuilder

Source§

fn default() -> QueryBuilder

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

impl<'de> Deserialize<'de> for QueryBuilder

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for QueryBuilder

Source§

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

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

impl Serialize for QueryBuilder

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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.
Source§

impl<T> ValidateIp for T
where T: ToString,

Source§

fn validate_ipv4(&self) -> bool

Validates whether the given string is an IP V4
Source§

fn validate_ipv6(&self) -> bool

Validates whether the given string is an IP V6
Source§

fn validate_ip(&self) -> bool

Validates whether the given string is an IP
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,