Struct saphir::response::Builder

source ·
pub struct Builder { /* private fields */ }
Expand description

Struct used to conveniently build a response

Implementations§

source§

impl Builder

source

pub fn json<T: Serialize>( self, t: &T ) -> Result<Builder, (Builder, SaphirError)>

Available on crate feature json only.
source§

impl Builder

source

pub fn form<T: Serialize>( self, t: &T ) -> Result<Builder, (Builder, SaphirError)>

Available on crate feature form only.
source§

impl Builder

source

pub fn file<F: Into<FileStream>>(self, file: F) -> Builder

Available on crate feature file only.
source§

impl Builder

source

pub fn new() -> Self

Creates a new default instance of Builder to construct either a Head or a Response.


let response = Builder::new()
    .status(200)
    .build()
    .unwrap();
source

pub fn status<T>(self, status: T) -> Builderwhere StatusCode: TryFrom<T>, <StatusCode as TryFrom<T>>::Error: Into<Error>,

Set the HTTP status for this response.

This function will configure the HTTP status code of the Response that will be returned from Builder::build.

By default this is 200.


let response = Builder::new()
    .status(200)
    .build()
    .unwrap();
source

pub fn version(self, version: Version) -> Builder

Set the HTTP version for this response.

This function will configure the HTTP version of the Response that will be returned from Builder::build.

By default this is HTTP/1.1


let response = Builder::new()
    .version(Version::HTTP_2)
    .build()
    .unwrap();
source

pub fn header<K, V>(self, key: K, value: V) -> Builderwhere HeaderName: TryFrom<K>, <HeaderName as TryFrom<K>>::Error: Into<Error>, HeaderValue: TryFrom<V>, <HeaderValue as TryFrom<V>>::Error: Into<Error>,

Appends a header to this response builder.

This function will append the provided key/value as a header to the internal HeaderMap being constructed. Essentially this is equivalent to calling HeaderMap::append.


let response = Builder::new()
    .header("Content-Type", "text/html")
    .header("X-Custom-Foo", "bar")
    .header("content-length", 0)
    .build()
    .unwrap();
source

pub fn headers_ref(&self) -> Option<&HeaderMap<HeaderValue>>

Get header on this response builder.

When builder has error returns None.

let res = Builder::new()
    .header("Accept", "text/html")
    .header("X-Custom-Foo", "bar");
let headers = res.headers_ref().unwrap();
assert_eq!( headers["Accept"], "text/html" );
assert_eq!( headers["X-Custom-Foo"], "bar" );
source

pub fn headers_mut(&mut self) -> Option<&mut HeaderMap<HeaderValue>>

Get header on this response builder. when builder has error returns None

let mut res = Builder::new();
{
  let headers = res.headers_mut().unwrap();
  headers.insert("Accept", HeaderValue::from_static("text/html"));
  headers.insert("X-Custom-Foo", HeaderValue::from_static("bar"));
}
let headers = res.headers_ref().unwrap();
assert_eq!( headers["Accept"], "text/html" );
assert_eq!( headers["X-Custom-Foo"], "bar" );
source

pub fn extension<T>(self, extension: T) -> Builderwhere T: Any + Send + Sync + 'static,

Adds an extension to this builder


let response = Builder::new()
    .extension("My Extension")
    .build()
    .unwrap();

assert_eq!(response.extensions().get::<&'static str>(),
           Some(&"My Extension"));
source

pub fn cookie(self, cookie: Cookie<'static>) -> Builder

Adds an extension to this builder


let cookie = Cookie::new("MyCookie", "MyCookieValue");

let response = Builder::new()
    .cookie(cookie)
    .build()
    .unwrap();

assert_eq!(response.cookies().get("MyCookie").map(|c| c.value()), Some("MyCookieValue"))
source

pub fn cookies_mut(&mut self) -> &mut CookieJar

source

pub fn cookies(self, cookies: CookieJar) -> Builder

source

pub fn body<B: 'static + Into<RawBody> + Send>(self, body: B) -> Builder

source

pub fn build(self) -> Result<Response<Body>, SaphirError>

Trait Implementations§

source§

impl Default for Builder

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Responder for Builder

source§

fn respond_with_builder(self, _builder: Builder, _ctx: &HttpContext) -> Builder

Consume self into a builder Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more