alt-art / genius-rs

Rust library that allows interact with Genius API
https://crates.io/crates/genius-rs
MIT License
7 stars 3 forks source link

Use `reqwest::Url` type to help validate input #8

Closed vrobweis closed 2 years ago

vrobweis commented 2 years ago

As of version 0.4.0, this crate will return the same kind of error reqwest::Error or panic for most methods of the Genius client. This mostly makes sense, given the most obvious way the methods can fail is if the underlying network requests fail. However, the methods can fail for reasons other than, say, no internet connection. They can fail if the input given to the methods is insufficient, and the methods currently have no form of input validation.

While on its face, this may seem like a nonissue, the reqwest crate itself doesn't give particularly helpful, typesafe errors in identifying when this happens. The error Kind of the error type will simply be the Kind::Builder variant, which does little to indicate what actually failed. Rust's type system gives us better ways of dealing with this.

A good start is to simply use the Url type, the one reqwest already uses, to validate input. This is already done within reqwest, but by introducing it at this level of abstraction, it will assist in dealing with these errors when they occur, or at the very least inform the error in a panic message what went wrong.

This PR starts with a commit that introduces the Url type to each relevant method, albeit it currently panics if the user's input cannot be successfully parsed. This can be changed in the future, i.e. by using map_err to map the parsing error to the same error type this crate uses, but with more descriptive information. Either way, it lends itself to indicating to crate consumers what went wrong.