Trait highnoon::Responder

source ·
pub trait Responder {
    fn into_response(self) -> Result<Response>;
}
Expand description

This trait is implemented for all the common types you can return from an endpoint

It’s also implemented for Response and hyper::Response for compatibility. There is an implementation for Result<R> where R: Responder which allows fallible functions to be used as endpoints

use highnoon::{Request, Responder, Json, StatusCode};

fn example_1(_: Request<()>) -> impl Responder {
    // return status code
    StatusCode::NOT_FOUND
}

fn example_2(_: Request<()>) -> impl Responder {
    // return strings (&str or String)
    "Hello World"
}

fn example_3(_: Request<()>) -> impl Responder {
    // return status code with data
    (StatusCode::NOT_FOUND, "Not found!")
}

fn example_4(_: Request<()>) -> impl Responder {
    // return JSON data - for any type implementing `serde::Serialize`
    Json(vec![1, 2, 3])
}

fn example_5(_: Request<()>) -> highnoon::Result<impl Responder> {
    // fallible functions too
    // (also works the return type as `impl Responder` as long as Rust can infer
    // the function returns `highnoon::Result`)
    Ok((StatusCode::CONFLICT, "Already Exists"))
}

Required Methods

Implementations on Foreign Types

Returns StatusCode::NotFound for None, and the inner value for Some

Compatibility with the inner hyper::Response

Implementors

Identity implementation