Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
skirsdeda committed Feb 18, 2024
1 parent 082c28a commit f867c63
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
8 changes: 2 additions & 6 deletions router/src/components/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,7 @@ fn redirect_route_for(route: &RouteDefinition) -> Option<RouteDefinition> {

let new_pattern = if add_slash {
// If we need to add a slash, we need to match on the path w/o it:
let mut path = route.path.clone();
path.pop();
path
route.path.trim_end_matches('/').to_string()
} else {
format!("{}/", route.path)
};
Expand All @@ -779,9 +777,7 @@ fn FixTrailingSlash(add_slash: bool) -> impl IntoView {
let path = if add_slash {
format!("{}/", route.path())
} else {
let mut path = route.path().to_string();
path.pop();
path
route.path().trim_end_matches('/').to_string()
};
let options = NavigateOptions {
replace: true,
Expand Down
32 changes: 14 additions & 18 deletions router/src/matching/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ impl Matcher {
Some((p, s)) => (p, Some(s.to_string())),
None => (path, None),
};
let segments = get_segments(pattern)
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>();
let segments: Vec<String> = get_segments(pattern);
let len = segments.len();
Self {
splat,
Expand All @@ -46,7 +43,7 @@ impl Matcher {

#[doc(hidden)]
pub fn test(&self, location: &str) -> Option<PathMatch> {
let loc_segments = get_segments(location);
let loc_segments: Vec<&str> = get_segments(location);

let loc_len = loc_segments.len();
let len_diff: i32 = loc_len as i32 - self.len as i32;
Expand Down Expand Up @@ -104,18 +101,17 @@ impl Matcher {
}
}

fn get_segments(pattern: &str) -> Vec<&str> {
// URL root paths "/" and "" are equivalent.
// Web servers (at least, Axum and Actix-Web) will send us a path of "/"
// even if we've routed "". Always treat these as equivalent:
if pattern == "/" {
return vec![];
}
pattern
fn get_segments<'a, S: From<&'a str>>(pattern: &'a str) -> Vec<S> {
// URL root paths ("/" and "") are equivalent and treated as 0-segment paths.
// non-root paths with trailing slashes get extra empty segment at the end.
// This makes sure that segment matching is trailing-slash sensitive.
let mut segments: Vec<S> = pattern
.split('/')
.enumerate()
// Only remove a leading slash, not trailing slashes:
.skip_while(|(i, part)| *i == 0 && part.is_empty())
.map(|(_, part)| part)
.collect()
.filter(|p| !p.is_empty())
.map(Into::into)
.collect();
if segments.len() > 0 && pattern.ends_with('/') {
segments.push("".into());
}
segments
}
4 changes: 2 additions & 2 deletions router/tests/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ cfg_if! {
Some(PathMatch {
path: "".into(),
params: params_map!(
"any" => "///"
"any" => ""
)
})
);
Expand All @@ -148,7 +148,7 @@ cfg_if! {
Some(PathMatch {
path: "/foo/bar".into(),
params: params_map!(
"any" => "///"
"any" => ""
)
})
);
Expand Down

0 comments on commit f867c63

Please sign in to comment.