lotr_api/item/
object.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use serde::{Deserialize, Serialize};

use crate::Item;

/// Struct for deserializing the Json response
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[allow(dead_code)]
pub(crate) struct Response<T> {
    docs: Vec<T>,
    total: u32,
    limit: u32,
    offset: u32,
    page: Option<u32>,
    pages: Option<u32>,
}

impl From<Response<Book>> for Vec<Item> {
    fn from(response: Response<Book>) -> Self {
        response.docs.into_iter().map(Item::from).collect()
    }
}

impl From<Response<Movie>> for Vec<Item> {
    fn from(response: Response<Movie>) -> Self {
        response.docs.into_iter().map(Item::from).collect()
    }
}

impl From<Response<Quote>> for Vec<Item> {
    fn from(response: Response<Quote>) -> Self {
        response.docs.into_iter().map(Item::from).collect()
    }
}

impl From<Response<Character>> for Vec<Item> {
    fn from(response: Response<Character>) -> Self {
        response.docs.into_iter().map(Item::from).collect()
    }
}

impl From<Response<Chapter>> for Vec<Item> {
    fn from(response: Response<Chapter>) -> Self {
        response.docs.into_iter().map(Item::from).collect()
    }
}

impl<T> Response<T> {
    pub fn get_contents(self) -> Vec<T> {
        self.docs
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Book {
    pub _id: String,
    pub name: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Movie {
    pub _id: String,
    pub name: String,

    #[serde(rename = "runtimeInMinutes")]
    pub runtime_in_minutes: f32,

    #[serde(rename = "budgetInMillions")]
    pub budget_in_millions: f32,

    #[serde(rename = "boxOfficeRevenueInMillions")]
    pub box_office_revenue_in_millions: f32,

    #[serde(rename = "academyAwardNominations")]
    pub academy_award_nominations: u32,

    #[serde(rename = "academyAwardWins")]
    pub academy_award_wins: u32,

    #[serde(rename = "rottenTomatoesScore")]
    pub rotten_tomates_score: f32,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Quote {
    pub _id: String,
    // This should be a String but until https://github.com/gitfrosh/lotr-api/issues/151 gets
    // resolved it must be an optional value
    pub dialog: Option<String>,
    pub movie: String,
    pub character: String,
    pub id: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Character {
    pub _id: String,
    pub height: Option<String>,
    pub gender: Option<String>,
    pub birth: Option<String>,
    pub spouse: Option<String>,
    pub death: Option<String>,
    pub realm: Option<String>,
    pub hair: Option<String>,
    pub name: String,

    #[serde(rename = "wikiUrl")]
    pub wiki_url: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Chapter {
    pub _id: String,

    #[serde(rename = "chapterName")]
    pub chapter_name: String,

    pub book: String,
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_movie_deserialize() {
        let tests = vec![
            r#"
    {
      "_id": "5cd95395de30eff6ebccde57",
      "name": "The Hobbit Series",
      "runtimeInMinutes": 462,
      "budgetInMillions": 675,
      "boxOfficeRevenueInMillions": 2932,
      "academyAwardNominations": 7,
      "academyAwardWins": 1,
      "rottenTomatoesScore": 66
    }"#,
            r#"
    {
      "_id": "5cd95395de30eff6ebccde57",
      "name": "The Hobbit Series",
      "runtimeInMinutes": 462.1,
      "budgetInMillions": 675.23,
      "boxOfficeRevenueInMillions": 2932.31,
      "academyAwardNominations": 7,
      "academyAwardWins": 1,
      "rottenTomatoesScore": 66.33333333
    }"#,
        ];
        for json in tests {
            println!("{}", json);
            serde_json::from_str::<Movie>(json).unwrap();
        }
    }
}