Struct Context

Source
pub struct Context<'v> { /* private fields */ }
Expand description

A data structure containing render context that can be “forked” and passed to a child context.

This struct is helpful if you want recursively call Resource::to_object to render a document’s primary data and included resources.

Since the Context struct requires a mutable (unique) reference to a document’s included resources, only one context can be operated on at a time. In other words, if you want to access a context, it cannot have any children in scope. Since you can only operate on a single context at time, a recursive implementation of included resources and sparse field-sets is much easier.

Implementations§

Source§

impl<'v> Context<'v>

Source

pub fn new( kind: Key, query: Option<&'v Query>, included: &'v mut Set<Object>, ) -> Self

Creates a new, root context.

This constructor can only be used when creating a root context. A child context can be created with the fork method.

§Example
use json_api::value::Set;
use json_api::view::Context;

let mut included = Set::new();
let mut ctx = Context::new("posts".parse()?, None, &mut included);
Source

pub fn field(&self, name: &str) -> bool

Returns true if the field name is present in the current context’s field-set or the current context’s field-set does not exist.

Source

pub fn fork(&mut self, kind: Key, key: &Key) -> Context<'_>

Creates a new child context from self.

Source

pub fn include(&mut self, value: Object) -> bool

Adds the value to the context’s included resource set.

If the set did not have this value present, true is returned.

If the set did have this value present, false is returned.

Source

pub fn included(&self) -> bool

Returns true if the context is valid with respect to parent context(s).

If there is no parent context (i.e the current context represents the primary data of the document), this will always return false.

if there is a parent context and this function returns false, this context can should be ignored.

Trait Implementations§

Source§

impl<'v> Debug for Context<'v>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'v> Freeze for Context<'v>

§

impl<'v> RefUnwindSafe for Context<'v>

§

impl<'v> Send for Context<'v>

§

impl<'v> Sync for Context<'v>

§

impl<'v> Unpin for Context<'v>

§

impl<'v> !UnwindSafe for Context<'v>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.