Trait json_api::Resource
[−]
[src]
pub trait Resource { 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>; }
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
fn kind() -> Key
Returns a key containing the type of resource.
Example
use json_api::Resource; let kind = Post::kind(); assert_eq!(kind, "posts");
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");
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.
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.