Cannot change path of Uri with less than two failure branches
まだ誰も着手していません。
評価
調査の方向性
まず、issue で説明されている既存の http::uri::Builder、Uri::into_parts、Parts、path_and_query API を読んでください。提案されている from_uri と from_parts の設計を比較し、そのうちどちらが既存のエラーモデルを維持し、builder API と一貫しているかを判断してください。未解決の代替案に基づく実装ではなく、合意されレビュー済みの API 設計が得られれば完了です。
索引モデルが issue の本文から書いたものです。
説明
I have a many scenarios where I have one Uri and want to change its path_and_query, but this doesn't seem possible without writing code that can yield at least two distinct errors.
This doesn't make sense; realistically, only one error can occur: that the path_and_query is invalid. Beyond that, the operation should be infallible.
Here's one approach that I tried:
// self.base_url is of type `Uri`
pub fn relative_uri(&self, path: &str) -> Result<Uri, XXX> {
let mut parts = self.base_url.clone().into_parts();
parts.path_and_query = Some(path.try_into().unwrap());
Uri::from_parts(parts).unwrap()
}
Because there's two possible error types here (http::uri::InvalidUriParts or http::uri::InvalidUri), I need to have an enum error type for this function that can be either of both variants (or Box it, or wrap it in some common type).
This makes my API a mess because now callers need to handle two distinct error types (or a container of various types) for an operation that, realistically, can only fail in a single way (the path_and_query supplied is invalid).
I tried using the Builder pattern, but the type returned by hyper::Uri::scheme cannot passed to http::uri::Builder::scheme, so I end up with a bunch of failure branches again.
These three approaches could work around the issue:
- Mutate an
Uriand change itspath_and_query, or - Create a new
Uriusing thePartsfrom another while changing only one part. - Allow creating a
builderpre-populated withPartsfrom an existing Uri. This would allow re-writing the above example in a way that only one error needs to be handled.
The first one has been mentioned in the past and it seems its undesirable, so I'll just ignore it. The second is tricky to design with a clean API. The third is probably the simplest, since the builder can be initialized with the parts from the source Uri.
I think something like this is quite viable:
impl Builder {
// ...
pub fn from_uri(uri: Uri) -> Self {
Builder {
parts: Ok(uri.into_parts()),
}
}
This could then be used as:
Builder::from_uri(my_uri)
.path_and_query(path)
.build()
.unwrap();
Note that there's only one failure branch (and one error type) that needs to be handled.
Though, perhaps the following is as simple, useful and a bit more correct:
impl Builder {
// ...
pub fn from_parts(parts: Parts) -> Self {
Builder {
parts: Ok(parts),
}
}
- 主要言語
- Rust
- スター
- 1.4k
- フォーク
- 385
- 平均マージ
- 1日 5時間
- マージ済み PR(30日)
- 2
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
hyperium/http のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 52/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 62/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 58/100
似ている issue
-
priority: medium
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
storytold/photocraft#8041 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
documentation good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
writerslogic/holographic-memory#96 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 63/100
メンテナーはふだん 1 日以内に返信
-
bug good first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
engine-rs P3 review-deferral wasm
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
maniator/verticopolis#1154 ·
メンテナーはふだん 1 日以内に返信