Skip to content

Commit 718cd29

Browse files
authored
Introduces the FromQuery and IntoQuery traits (#3565)
* Introduces the FromQuery and IntoQuery traits * Added documentation
1 parent 3b5c891 commit 718cd29

3 files changed

Lines changed: 20 additions & 17 deletions

File tree

packages/yew-router/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ pub mod history {
9090
};
9191
}
9292

93+
pub mod query {
94+
//! A module that provides custom query serialization & deserialization.
95+
96+
pub use gloo::history::query::{FromQuery, Raw, ToQuery};
97+
}
98+
9399
pub mod prelude {
94100
//! Prelude module to be imported when working with `yew-router`.
95101
//!

packages/yew-router/src/navigator.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::borrow::Cow;
22

3-
use serde::Serialize;
4-
53
use crate::history::{AnyHistory, History, HistoryError, HistoryResult};
4+
use crate::query::ToQuery;
65
use crate::routable::Routable;
76

87
pub type NavigationError = HistoryError;
@@ -93,20 +92,20 @@ impl Navigator {
9392
}
9493

9594
/// Same as `.push()` but affix the queries to the end of the route.
96-
pub fn push_with_query<R, Q>(&self, route: &R, query: &Q) -> NavigationResult<()>
95+
pub fn push_with_query<R, Q>(&self, route: &R, query: Q) -> Result<(), Q::Error>
9796
where
9897
R: Routable,
99-
Q: Serialize,
98+
Q: ToQuery,
10099
{
101100
self.inner
102101
.push_with_query(self.prefix_basename(&route.to_path()), query)
103102
}
104103

105104
/// Same as `.replace()` but affix the queries to the end of the route.
106-
pub fn replace_with_query<R, Q>(&self, route: &R, query: &Q) -> NavigationResult<()>
105+
pub fn replace_with_query<R, Q>(&self, route: &R, query: Q) -> Result<(), Q::Error>
107106
where
108107
R: Routable,
109-
Q: Serialize,
108+
Q: ToQuery,
110109
{
111110
self.inner
112111
.replace_with_query(self.prefix_basename(&route.to_path()), query)
@@ -116,12 +115,12 @@ impl Navigator {
116115
pub fn push_with_query_and_state<R, Q, T>(
117116
&self,
118117
route: &R,
119-
query: &Q,
118+
query: Q,
120119
state: T,
121-
) -> NavigationResult<()>
120+
) -> Result<(), Q::Error>
122121
where
123122
R: Routable,
124-
Q: Serialize,
123+
Q: ToQuery,
125124
T: 'static,
126125
{
127126
self.inner
@@ -132,12 +131,12 @@ impl Navigator {
132131
pub fn replace_with_query_and_state<R, Q, T>(
133132
&self,
134133
route: &R,
135-
query: &Q,
134+
query: Q,
136135
state: T,
137-
) -> NavigationResult<()>
136+
) -> Result<(), Q::Error>
138137
where
139138
R: Routable,
140-
Q: Serialize,
139+
Q: ToQuery,
141140
T: 'static,
142141
{
143142
self.inner.replace_with_query_and_state(

website/docs/concepts/router.mdx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,12 @@ fn create(ctx: &Context<Self>) -> Self {
348348
#### Specifying query parameters when navigating
349349

350350
In order to specify query parameters when navigating to a new route, use either `navigator.push_with_query` or
351-
the `navigator.replace_with_query` functions. It uses `serde` to serialize the parameters into a query string for the URL so
352-
any type that implements `Serialize` can be passed. In its simplest form, this is just a `HashMap` containing string
353-
pairs.
351+
the `navigator.replace_with_query` functions. It uses the `ToQuery` trait to serialize the parameters into a query string for the URL. The `ToQuery` trait is automatically implemented for `serde` so any type that implements `Serialize` can be passed. In its simplest form, this is just a `HashMap` containing string pairs. In more complex scenarios the `ToQuery` trait can be implemented manually for a custom query format.
354352

355353
#### Obtaining query parameters for the current route
356354

357-
`location.query` is used to obtain the query parameters. It uses `serde` to deserialize the parameters from the query string
358-
in the URL.
355+
`location.query` is used to obtain the query parameters. It uses the `FromQuery` trait to deserialize the parameters from the query string
356+
in the URL. The `FromQuery` trait is automatically implemented for `serde` so any type that implements `Deserialize` can be passed. If the URL is formatted in an custom way, a manual implementation of `FromQuery` can be used.
359357

360358
## Nested Router
361359

0 commit comments

Comments
 (0)