From bca1d4444e13f28bd8db065e328d85ae91278787 Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Sun, 23 Jan 2022 17:36:57 -0800 Subject: [PATCH] change the spec for the greater good Signed-off-by: Erik Hollensbe --- src/path.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/path.rs b/src/path.rs index cc31f85..baca788 100644 --- a/src/path.rs +++ b/src/path.rs @@ -21,8 +21,13 @@ impl Ord for Path { impl Path { pub(crate) fn new(path: String) -> Self { let mut parts = Self::default(); + if !path.contains("/") { + return Self::default(); + } - for arg in path.split("/") { + let args = path.split("/"); + + for arg in args { if arg.starts_with(":") { // is param parts.push(RoutePart::Param(arg.trim_start_matches(":").to_string())); @@ -30,7 +35,6 @@ impl Path { // skip empties. this will push additional leaders if there is an duplicate slash // (e.g.: `//one/two`), which will fail on matching; we don't want to support this // syntax in the router. - parts.push(RoutePart::Leader); } else { // is not param parts.push(RoutePart::PathComponent(arg.to_string())); @@ -134,7 +138,7 @@ impl PartialEq for Path { impl Default for Path { fn default() -> Self { - Self(Vec::new()) + Self(vec![RoutePart::Leader]) } } @@ -168,7 +172,7 @@ mod tests { let path = Path::new("/abc/def/ghi".to_string()); assert!(path.matches("/abc/def/ghi".to_string())); - assert!(!path.matches("//abc/def/ghi".to_string())); + assert!(path.matches("//abc/def/ghi".to_string())); assert!(!path.matches("/def/ghi".to_string())); assert!(path.params().is_empty()); @@ -198,5 +202,8 @@ mod tests { ); assert_eq!(Path::default().to_string(), "/".to_string()); + + let path = Path::new("/".to_string()); + assert!(path.matches("/".to_string())); } }