Trait RestController

Source
pub trait RestController: Controller {
    type Resource: ToParameter;

    // Provided methods
    fn handle<'life0, 'life1, 'async_trait>(
        &'life0 self,
        request: &'life1 Request,
    ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn rest(self, path: &str) -> Handler
       where Self: Sized + 'static { ... }
    fn list<'life0, 'life1, 'async_trait>(
        &'life0 self,
        request: &'life1 Request,
    ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn get<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        request: &'life1 Request,
        id: &'life2 Self::Resource,
    ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn create<'life0, 'life1, 'async_trait>(
        &'life0 self,
        request: &'life1 Request,
    ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn update<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        request: &'life1 Request,
        id: &'life2 Self::Resource,
    ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn patch<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        request: &'life1 Request,
        id: &'life2 Self::Resource,
    ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn delete<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        request: &'life1 Request,
        id: &'life2 Self::Resource,
    ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
}
Expand description

REST controller, which splits requests into 6 REST verbs.

This controller will split incoming requests based on the REST specification and route them to their respective methods.

Available methods are:

  • list (GET /path)
  • create (POST /path)
  • get (GET /path/:id)
  • update (PUT /path/:id)
  • patch (PATCH /path/:id)
  • delete (DELETE /path/:id)

By default, all methods will respond with 501 - Not Implemented. It’s up to the user to implement each method according to their needs.

The :id can be any value which implements the ToParameter trait. Common data types are implemented, e.g., i64 and String.

§Example

use rwf::prelude::*;

#[derive(Default, macros::RestController)]
struct MyController;

#[async_trait]
impl RestController for MyController {
    type Resource = i64;

    async fn get(&self, request: &Request, id: &i64) -> Result<Response, Error> {
        Ok(Response::new().html(format!("Hello, id #{}", id)))
    }
}

Required Associated Types§

Source

type Resource: ToParameter

Resource type used in the request.

Rust is a typed language, this makes handling IDs easier by specifying the expected data type. Inputs not matching this data type will be rejected.

Provided Methods§

Source

fn handle<'life0, 'life1, 'async_trait>( &'life0 self, request: &'life1 Request, ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Figure out which method to call based on request method and path.

Source

fn rest(self, path: &str) -> Handler
where Self: Sized + 'static,

Get a route handler for this controller. Used when adding this controller to the server with a route mapping.

Use the rest! macro instead, e.g.:

rest!("/path" => YourResetController)
Source

fn list<'life0, 'life1, 'async_trait>( &'life0 self, request: &'life1 Request, ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Responds to GET /path. List all available resources at this endpoint. Pagination is allowed.

§Signature
async fn list(&self, request: &Request) -> Result<Response, Error>;
Source

fn get<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, request: &'life1 Request, id: &'life2 Self::Resource, ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Responds to GET /path/:id. Fetch a specific resource, as identified in the request.

§Signature
async fn get(&self, request: &Request, id: &Self::Resource) -> Result<Response, Error>;
Source

fn create<'life0, 'life1, 'async_trait>( &'life0 self, request: &'life1 Request, ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Responds to POST /path. Create a new resource.

§Signature
async fn create(&self, request: &Request) -> Result<Response, Error>;
Source

fn update<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, request: &'life1 Request, id: &'life2 Self::Resource, ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Responds to PUT /path/:id. Update an existing resource.

§Signature
async fn update(&self, request: &Request, id: &Self::Resource) -> Result<Response, Error>;
Source

fn patch<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, request: &'life1 Request, id: &'life2 Self::Resource, ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Responds to PATCH /path/:id. Partially update an existing resource.

§Signature
async fn patch(&self, request: &Request, id: &Self::Resource) -> Result<Response, Error>;
Source

fn delete<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, request: &'life1 Request, id: &'life2 Self::Resource, ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Responds to DELETE /path:id. Deletes an existing resource.

§Signature
async fn delete(&self, request: &Request, id: &Self::Resource) -> Result<Response, Error>;

Implementors§