Struct Response

Source
pub struct Response { /* private fields */ }
Expand description

Each request ultimately ends in a Response that is served to the client and then discarded, like fallen flower petals. Together with Request and Responder it forms the holy trinity of R’s in Vial.

Rather than use the “Builder” pattern like more mature and better designed libraries, Vial’s Response lets you set properties either directly or using Builder-style methods:

vial::routes! {
    GET "/404" => |_| Response::from(404)
        .with_header("Content-Type", "text/plain")
        .with_body("404 Not Found");
}

It also defaults to text/html, so you need to use with_header() or header() to send plain text.

Implementations§

Source§

impl Response

Source

pub fn new() -> Response

Create a new, empty, 200 response - ready for HTML!

Source

pub fn code(&self) -> usize

HTTP Status Code

Source

pub fn content_type(&self) -> &str

Either the inferred or user-set Content-Type for this Response.

Source

pub fn body(&self) -> &str

Response body.

Source

pub fn headers(&self) -> &HashMap<String, String>

Take a peek at all the headers for this response.

Source

pub fn header(&self, name: &str) -> Option<&str>

Get an individual header. name is case insensitive.

Source

pub fn set_header(&mut self, name: &str, value: &str)

Set an individual header.

Source

pub fn from<T: Into<Response>>(from: T) -> Response

Convert into a Response.

Source

pub fn from_asset(path: &str) -> Response

Create a response from an asset. See the asset module for more information on using assets.

Source

pub fn from_reader(reader: Box<dyn Read>) -> Response

Create a response from a (boxed) io::Read.

Source

pub fn from_file(path: &str) -> Response

Creates a response from a file on disk. TODO: Path?

Source

pub fn from_error<E: Error>(err: E) -> Response

Creates a 500 response from an error, displaying it.

Source

pub fn from_header(name: &str, value: &str) -> Response

Creates a new Response and sets the given header, in addition to the defaults.

Source

pub fn from_body<S: AsRef<str>>(body: S) -> Response

Creates a new default Response with the given body.

Source

pub fn from_text<S: AsRef<str>>(text: S) -> Response

Creates a new text/plain Response with the given body.

Source

pub fn from_code(code: usize) -> Response

Creates a new response with the given HTTP Status Code.

Source

pub fn with_code(self, code: usize) -> Response

Creates a new response with the given HTTP Status Code.

Source

pub fn with_body<S: AsRef<str>>(self, body: S) -> Response

Body builder. Returns a Response with the given body.

Source

pub fn with_text<S: AsRef<str>>(self, text: S) -> Response

Returns a text/plain Response with the given body.

Source

pub fn with_reader(self, reader: Box<dyn Read>) -> Response

Returns a Response using the given reader for the body.

Source

pub fn with_asset(self, path: &str) -> Response

Uses an asset for the given body and sets the Content-Type header based on the file’s extension.

See the asset module for more information on using assets.

Source

pub fn with_file(self, path: &str) -> Response

Sets this Response’s body to the body of the given file and sets the Content-Type header based on the file’s extension.

Source

pub fn with_error<E: Error>(self, err: E) -> Response

Sets the response code to 500 and the body to the error’s text.

Source

pub fn with_header(self, key: &str, value: &str) -> Response

Returns a Response with the given header set to the value.

Source

pub fn len(&self) -> usize

Length of the body.

Source

pub fn is_empty(&self) -> bool

Is ths response empty?

Source

pub fn redirect_to<U: AsRef<str>>(url: U) -> Response

Returns a 302 redirect to the given URL.

Source

pub fn write<W: Write>(self, w: W) -> Result<()>

Writes this response to a stream.

Trait Implementations§

Source§

impl Debug for Response

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Response

Source§

fn default() -> Response

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

impl Display for Response

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&String> for Response

Source§

fn from(s: &String) -> Response

Converts to this type from the input type.
Source§

impl From<&str> for Response

Source§

fn from(s: &str) -> Response

Converts to this type from the input type.
Source§

impl From<Cow<'_, [u8]>> for Response

Source§

fn from(i: Cow<'_, [u8]>) -> Response

Converts to this type from the input type.
Source§

impl From<String> for Response

Source§

fn from(body: String) -> Response

Converts to this type from the input type.
Source§

impl From<usize> for Response

Source§

fn from(i: usize) -> Response

Converts to this type from the input type.
Source§

impl PartialEq for Response

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Responder for Response

Source§

fn to_response(self) -> Response

Consume this object and return a Response representing it.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.