Skip to content

Commit 3f03139

Browse files
deciduouslynitsky
andauthored
fix(client): correct strip_prefix (#127)
Co-authored-by: David Yamnitsky <[email protected]>
1 parent b5d99d1 commit 3f03139

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

packages/client/src/path.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,23 @@ impl Path {
125125

126126
#[must_use]
127127
pub fn strip_prefix(&self, prefix: &Self) -> Option<Self> {
128-
self.string
129-
.strip_prefix(prefix.as_str())
130-
.map(|string| string.parse().unwrap())
128+
if self
129+
.components()
130+
.iter()
131+
.zip(prefix.components())
132+
.take_while(|(s, p)| s == p)
133+
.count() < prefix.components().len()
134+
{
135+
None
136+
} else {
137+
Some(
138+
self.components()
139+
.iter()
140+
.skip(prefix.components().len())
141+
.cloned()
142+
.collect(),
143+
)
144+
}
131145
}
132146

133147
#[must_use]
@@ -303,4 +317,25 @@ mod tests {
303317
let path: Path = "./bar/baz".parse().unwrap();
304318
assert_eq!(path.normalize().to_string(), "bar/baz");
305319
}
320+
321+
#[test]
322+
fn strip_prefix() {
323+
let path: Path = "/hello/world".parse().unwrap();
324+
let prefix: Path = "/hello".parse().unwrap();
325+
let left = path.strip_prefix(&prefix);
326+
let right = Some("world".parse().unwrap());
327+
assert_eq!(left, right);
328+
329+
let path: Path = "/hello/world".parse().unwrap();
330+
let prefix: Path = "/world".parse().unwrap();
331+
let left = path.strip_prefix(&prefix);
332+
let right = None;
333+
assert_eq!(left, right);
334+
335+
let path: Path = "/foo/bar".parse().unwrap();
336+
let prefix: Path = "/foo/bar/baz".parse().unwrap();
337+
let left = path.strip_prefix(&prefix);
338+
let right = None;
339+
assert_eq!(left, right);
340+
}
306341
}

0 commit comments

Comments
 (0)