Skip to content

Commit

Permalink
Introduces the FromQuery and IntoQuery traits.
Browse files Browse the repository at this point in the history
These two traits are introduced to allow for customizing the query
encoding and decoding behaviour by implementing these traits for custom
types. The previous behaviour of using serde_urlencoded for anything
that implements Serialize and Deserialize is preserved. Customt wrapper
types are added which modify the encoding and decoding behaviour. An
optional feature named `query-qs` is introduced which allows for
optionally encoding and decoding query strings using serde_qs.
  • Loading branch information
xfbs committed Aug 8, 2023
1 parent f5f963a commit cc2faf0
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 114 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### Next Version

- Migrate to Edition 2021 and Apply MSRV in Cargo.toml (#360)
- Introduces the `FromQuery` and `IntoQuery` traits to allow for customizing
how query strings are encoded and decoded in `gloo_history`.

### Version "0.2.3"

Expand Down
34 changes: 17 additions & 17 deletions crates/history/src/any.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use std::borrow::Cow;

#[cfg(feature = "query")]
use serde::Serialize;
use crate::{error::HistoryResult, query::IntoQuery};

use crate::browser::BrowserHistory;
#[cfg(feature = "query")]
use crate::error::HistoryResult;
use crate::hash::HashHistory;
use crate::history::History;
use crate::listener::HistoryListener;
use crate::location::Location;
use crate::memory::MemoryHistory;
use crate::{
browser::BrowserHistory, hash::HashHistory, history::History, listener::HistoryListener,
location::Location, memory::MemoryHistory,
};

/// A [`History`] that provides a universal API to the underlying history type.
#[derive(Clone, PartialEq, Debug)]
Expand Down Expand Up @@ -79,9 +75,13 @@ impl History for AnyHistory {
}

#[cfg(feature = "query")]
fn push_with_query<'a, Q>(&self, route: impl Into<Cow<'a, str>>, query: Q) -> HistoryResult<()>
fn push_with_query<'a, Q>(
&self,
route: impl Into<Cow<'a, str>>,
query: Q,
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
{
match self {
Self::Browser(m) => m.push_with_query(route, query),
Expand All @@ -94,9 +94,9 @@ impl History for AnyHistory {
&self,
route: impl Into<Cow<'a, str>>,
query: Q,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
{
match self {
Self::Browser(m) => m.replace_with_query(route, query),
Expand All @@ -111,9 +111,9 @@ impl History for AnyHistory {
route: impl Into<Cow<'a, str>>,
query: Q,
state: T,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
T: 'static,
{
match self {
Expand All @@ -129,9 +129,9 @@ impl History for AnyHistory {
route: impl Into<Cow<'a, str>>,
query: Q,
state: T,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
T: 'static,
{
match self {
Expand Down
50 changes: 25 additions & 25 deletions crates/history/src/browser.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
use std::any::Any;
use std::borrow::Cow;
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use std::{any::Any, borrow::Cow, cell::RefCell, fmt, rc::Rc};

use gloo_events::EventListener;
use gloo_utils::window;
#[cfg(feature = "query")]
use serde::Serialize;
use wasm_bindgen::{JsValue, UnwrapThrowExt};
use web_sys::Url;

#[cfg(feature = "query")]
use crate::error::HistoryResult;
use crate::history::History;
use crate::listener::HistoryListener;
use crate::location::Location;
use crate::state::{HistoryState, StateMap};
use crate::utils::WeakCallback;
use crate::{error::HistoryResult, query::IntoQuery};
use crate::{
history::History,
listener::HistoryListener,
location::Location,
state::{HistoryState, StateMap},
utils::WeakCallback,
};

/// A [`History`] that is implemented with [`web_sys::History`] that provides native browser
/// history and state access.
Expand Down Expand Up @@ -109,12 +105,16 @@ impl History for BrowserHistory {
}

#[cfg(feature = "query")]
fn push_with_query<'a, Q>(&self, route: impl Into<Cow<'a, str>>, query: Q) -> HistoryResult<()>
fn push_with_query<'a, Q>(
&self,
route: impl Into<Cow<'a, str>>,
query: Q,
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
{
let route = route.into();
let query = serde_urlencoded::to_string(query)?;
let query = query.encode()?;

let url = Self::combine_url(&route, &query);

Expand All @@ -131,12 +131,12 @@ impl History for BrowserHistory {
&self,
route: impl Into<Cow<'a, str>>,
query: Q,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
{
let route = route.into();
let query = serde_urlencoded::to_string(query)?;
let query = query.encode()?;

let url = Self::combine_url(&route, &query);

Expand All @@ -154,9 +154,9 @@ impl History for BrowserHistory {
route: impl Into<Cow<'a, str>>,
query: Q,
state: T,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
T: 'static,
{
let (id, history_state) = Self::create_history_state();
Expand All @@ -165,7 +165,7 @@ impl History for BrowserHistory {
states.insert(id, Rc::new(state) as Rc<dyn Any>);

let route = route.into();
let query = serde_urlencoded::to_string(query)?;
let query = query.encode()?;

let url = Self::combine_url(&route, &query);

Expand All @@ -184,9 +184,9 @@ impl History for BrowserHistory {
route: impl Into<Cow<'a, str>>,
query: Q,
state: T,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
T: 'static,
{
let (id, history_state) = Self::create_history_state();
Expand All @@ -195,7 +195,7 @@ impl History for BrowserHistory {
states.insert(id, Rc::new(state) as Rc<dyn Any>);

let route = route.into();
let query = serde_urlencoded::to_string(query)?;
let query = query.encode()?;

let url = Self::combine_url(&route, &query);

Expand Down
2 changes: 1 addition & 1 deletion crates/history/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ pub enum HistoryError {
}

/// The Result type for History.
pub type HistoryResult<T> = std::result::Result<T, HistoryError>;
pub type HistoryResult<T, E = HistoryError> = std::result::Result<T, E>;
48 changes: 26 additions & 22 deletions crates/history/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use std::borrow::Cow;
use std::fmt;
use std::{borrow::Cow, fmt};

use gloo_utils::window;
#[cfg(feature = "query")]
use serde::Serialize;
use wasm_bindgen::UnwrapThrowExt;
use web_sys::Url;

use crate::browser::BrowserHistory;
#[cfg(feature = "query")]
use crate::error::HistoryResult;
use crate::history::History;
use crate::listener::HistoryListener;
use crate::location::Location;
use crate::utils::{assert_absolute_path, assert_no_query};
use crate::{error::HistoryResult, query::IntoQuery};

use crate::{
browser::BrowserHistory,
history::History,
listener::HistoryListener,
location::Location,
utils::{assert_absolute_path, assert_no_query},
};

/// A [`History`] that is implemented with [`web_sys::History`] and stores path in `#`(fragment).
///
Expand Down Expand Up @@ -95,11 +95,15 @@ impl History for HashHistory {
}

#[cfg(feature = "query")]
fn push_with_query<'a, Q>(&self, route: impl Into<Cow<'a, str>>, query: Q) -> HistoryResult<()>
fn push_with_query<'a, Q>(
&self,
route: impl Into<Cow<'a, str>>,
query: Q,
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
{
let query = serde_urlencoded::to_string(query)?;
let query = query.encode()?;
let route = route.into();

assert_absolute_path(&route);
Expand All @@ -116,11 +120,11 @@ impl History for HashHistory {
&self,
route: impl Into<Cow<'a, str>>,
query: Q,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
{
let query = serde_urlencoded::to_string(query)?;
let query = query.encode()?;
let route = route.into();

assert_absolute_path(&route);
Expand All @@ -139,9 +143,9 @@ impl History for HashHistory {
route: impl Into<Cow<'a, str>>,
query: Q,
state: T,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
T: 'static,
{
let route = route.into();
Expand All @@ -151,7 +155,7 @@ impl History for HashHistory {

let url = Self::get_url();

let query = serde_urlencoded::to_string(query)?;
let query = query.encode()?;
url.set_hash(&format!("{route}?{query}"));

self.inner.push_with_state(url.href(), state);
Expand All @@ -165,9 +169,9 @@ impl History for HashHistory {
route: impl Into<Cow<'a, str>>,
query: Q,
state: T,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
T: 'static,
{
let route = route.into();
Expand All @@ -177,7 +181,7 @@ impl History for HashHistory {

let url = Self::get_url();

let query = serde_urlencoded::to_string(query)?;
let query = query.encode()?;
url.set_hash(&format!("{route}?{query}"));

self.inner.replace_with_state(url.href(), state);
Expand Down
28 changes: 14 additions & 14 deletions crates/history/src/history.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::borrow::Cow;

#[cfg(feature = "query")]
use serde::Serialize;

#[cfg(feature = "query")]
use crate::error::HistoryResult;
use crate::listener::HistoryListener;
use crate::location::Location;
use crate::{error::HistoryResult, query::IntoQuery};
use crate::{listener::HistoryListener, location::Location};

/// A trait to provide [`History`] access.
///
Expand Down Expand Up @@ -56,19 +52,23 @@ pub trait History: Clone + PartialEq {

/// Same as `.push()` but affix the queries to the end of the route.
#[cfg(feature = "query")]
fn push_with_query<'a, Q>(&self, route: impl Into<Cow<'a, str>>, query: Q) -> HistoryResult<()>
fn push_with_query<'a, Q>(
&self,
route: impl Into<Cow<'a, str>>,
query: Q,
) -> HistoryResult<(), Q::Error>
where
Q: Serialize;
Q: IntoQuery;

/// Same as `.replace()` but affix the queries to the end of the route.
#[cfg(feature = "query")]
fn replace_with_query<'a, Q>(
&self,
route: impl Into<Cow<'a, str>>,
query: Q,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize;
Q: IntoQuery;

/// Same as `.push_with_state()` but affix the queries to the end of the route.
#[cfg(feature = "query")]
Expand All @@ -77,9 +77,9 @@ pub trait History: Clone + PartialEq {
route: impl Into<Cow<'a, str>>,
query: Q,
state: T,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
T: 'static;

/// Same as `.replace_with_state()` but affix the queries to the end of the route.
Expand All @@ -89,9 +89,9 @@ pub trait History: Clone + PartialEq {
route: impl Into<Cow<'a, str>>,
query: Q,
state: T,
) -> HistoryResult<()>
) -> HistoryResult<(), Q::Error>
where
Q: Serialize,
Q: IntoQuery,
T: 'static;

/// Creates a Listener that will be notified when current state changes.
Expand Down
2 changes: 2 additions & 0 deletions crates/history/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod history;
mod listener;
mod location;
mod memory;
#[cfg(feature = "query")]
pub mod query;
mod state;
mod utils;

Expand Down
Loading

0 comments on commit cc2faf0

Please sign in to comment.