use axum::{
response::{IntoResponse, Response},
Json,
};
use serde_json::{json, Value};
pub trait JsonResource {
fn to_json(&self) -> Value;
fn to_response(&self) -> Response {
Json(json!({
"data": self.to_json()
}))
.into_response()
}
}
pub struct ResourceCollection<T: JsonResource> {
pub data: Vec<T>,
}
impl<T: JsonResource> ResourceCollection<T> {
pub fn new(data: Vec<T>) -> Self {
Self { data }
}
}
impl<T: JsonResource> IntoResponse for ResourceCollection<T> {
fn into_response(self) -> Response {
let transformed: Vec<Value> = self.data.iter().map(|item| item.to_json()).collect();
Json(json!({
"data": transformed
}))
.into_response()
}
}
#[macro_export]
macro_rules! resource {
($name:ident, $body:expr) => {
impl JsonResource for $name {
fn to_json(&self) -> serde_json::Value {
$body
}
}
};
}