From 53c748dca8bbdd868321ff237616c26f083d7784 Mon Sep 17 00:00:00 2001 From: Ben Lovy Date: Fri, 12 Apr 2024 12:35:36 -0400 Subject: [PATCH 1/2] fix(client/path): correct strip_prefix behavior --- packages/client/src/path.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/client/src/path.rs b/packages/client/src/path.rs index 6fae2b2db..02d136ec8 100644 --- a/packages/client/src/path.rs +++ b/packages/client/src/path.rs @@ -125,9 +125,13 @@ impl Path { #[must_use] pub fn strip_prefix(&self, prefix: &Self) -> Option { - self.string - .strip_prefix(prefix.as_str()) - .map(|string| string.parse().unwrap()) + self.string.strip_prefix(prefix.as_str()).map(|string| { + let mut string = string.to_string(); + if string.starts_with('/') { + string.remove(0); + } + string.parse().unwrap() + }) } #[must_use] @@ -297,4 +301,12 @@ mod tests { let path: Path = "./bar/baz".parse().unwrap(); assert_eq!(path.normalize().to_string(), "bar/baz"); } + + #[test] + fn strip_prefix() { + let path: Path = "/hello/world".parse().unwrap(); + let prefix: Path = "/hello".parse().unwrap(); + let expected: Path = "world".parse().unwrap(); + assert_eq!(path.strip_prefix(&prefix).unwrap(), expected); + } } From 73323795e32941270d1ef57dd10902aa6756dcf9 Mon Sep 17 00:00:00 2001 From: David Yamnitsky Date: Sat, 13 Apr 2024 11:29:09 -0400 Subject: [PATCH 2/2] use components --- packages/client/src/path.rs | 41 +++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/packages/client/src/path.rs b/packages/client/src/path.rs index 02d136ec8..84340aae8 100644 --- a/packages/client/src/path.rs +++ b/packages/client/src/path.rs @@ -125,13 +125,23 @@ impl Path { #[must_use] pub fn strip_prefix(&self, prefix: &Self) -> Option { - self.string.strip_prefix(prefix.as_str()).map(|string| { - let mut string = string.to_string(); - if string.starts_with('/') { - string.remove(0); - } - string.parse().unwrap() - }) + if self + .components() + .iter() + .zip(prefix.components()) + .take_while(|(s, p)| s == p) + .count() < prefix.components().len() + { + None + } else { + Some( + self.components() + .iter() + .skip(prefix.components().len()) + .cloned() + .collect(), + ) + } } #[must_use] @@ -306,7 +316,20 @@ mod tests { fn strip_prefix() { let path: Path = "/hello/world".parse().unwrap(); let prefix: Path = "/hello".parse().unwrap(); - let expected: Path = "world".parse().unwrap(); - assert_eq!(path.strip_prefix(&prefix).unwrap(), expected); + let left = path.strip_prefix(&prefix); + let right = Some("world".parse().unwrap()); + assert_eq!(left, right); + + let path: Path = "/hello/world".parse().unwrap(); + let prefix: Path = "/world".parse().unwrap(); + let left = path.strip_prefix(&prefix); + let right = None; + assert_eq!(left, right); + + let path: Path = "/foo/bar".parse().unwrap(); + let prefix: Path = "/foo/bar/baz".parse().unwrap(); + let left = path.strip_prefix(&prefix); + let right = None; + assert_eq!(left, right); } }