use rjapi::{JsonApiResource, JsonApiResponse, Resource};
use serde::Serialize;
use serde_json::json;
#[derive(Serialize)]
struct Post {
id: String,
title: String,
content: String,
created_at: String,
}
impl JsonApiResource for Post {
const RESOURCE_TYPE: &'static str = "posts";
fn id(&self) -> String {
self.id.clone()
}
fn attributes(&self) -> serde_json::Value {
json!({
"title": self.title,
"content": self.content,
"created_at": self.created_at,
})
}
}
fn main() {
let post = Post {
id: "1".to_string(),
title: "Example Post".to_string(),
content: "This is an example post.".to_string(),
created_at: "2023-01-01T00:00:00Z".to_string(),
};
let resource = Resource {
resource_type: Post::RESOURCE_TYPE.to_string(),
id: post.id(),
attributes: Some(post.attributes()),
relationships: None,
links: None,
meta: None,
};
let response = JsonApiResponse::new(resource).build();
println!("{}", serde_json::to_string_pretty(&response).unwrap());
}