json_placeholder_data/users/
mod.rs

1use crate::{by_id, from_json};
2use serde::{Deserialize, Serialize};
3use serde_with::skip_serializing_none;
4
5const PLACEHOLDER_JSON_USERS: &str = include_str!("users.json");
6
7#[skip_serializing_none]
8#[derive(Deserialize, Serialize)]
9pub struct Geo {
10    pub lat: String,
11    pub lng: String,
12}
13
14#[skip_serializing_none]
15#[derive(Deserialize, Serialize)]
16pub struct Address {
17    pub street: String,
18    pub suite: String,
19    pub city: String,
20    pub zipcode: String,
21    pub geo: Geo,
22}
23
24#[skip_serializing_none]
25#[derive(Deserialize, Serialize)]
26pub struct Company {
27    pub name: String,
28    #[serde(rename = "catchPhrase")]
29    pub catch_phrase: String,
30    pub bs: String,
31}
32
33#[skip_serializing_none]
34#[derive(Deserialize, Serialize)]
35pub struct User {
36    pub id: Option<i32>,
37    pub name: String,
38    pub username: String,
39    pub email: String,
40    pub address: Address,
41    pub phone: String,
42    pub website: String,
43    pub company: Company,
44}
45
46/// Get all the users available
47///
48/// # Example
49/// ```
50/// use json_placeholder_data::users::get_all;
51/// assert_eq!(get_all().len(), 10);
52/// ```
53pub fn get_all() -> Vec<User> {
54    from_json!(PLACEHOLDER_JSON_USERS)
55}
56
57/// Get a user by id
58///
59/// # Example
60/// ```
61/// use json_placeholder_data::users::get;
62/// assert_eq!(
63///     get(1).name.as_str(),
64///     "Leanne Graham",
65/// );
66/// ```
67pub fn get(id: i32) -> User {
68    by_id!(id)
69}