| Crates.io | reqw |
| lib.rs | reqw |
| version | 0.1.0 |
| created_at | 2025-07-04 14:40:25.300431+00 |
| updated_at | 2025-07-04 14:40:25.300431+00 |
| description | A small helper to handle reqwest results more intuitively |
| homepage | https://github.com/edge-m/reqw |
| repository | https://github.com/edge-m/reqw |
| max_upload_size | |
| id | 1738077 |
| size | 59,394 |
⚠️ Warning: This crate is only published to provide documentation on docs.rs. It is not intended to be used via
cargo add. Please copy the code manually into your project.
Note: This is not a library — just a code snippet you can copy-paste into your project. Why? Because Rust treats each crate version as a unique type. If this helper were a real library and depended on
reqwest, it would only work when your project uses exactly the same version ofreqwest. Supporting every version would require releasing multiple versions of this crate — which is overkill for a tiny helper like this. So instead, it’s just a clean, dependency-free snippet you can drop into your own codebase.
Save the following code as reqw.rs somewhere in your project (e.g., in src/utils/reqw.rs):
pub enum Error {
Http(reqwest::Response),
Transport(reqwest::Error),
}
/// This function is re-estimates response of reqwest
///
/// - Returns `Ok(reqwest::Response)` if status code is 200-299
/// - Returns `Err(Error::Http)` if the status code is not 2xx
/// - Returns `Err(Error::Transport)` if there was a transport error
///
/// # Examples
///
/// ```rust
/// async fn run() {
/// let result = reqw::est(reqwest::get("https://example.com").await);
/// assert!(result.is_ok());
/// }
/// ```
pub fn est(result: Result<reqwest::Response, reqwest::Error>) -> Result<reqwest::Response, Error> {
match result {
Ok(r) if r.status().is_success() => Ok(r),
Ok(r) => Err(Error::Http(r)),
Err(e) => Err(Error::Transport(e)),
}
}
Then, use it like this:
mod utils;
use utils::reqw;
#[tokio::main]
async fn main() {
match reqw::est(reqwest::get("https://example.com").await) {
Ok(res) => println!("✅ Success! Status: {}", res.status()),
Err(reqw::Error::Http(res)) => eprintln!("❌ HTTP error: {}", res.status()),
Err(reqw::Error::Transport(err)) => eprintln!("💥 Transport error: {}", err),
}
}
Reqw::est() doesReqw::est() stands for "estimate" — it redefines how to interpret reqwest's Result<Response, Error>
by re-estimating the outcome of an HTTP request into a simpler, cleaner success/error model that better fits real-world application logic.
In reqwest, the result of an HTTP request looks like this:
Result<reqwest::Response, reqwest::Error>
This has three possible real-world meanings:
By default, reqwest treats all Ok(Response) as "success", even if the server returned a 500 Internal Server Error — because the HTTP protocol worked.
Reqw::est() reinterprets that like this:
Result<reqwest::Response, reqw::Error>
With:
This way, you can treat HTTP error responses in the same way as transport errors — making your control flow simpler and more idiomatic in Rust.