Skip to main content

rullst_orm/
resource.rs

1use serde_json::Value;
2
3pub trait ApiResource {
4    fn to_array(&self) -> Value;
5}
6
7pub struct JsonResource<'a, T: ApiResource> {
8    pub data: &'a T,
9}
10
11impl<'a, T: ApiResource> JsonResource<'a, T> {
12    pub fn new(data: &'a T) -> Self {
13        Self { data }
14    }
15
16    pub fn resolve(&self) -> Value {
17        self.data.to_array()
18    }
19}
20
21pub struct ResourceCollection<'a, T: ApiResource> {
22    pub data: &'a [T],
23}
24
25impl<'a, T: ApiResource> ResourceCollection<'a, T> {
26    pub fn new(data: &'a [T]) -> Self {
27        Self { data }
28    }
29
30    pub fn resolve(&self) -> Value {
31        let array: Vec<Value> = self.data.iter().map(|item| item.to_array()).collect();
32        serde_json::json!(array)
33    }
34}