lotr_api/item/
object.rs

1use serde::{Deserialize, Serialize};
2
3use crate::Item;
4
5/// Struct for deserializing the Json response
6#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
7#[allow(dead_code)]
8pub(crate) struct Response<T> {
9    docs: Vec<T>,
10    total: u32,
11    limit: u32,
12    offset: u32,
13    page: Option<u32>,
14    pages: Option<u32>,
15}
16
17impl From<Response<Book>> for Vec<Item> {
18    fn from(response: Response<Book>) -> Self {
19        response.docs.into_iter().map(Item::from).collect()
20    }
21}
22
23impl From<Response<Movie>> for Vec<Item> {
24    fn from(response: Response<Movie>) -> Self {
25        response.docs.into_iter().map(Item::from).collect()
26    }
27}
28
29impl From<Response<Quote>> for Vec<Item> {
30    fn from(response: Response<Quote>) -> Self {
31        response.docs.into_iter().map(Item::from).collect()
32    }
33}
34
35impl From<Response<Character>> for Vec<Item> {
36    fn from(response: Response<Character>) -> Self {
37        response.docs.into_iter().map(Item::from).collect()
38    }
39}
40
41impl From<Response<Chapter>> for Vec<Item> {
42    fn from(response: Response<Chapter>) -> Self {
43        response.docs.into_iter().map(Item::from).collect()
44    }
45}
46
47impl<T> Response<T> {
48    pub fn get_contents(self) -> Vec<T> {
49        self.docs
50    }
51}
52
53#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
54pub struct Book {
55    pub _id: String,
56    pub name: String,
57}
58
59#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
60pub struct Movie {
61    pub _id: String,
62    pub name: String,
63
64    #[serde(rename = "runtimeInMinutes")]
65    pub runtime_in_minutes: f32,
66
67    #[serde(rename = "budgetInMillions")]
68    pub budget_in_millions: f32,
69
70    #[serde(rename = "boxOfficeRevenueInMillions")]
71    pub box_office_revenue_in_millions: f32,
72
73    #[serde(rename = "academyAwardNominations")]
74    pub academy_award_nominations: u32,
75
76    #[serde(rename = "academyAwardWins")]
77    pub academy_award_wins: u32,
78
79    #[serde(rename = "rottenTomatoesScore")]
80    pub rotten_tomates_score: f32,
81}
82
83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
84pub struct Quote {
85    pub _id: String,
86    // This should be a String but until https://github.com/gitfrosh/lotr-api/issues/151 gets
87    // resolved it must be an optional value
88    pub dialog: Option<String>,
89    pub movie: String,
90    pub character: String,
91    pub id: String,
92}
93
94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
95pub struct Character {
96    pub _id: String,
97    pub height: Option<String>,
98    pub gender: Option<String>,
99    pub birth: Option<String>,
100    pub spouse: Option<String>,
101    pub death: Option<String>,
102    pub realm: Option<String>,
103    pub hair: Option<String>,
104    pub name: String,
105
106    #[serde(rename = "wikiUrl")]
107    pub wiki_url: Option<String>,
108}
109
110#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
111pub struct Chapter {
112    pub _id: String,
113
114    #[serde(rename = "chapterName")]
115    pub chapter_name: String,
116
117    pub book: String,
118}
119
120#[cfg(test)]
121mod test {
122    use super::*;
123
124    #[test]
125    fn test_movie_deserialize() {
126        let tests = vec![
127            r#"
128    {
129      "_id": "5cd95395de30eff6ebccde57",
130      "name": "The Hobbit Series",
131      "runtimeInMinutes": 462,
132      "budgetInMillions": 675,
133      "boxOfficeRevenueInMillions": 2932,
134      "academyAwardNominations": 7,
135      "academyAwardWins": 1,
136      "rottenTomatoesScore": 66
137    }"#,
138            r#"
139    {
140      "_id": "5cd95395de30eff6ebccde57",
141      "name": "The Hobbit Series",
142      "runtimeInMinutes": 462.1,
143      "budgetInMillions": 675.23,
144      "boxOfficeRevenueInMillions": 2932.31,
145      "academyAwardNominations": 7,
146      "academyAwardWins": 1,
147      "rottenTomatoesScore": 66.33333333
148    }"#,
149        ];
150        for json in tests {
151            println!("{}", json);
152            serde_json::from_str::<Movie>(json).unwrap();
153        }
154    }
155}