Cannot change path of Uri with less than two failure branches
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
Inizia leggendo le API esistenti http::uri::Builder, Uri::into_parts, Parts e path_and_query descritte nell’issue. Confronta i design proposti per from_uri e from_parts, quindi determina quale preserva il modello di errore esistente ed è coerente con la builder API. Il lavoro è completato quando è disponibile un design dell’API concordato e revisionato, non un’implementazione basata su un’alternativa non risolta.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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),
}
}
- Lingua principale
- Rust
- Stelle
- 1.4k
- Fork
- 385
- Merge medio
- 1g 5h
- PR unite (30g)
- 2
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di hyperium/http
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 52/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 62/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 58/100
Tutte le issue di hyperium/http
Issue simili
-
bug needs triage P3
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
pbakaus/impeccable#1143 ·
I maintainer di solito rispondono entro 1 giorno
-
Bundled uv is never updated and trusts the compromised eMudhra certificatesForse già presa @FabienDanieau l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
foxglove/mcap#1907 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 66/100
I maintainer di solito rispondono entro 1 giorno