ResultExt

Trait ResultExt 

Source
pub trait ResultExt<T>
where Self: Sized,
{ // Required method fn status<S>(self, status: S) -> Result<T> where S: TryInto<StatusCode>, S::Error: Debug; }
Expand description

Extension trait that adds HTTP status code handling to Result and Option types.

This trait provides a convenient status method that allows you to associate an HTTP status code with errors when converting them to the HTTP toolkit’s Result type.

§Examples

use http_kit::{ResultExt, Result};
use http::StatusCode;
use std::fs;

fn read_config() -> Result<String> {
    fs::read_to_string("config.txt")
        .status(StatusCode::NOT_FOUND)
}

fn get_user_id() -> Result<u32> {
    Some(42_u32)
        .status(StatusCode::BAD_REQUEST)
}

Required Methods§

Source

fn status<S>(self, status: S) -> Result<T>
where S: TryInto<StatusCode>, S::Error: Debug,

Associates an HTTP status code with an error or None value.

For Result types, this wraps any error with the specified status code. For Option types, this converts None to an error with the specified status code.

§Arguments
  • status - HTTP status code to associate with the error
§Examples
use http_kit::{ResultExt, Result};
use http::StatusCode;
use std::fs;

// With Result
let result: Result<String> = fs::read_to_string("missing.txt")
    .status(StatusCode::NOT_FOUND);

// With Option
let result: Result<i32> = None
    .status(StatusCode::BAD_REQUEST);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> ResultExt<T> for Option<T>

Source§

fn status<S>(self, status: S) -> Result<T>
where S: TryInto<StatusCode>, S::Error: Debug,

Source§

impl<T, E> ResultExt<T> for Result<T, E>
where E: Error + Send + Sync + 'static,

Source§

fn status<S>(self, status: S) -> Result<T>
where S: TryInto<StatusCode>, S::Error: Debug,

Implementors§