http icon indicating copy to clipboard operation
http copied to clipboard

add Uri#into_builder() to easily modify a part of Uri

Open gfx opened this issue 5 years ago • 3 comments

It is messy to modify a Uri instance with Uri::Builder. For example, if one wants to modify path_and_query, they should do something like this:

let uri: &Uri = req.uri();
let path_and_query: String = "...";
let parts = uri.clone().into_parts();
let modified_uri = Uri::builder()
    .scheme(parts.scheme.unwrap())
    .authority(parts.authority.unwrap())
    .path_and_query(path_and_query.as_str())
    .build()
    .unwrap();

With this PR, it will become:

let uri: &Uri = req.uri();
let path_and_query: String = "...";
let modified_uri = uri.into_builder()
    .path_and_query(path_and_query.as_str())
    .build()
    .unwrap();

It will let us focus on the concern.

What do you think of it?

gfx avatar Nov 25 '20 02:11 gfx

Unofficial review: this looks good to me; I think it's solving a real problem with a nicely compact API.

djc avatar Nov 25 '20 08:11 djc

Alternative design:
Builder::from_parts() / (impl From<Parts> for Builder) construction becomes Builder::from_parts(uri.to_parts()) / (Builder::from(uri.to_parts())).

Pros:

  • equally comprehensible
  • mirrors Uri::from_parts method
  • (permits parts.into() where Builder is passed to fn)

Cons:

  • uglier when chained
  • requires Builder import

robjtede avatar Apr 27 '22 02:04 robjtede

Came looking for this! Any chance we can get this reviewed by a maintainer?

reu avatar May 17 '22 16:05 reu