Cannot change path of Uri with less than two failure branches
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 35/100
Piste de recherche
Commencez par lire les API existantes http::uri::Builder, Uri::into_parts, Parts et path_and_query décrites dans l’issue. Comparez les conceptions proposées pour from_uri et from_parts, puis déterminez laquelle préserve le modèle d’erreur existant et est cohérente avec l’API de builder. Le travail est terminé lorsqu’une conception d’API approuvée et examinée est définie, et non lorsqu’une implémentation basée sur une alternative non résolue est produite.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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),
}
}
- Langage dominant
- Rust
- Étoiles
- 1.4k
- Forks
- 385
- Merge moyen
- 1 j 5 h
- PR mergées (30 j)
- 2
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de hyperium/http
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
-
Difficulté 3/5 1-2 jours Accessibilité débutants 52/100
-
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
-
Difficulté 3/5 1-2 jours Accessibilité débutants 62/100
-
Difficulté 3/5 1-2 jours Accessibilité débutants 58/100
Toutes les issues de hyperium/http
Issues similaires
-
Update webpki-root-certsOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
rustls/rustls-platform-verifier#262 · 2 commentaires · 2 réactions ·
-
needs-triage
Difficulté 2/5 1-3 heures Accessibilité débutants 73/100
bherbruck/solvecraft#4493 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 77/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
IngvarConsulting/unica#1911 ·
Les mainteneurs répondent en général sous 1 jour