A Rust library handling http status codes.
No dependencies.
- The library uses the official list from Mozilla's reference on HTTP Status codes
- For each code, the library provides a public const consistent with Python enums for HTTP Status code
The to_string() function returns the String value of the code.
HTTP status codes are grouped into five classes:
- Informational responses (100–199): The request was received, continuing process
- Successful responses (200–299): The request was successfully received, understood, and accepted
- Redirection messages (300–399): Further action needs to be taken to complete the request
- Client error responses (400–499): The request contains bad syntax or cannot be fulfilled
- Server error responses (500–599): The server failed to fulfill an apparently valid request
The classify function in the httpstatus crate can determine which class a status code belongs to.
To use this library in your Rust project, add it to your Cargo.toml:
[dependencies]
httpstatus2 = "0.1"You can use the HTTP status code constants directly:
use httpstatus2::{OK, NOT_FOUND, INTERNAL_SERVER_ERROR};
fn main() {
println!("OK code: {}", OK);
println!("Not Found code: {}", NOT_FOUND);
println!("Internal Server Error code: {}", INTERNAL_SERVER_ERROR);
}The classify function determines the class of a status code:
use httpstatus2::{classify, StatusClass};
fn main() {
match classify(200) {
StatusClass::Successful => println!("Request was successful"),
StatusClass::ClientError => println!("Client error occurred"),
StatusClass::ServerError => println!("Server error occurred"),
_ => println!("Other status"),
}
}The to_string function returns the standard message for a status code:
use httpstatus2::to_string;
fn main() {
let status_map = to_string();
if let Some(message) = status_map.get(&200) {
println!("Status 200: {}", message);
}
}- Unlike httpstatus or http, httpstatus2 does not add dependencies beside the standard rust library
- tests are found directly in the lib.rs source code file.
Apache 2.0 or MIT