Trait Resource

Source
pub trait Resource {
    // Required methods
    fn kind() -> Key;
    fn id(&self) -> String;
    fn to_ident(&self, ctx: &mut Context<'_>) -> Result<Identifier, Error>;
    fn to_object(&self, ctx: &mut Context<'_>) -> Result<Object, Error>;
}
Expand description

A trait indicating that the given type can be represented as a resource.

Implementing this trait manually is not recommended. The resource! macro provides a friendly DSL that implements trait with some additional functionality.

§Example

#[macro_use]
extern crate json_api;

struct Post(u64);

resource!(Post, |&self| {
    kind "posts";
    id self.0;
});

Required Methods§

Source

fn kind() -> Key

Returns a key containing the type of resource.

§Example
use json_api::Resource;

let kind = Post::kind();
assert_eq!(kind, "posts");
Source

fn id(&self) -> String

Returns a given resource’s id as a string.

§Example
use json_api::Resource;

let post = Post(25);
assert_eq!(post.id(), "25");
Source

fn to_ident(&self, ctx: &mut Context<'_>) -> Result<Identifier, Error>

Renders a given resource as an identifier object.

Calling this function directly is not recommended. It is much more ergonomic to use the json_api::to_doc function.

Source

fn to_object(&self, ctx: &mut Context<'_>) -> Result<Object, Error>

Renders a given resource as a resource object.

Calling this function directly is not recommended. It is much more ergonomic to use the json_api::to_doc function.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§