pub(crate) fn combine_group_path(prefix: &str, route_path: &str) -> (String, Option<String>) {
debug_assert!(
!prefix.ends_with("//"),
"combine_group_path: prefix must not end with multiple slashes (got {prefix:?})"
);
let stripped = prefix.strip_suffix('/').unwrap_or(prefix);
if route_path == "/" {
if stripped.is_empty() {
return ("/".to_string(), None);
}
let canonical = stripped.to_string();
let alternate = format!("{canonical}/");
return (canonical, Some(alternate));
}
if stripped.is_empty() {
return (route_path.to_string(), None);
}
(format!("{stripped}{route_path}"), None)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn combine_group_path_matrix() {
let cases: &[(&str, &str, &str, Option<&str>)] = &[
("", "/", "/", None),
("/", "/", "/", None),
("/", "/x", "/x", None),
("/api", "/", "/api", Some("/api/")),
("/api", "/x", "/api/x", None),
("/api/", "/x", "/api/x", None),
("/api/", "/", "/api", Some("/api/")),
("/s/{slug}", "/", "/s/{slug}", Some("/s/{slug}/")),
];
for (prefix, route, want_canon, want_alt) in cases {
let (canon, alt) = combine_group_path(prefix, route);
assert_eq!(&canon, want_canon, "prefix={prefix:?} route={route:?}");
assert_eq!(
alt.as_deref(),
*want_alt,
"prefix={prefix:?} route={route:?}"
);
}
}
}