servo / rust-url

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

feature: expose pathname publicly #833

Open waynevanson opened 1 year ago

waynevanson commented 1 year ago

Hi! I enjoy this package, thank you for making it.

I would like to construct a pathname then pass it into the URL.

I see that PathSegmentsMut is available but looks like it's purposefully not exposed. Was there a reason for that?

Just like we can join paths in the standard PathBuf, I would like to see something similar here for urls.

valenting commented 1 year ago

I'm not sure I understand. You can call set_path, or use path_segments_mut to change the path of a URL https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=41051f540e7ac7828963c10cd8da4c1b

use url;
use url::Url;

fn main() {
    let mut url = Url::parse("http://example.com/path").unwrap();
    // https://docs.rs/url/2.3.1/url/struct.Url.html#method.set_path
    url.set_path("/some/other/path");
    println!("{:?}", url.as_str());

    // https://docs.rs/url/2.3.1/url/struct.PathSegmentsMut.html
    url.path_segments_mut().unwrap().pop().push("img");
    println!("{:?}", url.as_str());
}

Could you expand of what else you would need?

waynevanson commented 1 year ago

@valenting The API implies that the host must be defined and known ahead of time.

Instead, I would like PathSegmentsMut publicly exposed with pub keyword and exported out the main module.

lucacasonato commented 1 year ago

@waynevanson I don't understand. Can you create an example of what you'd like to be able to do?