Response

Struct Response 

Source
pub struct Response {
    pub cors: String,
    /* private fields */
}
Expand description

Response struct to client

Fields§

§cors: String

Cross Origin Site

Implementations§

Source§

impl Response

Source

pub fn new_empty() -> Response

Returns a new Response

Source§

impl Response

Source

pub fn html(page: &'static str) -> Response

Send a html file from resources/views directory

§Arguments
  • page - A String that the page on views directory
§Examples
use pillow::http::*;

#[controller(method = "GET", path = "/")]
fn index() -> Response {
    Response::html("index")
}
Source

pub fn view(template: Template) -> Response

Send a html file from resources/views directory

§Arguments
  • Template - Enum contain the name of file in resources/views
§Examples
use pillow::{
    http::*,
    templates::{Context, Template}
};

#[controller(method = "GET", path = "/")]
 fn index() -> Response {
    let mut ctx = Context::new();

    Response::view(Template::Tera("index", "tera.html", ctx))
}
Source

pub fn hbs(page: &'static str, data: Value) -> Response

Send a hbs file from resources/views directory

§Arguments
  • page - A String that the page on views directory
  • data - Json value
§Examples
use pillow::json;
use pillow::http::*;

#[controller(method = "GET", path = "/")]
 fn index() -> Response {
    let json = json! ({
        "name": "foo"
    });

    Response::hbs("index", json)
}
Source

pub fn json(js: Value) -> Response

Send a json from macro json!

§Arguments
  • json - A string slice that sends to http client
§Examples
use pillow::json;
use pillow::http::{ MainRouter, Response };

#[controller(method = "GET", path = "/")]
 fn index() -> Response {
    let json = json! ({
        "name": "foo"
    });

    Response::json(json)
}
Source

pub fn json_from_str(json: &str) -> Response

Send a json from str

§Arguments
  • json - A string slice that sends to http client
§Examples
use pillow::http::*;

#[controller(method = "GET", path = "/")]
 fn index() -> Response {
    Response::json_from_str(r#{
        "name": "foo"
    }#)
}
Source

pub fn text(txt: &str) -> Response

Send text to client

§Examples
use pillow::http::*;

#[controller(method = "GET", path = "/")]
 fn index() -> Response {
    Response::text("hello world")
}
Source

pub fn css(&mut self, css: String) -> String

Send css response to client

Source

pub fn javascript(&mut self, js: String) -> String

Send javascript to client

Source

pub fn file(content_type: ContentType, content: Vec<u8>) -> Response

Source§

impl Response

Source

pub fn redirect(location: &'static str) -> Response

Source§

impl Response

Source

pub fn insert_string_content(&mut self, content: String)

Insert content in the Response.body

Source

pub fn insert_bytes_content(&mut self, content: Vec<u8>)

Source§

impl Response

Source

pub fn content_type(&mut self, content_type: ContentType) -> &Response

Change Content-Type of the response

Source§

impl Response

Source§

impl Response

Source

pub fn add_header(&mut self, header: Header, value: String)

Add header to response

§Arguments
  • header - Header name
  • value - Header value
§Examples
use pillow::http::Router;

let mut app = Router::new();

app.get("/", |_, response| {
    response.add_header(Header::ContentType, "text/hmtl".to_string());
    response.view("index")
})
Source

pub fn add_multiple_headers(&mut self, headers: Vec<(Header, String)>)

Add multiple headers to response

§Examples
use pillow::http::*;

#[controller(method="GET", path = "/")]
fn index() {
    let response: Response = Response::new_empty();

    response.add_multiple_headers(vec![
        (Header::ContentType, "text/html".to_string()),
        (Header::AccessControlAllowOrigin, "*".to_string())
    ])

    response
}
Source

pub fn get_headers(&self) -> String

Get All headers in one string

§Examples
let headers: String = self.get_headers();
Source

pub fn clear_headers(&mut self)

Clear All headers

Source

pub fn get_status_line(&self) -> String

Get Status Line HTTP/1.1 200 OK

§Examples
let status_line: String = self.get_status_line();

assert_eq!(status_line, "HTTP/1.1 200 OK".to_string());
Source

pub fn set_status_code(&mut self, code: StatusCode)

Set Status Code Like 200 OK

§Examples
let response: Response = Response::new_empty();
response.set_status_code(StatusCode::Successfull(status_code::Successfull::OK));
Source

pub fn get_body(&self) -> Body

Trait Implementations§

Source§

impl Clone for Response

Source§

fn clone(&self) -> Response

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Response

Source§

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

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

impl PartialEq for Response

Source§

fn eq(&self, other: &Response) -> 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 ToString for Response

Source§

fn to_string(&self) -> String

Convert Response struct in String

Source§

impl Eq for Response

Source§

impl StructuralPartialEq for Response

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V