Skip to content

fruafr/rust-httpstatus2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

httpstatus2

A Rust library handling http status codes.

No dependencies.

HTTP Status Code

The to_string() function returns the String value of the code.

HTTP Status Code Classification

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.

Usage

To use this library in your Rust project, add it to your Cargo.toml:

[dependencies]
httpstatus2 = "0.1"

Using Status Code Constants

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

Using the classify Function

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

Using the to_string Function

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

Dependencies

  • Unlike httpstatus or http, httpstatus2 does not add dependencies beside the standard rust library

Tests

  • tests are found directly in the lib.rs source code file.

Author

David Heurtevent - frua.fr

License

Apache 2.0 or MIT

About

A Rust library handling http status codes.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages