Url decode routes (#1407)

* fix a few new clippy lints

* url decode routes

* fix catch all segments

* fix clippy
This commit is contained in:
ealmloff authored and GitHub committed 2023-09-06 12:56:43 -05:00
1 parent e8ea228759
commit c8127e164b
7 files changed
+60 -13

No files matched your search

+5
View File
@@ -82,3 +82,8 @@ pub mod prelude {
mod utils {
pub(crate) mod use_router_internal;
}
#[doc(hidden)]
pub mod exports {
pub use urlencoding;
}
+36 -3
View File
@@ -34,7 +34,7 @@ pub trait FromQuery {
impl<T: for<'a> From<&'a str>> FromQuery for T {
fn from_query(query: &str) -> Self {
T::from(query)
T::from(&*urlencoding::decode(query).expect("Failed to decode url encoding"))
}
}
@@ -54,10 +54,22 @@ where
type Err = <T as FromStr>::Err;
fn from_route_segment(route: &str) -> Result<Self, Self::Err> {
T::from_str(route)
match urlencoding::decode(route) {
Ok(segment) => T::from_str(&segment),
Err(err) => {
log::error!("Failed to decode url encoding: {}", err);
T::from_str(route)
}
}
}
}
#[test]
fn full_circle() {
let route = "testing 1234 hello world";
assert_eq!(String::from_route_segment(route).unwrap(), route);
}
/// Something that can be converted to route segments
pub trait ToRouteSegments {
/// Display the route segments
@@ -71,12 +83,33 @@ where
fn display_route_segements(self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for segment in self {
write!(f, "/")?;
write!(f, "{}", segment)?;
let segment = segment.to_string();
match urlencoding::decode(&segment) {
Ok(segment) => write!(f, "{}", segment)?,
Err(err) => {
log::error!("Failed to decode url encoding: {}", err);
write!(f, "{}", segment)?
}
}
}
Ok(())
}
}
#[test]
fn to_route_segments() {
struct DisplaysRoute;
impl std::fmt::Display for DisplaysRoute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let segments = vec!["hello", "world"];
segments.display_route_segements(f)
}
}
assert_eq!(DisplaysRoute.to_string(), "/hello/world");
}
/// Something that can be created from route segments
pub trait FromRouteSegments: Sized {
/// The error that can occur when parsing route segments