json_placeholder_data/comments/
mod.rs

1use crate::{by_id, from_json};
2use serde::{Deserialize, Serialize};
3use serde_with::skip_serializing_none;
4
5const PLACEHOLDER_JSON_COMMENTS: &str = include_str!("comments.json");
6
7#[skip_serializing_none]
8#[derive(Deserialize, Serialize)]
9pub struct Comment {
10    #[serde(rename = "postId")]
11    pub post_id: i32,
12    pub id: Option<i32>,
13    pub name: String,
14    pub email: String,
15    pub body: String,
16}
17
18/// Get all the comments available
19///
20/// # Example
21/// ```
22/// use json_placeholder_data::comments::get_all;
23/// assert_eq!(get_all().len(), 500);
24/// ```
25pub fn get_all() -> Vec<Comment> {
26    from_json!(PLACEHOLDER_JSON_COMMENTS)
27}
28
29/// Get a comment by id
30///
31/// # Example
32/// ```
33/// use json_placeholder_data::comments::get;
34/// assert_eq!(
35///     get(37).email.as_str(),
36///     "Jacky@victoria.net",
37/// );
38/// ```
39pub fn get(id: i32) -> Comment {
40    by_id!(id)
41}