Skip to main content

Resource

Trait Resource 

Source
pub trait Resource<S: Validators>:
    Sized
    + Send
    + 'static {
    type Create: FromRequest<S>;
    type Update: FromRequest<S>;

    const NAME: &'static str;
    const PATH: &'static str;

    // Required method
    fn to_resource(
        &self,
        state: &S,
        ctx: &Context,
    ) -> Result<ResourceObject, JsonApiError>;
}
Available on crate features http and validation only.
Expand description

One of your types, as a JSON:API resource.

The two directions a handler needs: reading a validated request document into your type, and writing your type back out as a resource object.

Required Associated Constants§

Source

const NAME: &'static str

The JSON:API type member, e.g. "todo".

Source

const PATH: &'static str

Where the resource is mounted, e.g. /api/v1/todos.

Required Associated Types§

Source

type Create: FromRequest<S>

What a create body turns into.

Its FromRequest impl owns reading and validating the request, so a resource whose body is not JSON:API — multipart, form-encoded — needs no special case here.

Usually Self, but it can be a distinct type when a create carries different fields from a stored row.

Source

type Update: FromRequest<S>

What an update body turns into. Usually the same as Self::Create.

Required Methods§

Source

fn to_resource( &self, state: &S, ctx: &Context, ) -> Result<ResourceObject, JsonApiError>

Write this row out as a resource object, links and linkage included.

state is your application state, for anything resolved at startup rather than compiled in — a schema registry, a base URL, a feature flag deciding whether an attribute is exposed. Ignore it when the resource knows its own shape, which is the usual case.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§