Cannot change path of Uri with less than two failure branches
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 35/100
Rechercherichtung
Beginne damit, die im Issue beschriebenen vorhandenen APIs http::uri::Builder, Uri::into_parts, Parts und path_and_query zu lesen. Vergleiche die vorgeschlagenen Designs für from_uri und from_parts und bestimme anschließend, welches das bestehende Fehlermodell beibehält und mit der Builder-API konsistent ist. Als erledigt gilt die Aufgabe, wenn ein abgestimmtes und geprüftes API-Design vorliegt, nicht eine Implementierung auf Grundlage einer ungelösten Alternative.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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),
}
}
- Vorherrschende Sprache
- Rust
- Sterne
- 1.4k
- Forks
- 385
- Ø Merge
- 1 T. 5 Std.
- Gemergte PRs (30 T.)
- 2
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus hyperium/http
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 52/100
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 45/100
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 62/100
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 58/100
Ähnliche Issues
-
checkpoint/verify/rebuild_indexes on a thread holding a WriteTransaction deadlock instead of INT-001Offenbug known-issue
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
project-minigraf/minigraf#734 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
Maintainer antworten meist innerhalb von 4 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
Nimblesite/lspkit#26 ·
Maintainer antworten meist innerhalb von 1 Tag