Skip to main content

Resource

Trait Resource 

Source
pub trait Resource:
    Send
    + Sync
    + 'static {
    const ACTIONS: &'static [Action] = Action::ALL;

    // Provided methods
    fn index(_ctx: Context) -> impl Future<Output = Result<Response>> + Send { ... }
    fn new(_ctx: Context) -> impl Future<Output = Result<Response>> + Send { ... }
    fn create(_ctx: Context) -> impl Future<Output = Result<Response>> + Send { ... }
    fn show(_ctx: Context) -> impl Future<Output = Result<Response>> + Send { ... }
    fn edit(_ctx: Context) -> impl Future<Output = Result<Response>> + Send { ... }
    fn update(_ctx: Context) -> impl Future<Output = Result<Response>> + Send { ... }
    fn destroy(_ctx: Context) -> impl Future<Output = Result<Response>> + Send { ... }
}
Expand description

A controller for a RESTful resource, mounted with Router::resources.

Every action defaults to 404 Not Found, so implement only the ones you need (and use Router::resources_only to avoid registering the rest):

use glacier::prelude::*;

struct Posts;

impl Resource for Posts {
    async fn index(_ctx: Context) -> Result<Response> {
        Ok(Response::text("all posts"))
    }

    async fn show(ctx: Context) -> Result<Response> {
        let id: u64 = ctx.param_as("id")?;
        Ok(Response::text(format!("post {id}")))
    }
}

Provided Associated Constants§

Source

const ACTIONS: &'static [Action] = Action::ALL

The actions Router::resources mounts. #[glacier::controller] sets this to the actions that are actually defined.

Provided Methods§

Source

fn index(_ctx: Context) -> impl Future<Output = Result<Response>> + Send

GET /name

Source

fn new(_ctx: Context) -> impl Future<Output = Result<Response>> + Send

GET /name/new

Source

fn create(_ctx: Context) -> impl Future<Output = Result<Response>> + Send

POST /name

Source

fn show(_ctx: Context) -> impl Future<Output = Result<Response>> + Send

GET /name/:id

Source

fn edit(_ctx: Context) -> impl Future<Output = Result<Response>> + Send

GET /name/:id/edit

Source

fn update(_ctx: Context) -> impl Future<Output = Result<Response>> + Send

PATCH /name/:id and PUT /name/:id

Source

fn destroy(_ctx: Context) -> impl Future<Output = Result<Response>> + Send

DELETE /name/:id

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§