servo / rust-url

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

Feature request: provide separate struct for URL which is can-be-base #909

Open kriomant opened 4 months ago

kriomant commented 4 months ago

Suppose you write client for some REST service:

struct Client { url: Url }
impl Client {
    fn get_smth(&self) -> Result<Smth> {
        let mut url = self.url.clone();
        url.path_segments_mut().unwrap()  // <---- HERE
            .push("segment");
    }
}

The problem is that almost every client method would have to manipulate URL, e.g. add path segments, but these methods may return error for 'cannot-be-base' URLs. So you either have to propagate this error or use unwrap, both ways are so-so.

It would be convenient to have separate type for URLs which are always can-be-base. You convert URL to this BaseURL once, pass it to client and client is sure that URL-manipulation methods are infallible.