Module gloo::history::query

source ·
Available on crate features history and query only.
Expand description

Encoding and decoding strategies for query strings.

There are various strategies to map Rust types into HTTP query strings. The FromQuery and ToQuery encode the logic for how this encoding and decoding is performed. These traits are public as a form of dependency inversion, so that you can override the decoding and encoding strategy being used.

These traits are used by the History trait, which allows for modifying the history state, and the Location struct, which allows for extracting the current location (and this query).

Default Strategy

By default, any Rust type that implements Serialize or Deserialize has an implementation of ToQuery or FromQuery, respectively. This implementation uses the serde_urlencoded crate, which implements a standards-compliant x-www-form-urlencoded encoder and decoder. Some patterns are not supported by this crate, for example it is not possible to serialize arrays at the moment. If this is an issue for you, consider using the serde_qs crate.

Example:

use serde::{Serialize, Deserialize};
use gloo_history::{MemoryHistory, History};

#[derive(Serialize)]
struct Query {
    name: String,
}

let query = Query {
    name: "user".into(),
};

let history = MemoryHistory::new();
history.push_with_query("index.html", &query).unwrap();

Custom Strategy

If desired, the FromQuery and ToQuery traits can also be manually implemented on types to customize the encoding and decoding strategies. See the documentation for these traits for more detail on how this can be done.

Structs

  • Encoding for raw query strings.

Traits

  • Type that can be decoded from a query string.
  • Type that can be encoded into a query string.