Skip to content

edge-m/reqw

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reqw

⚠️ 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 of reqwest. 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.


How to use

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 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
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),
    }
}

What Reqw::est() does

Reqw::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:

  • ✅ Ok(res) where res.status().is_success() (e.g., 200 OK)
  • ✅ Ok(res) where res.status() is an error (e.g., 404, 500)
  • ❌ Err(e) for transport-level failures (e.g., timeout, DNS failure)

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:

  • ✅ Ok(res) → only if the response is a 2xx success
  • ❌ Err(reqw::Error::Http(reqwest::Response)) → if response is non-2xx
  • ❌ Err(reqw::Error::Transport(reqwest::Error)) → if the request itself failed (network error, timeout, etc.)

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.

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages