servo / rust-url

URL parser for Rust
https://docs.rs/url/
Apache License 2.0
1.31k stars 325 forks source link

Poping a path segment removes slash separator #912

Open dsherret opened 6 months ago

dsherret commented 6 months ago

Describe the bug

Given the following code:

fn pop_last_segment(mut url: Url) -> Url {
  if let Ok(mut segments) = url.path_segments_mut() {
    segments.pop();
  }
  url
}

I get the following input and output:

IN:  file:///bar/deno.json
OUT: file:///bar

I would have expected the following output instead with a trailing slash so that it remains a directory:

file:///bar/
Scripter17 commented 6 months ago

As I understand it, a URL path is basically a Vec<String> joined by and prefixed with "/", so this is the correct behavior

I assume something along the lines of if let Some(_) = segments.pop() {segments.push("");} should give what you want

dsherret commented 6 months ago

I ended up working around it, but it seems strange that this would be desired behaviour. For example instead of:

file:///some/path/to/file.js (file)
file:///some/path/to (file)
file:///some/path (file)
file:///some (file)

I would expect:

file:///some/path/to/file.js (file)
file:///some/path/to/ (dir)
file:///some/path/ (dir)
file:///some/ (dir)

Because when you're poping from a file url, you're going to the parent directory rather than a file in the grandparent directory with the same name as the parent directory.

Edit: Sorry, I accidentally hit some keyboard shortcut and closed the issue.