Struct QString

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

A query string. Holds a list of (key,value).

Examples

Parameters can be get by their names.

let qs = qstring::QString::from("?foo=bar%20baz");
let foo = qs.get("foo").unwrap();
assert_eq!(foo, "bar baz");

Parameters not found are None.

let qs = qstring::QString::from("?foo=bar");
let foo = &qs.get("panda");
assert!(foo.is_none());

The query string can be assembled from pairs.

let qs = qstring::QString::new(vec![
   ("foo", "bar baz"),
   ("panda", "true"),
]);
assert_eq!(format!("{}", qs), "foo=bar%20baz&panda=true");

Implementations§

Source§

impl QString

Source

pub fn new<S, T>(params: Vec<(S, T)>) -> QString
where S: Into<String>, T: Into<String>,

Constructs a QString from a list of pairs.

let qs = qstring::QString::new(vec![
   ("foo", "bar baz"),
   ("panda", "true"),
]);
assert_eq!(format!("{}", qs), "foo=bar%20baz&panda=true");
Source

pub fn has(&self, name: &str) -> bool

Tells if a query parameter is present.

let qs = qstring::QString::from("?foo");
assert!(qs.has("foo"));
assert!(qs.get("foo").is_some());
Source

pub fn get<'a>(&'a self, name: &str) -> Option<&'a str>

Get a query parameter by name.

Empty query parameters (?foo) return ""

let qs = qstring::QString::from("?foo=bar");
let foo = qs.get("foo");
assert_eq!(foo, Some("bar"));
Source

pub fn into_pairs(self) -> Vec<(String, String)>

Converts the QString to list of pairs.

let qs = qstring::QString::from("?foo=bar&baz=boo");
let ps = qs.into_pairs();
assert_eq!(ps, vec![
    ("foo".to_string(), "bar".to_string()),
    ("baz".to_string(), "boo".to_string()),
]);
Source

pub fn to_pairs(&self) -> Vec<(&str, &str)>

Represent the QString as a list of pairs.

let qs = qstring::QString::from("?foo=bar&baz=boo");
let ps = qs.to_pairs();
assert_eq!(ps, vec![
    ("foo", "bar"),
    ("baz", "boo"),
]);
Source

pub fn add_pair<S, T>(&mut self, pair: (S, T))
where S: Into<String>, T: Into<String>,

Adds another query parameter pair.

let mut qs = qstring::QString::from("?foo=bar&baz=boo");

qs.add_pair(("panda", "bear"));

assert_eq!(qs.to_string(), "foo=bar&baz=boo&panda=bear");
Source

pub fn add_str(&mut self, origin: &str)

Parse the string and add all found parameters to this instance.

let mut qs = qstring::QString::from("?foo");

qs.add_str("&bar=baz&pooch&panda=bear");

assert_eq!(qs.to_string(), "foo&bar=baz&pooch&panda=bear");
Source

pub fn len(&self) -> usize

The number of query string pairs.

Source

pub fn is_empty(&self) -> bool

if this query string is empty.

Trait Implementations§

Source§

impl Clone for QString

Source§

fn clone(&self) -> QString

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 QString

Source§

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

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

impl Default for QString

Source§

fn default() -> QString

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

impl Display for QString

Source§

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

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

impl<'a> From<&'a str> for QString

Source§

fn from(origin: &str) -> Self

Constructs a new QString by parsing a query string part of the URL. Can start with ? or not, either works.

Examples

let qs = qstring::QString::from("?foo=bar");
let v: Vec<(String, String)> = qs.into_pairs();
assert_eq!(v, vec![("foo".to_string(), "bar".to_string())]);
Source§

impl Into<String> for QString

Source§

fn into(self) -> String

Converts this type into the (usually inferred) input type.
Source§

impl Into<Vec<(String, String)>> for QString

Source§

fn into(self) -> Vec<(String, String)>

Converts this type into the (usually inferred) input type.
Source§

impl IntoIterator for QString

Source§

type Item = (String, String)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<(String, String)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for QString

Source§

fn eq(&self, other: &QString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for QString

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.