pub struct ResourceCollection<T: Resource> { /* private fields */ }Expand description
A collection of resources with optional pagination metadata.
Wraps Vec<T: Resource> and produces the standard JSON envelope:
- Without pagination:
{"data": [...]} - With pagination:
{"data": [...], "meta": {...}, "links": {...}}
§Example
ⓘ
use ferro::{Resource, ResourceCollection, PaginationMeta};
let resources: Vec<UserResource> = users.into_iter()
.map(UserResource::from)
.collect();
// Without pagination
let collection = ResourceCollection::new(resources);
Ok(collection.to_response(&req))
// With pagination
let meta = PaginationMeta::new(page, per_page, total);
let collection = ResourceCollection::paginated(resources, meta);
Ok(collection.to_response(&req))Implementations§
Source§impl<T: Resource> ResourceCollection<T>
impl<T: Resource> ResourceCollection<T>
Sourcepub fn paginated(items: Vec<T>, meta: PaginationMeta) -> Self
pub fn paginated(items: Vec<T>, meta: PaginationMeta) -> Self
Create a collection with pagination metadata.
Sourcepub fn additional(self, value: Value) -> Self
pub fn additional(self, value: Value) -> Self
Add extra top-level fields merged alongside data/meta/links.
Sourcepub fn to_response(&self, req: &Request) -> HttpResponse
pub fn to_response(&self, req: &Request) -> HttpResponse
Produce the JSON response.
- Without pagination:
{"data": [...]} - With pagination:
{"data": [...], "meta": {...}, "links": {...}} - With additional: merges additional fields at top level
Auto Trait Implementations§
impl<T> Freeze for ResourceCollection<T>
impl<T> RefUnwindSafe for ResourceCollection<T>where
T: RefUnwindSafe,
impl<T> Send for ResourceCollection<T>where
T: Send,
impl<T> Sync for ResourceCollection<T>where
T: Sync,
impl<T> Unpin for ResourceCollection<T>where
T: Unpin,
impl<T> UnsafeUnpin for ResourceCollection<T>
impl<T> UnwindSafe for ResourceCollection<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more