mal-api 2.0.3

An asynchronous MyAnimeList (MAL) API library for Rust
Documentation
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::fmt::Display;

use crate::common::{
    AlternativeTitles, Genre, MainPicture, Paging, PagingIter, Ranking, RelationType, NSFW,
};
use serde::{Deserialize, Serialize};
use serde_json::{self, Value};

#[derive(Debug, Deserialize, Serialize)]
pub struct AnimeList {
    pub data: Vec<AnimeListNode>,
    pub paging: Paging,
}

impl PagingIter for AnimeList {
    type Item = Self;

    fn next_page(&self) -> Option<&String> {
        self.paging.next.as_ref()
    }

    fn prev_page(&self) -> Option<&String> {
        self.paging.previous.as_ref()
    }
}

impl Display for AnimeList {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AnimeListNode {
    pub node: AnimeFields,

    /// This field is only present when querying for a User's anime list
    pub list_status: Option<AnimeListStatus>,
}

impl Display for AnimeListNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AnimePicture {
    pub medium: String,
    pub large: String,
}

impl Display for AnimePicture {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum AnimeMediaType {
    Unknown,
    TvSpecial, // undocumented media type...
    Tv,
    Cm, // undocumented media type...
    Ova,
    Movie,
    Special,
    Ona,
    Music,
    Pv, // undocumented media type...
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum AnimeStatus {
    FinishedAiring,
    CurrentlyAiring,
    NotYetAired,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AnimeListStatus {
    pub status: Option<super::requests::UserAnimeListStatus>,
    pub score: u8,
    pub num_episodes_watched: u32,
    pub is_rewatching: bool,
    pub start_date: Option<String>,
    pub finish_date: Option<String>,
    pub priority: u8,
    pub num_times_rewatched: u32,
    pub rewatch_value: u8,
    pub tags: Vec<String>,
    pub comments: String,
    pub updated_at: String,
}

impl Display for AnimeListStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct StartSeason {
    pub year: u32,
    pub season: super::requests::Season,
}

impl Display for StartSeason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Broadcast {
    pub day_of_the_week: String,
    pub start_time: Option<String>,
}

impl Display for Broadcast {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum Source {
    Other,
    Original,
    Manga,
    #[serde(rename = "4_koma_manga")]
    KomaManga,
    WebManga,
    DigitalMedia,
    Novel,
    LightNovel,
    VisualNovel,
    Game,
    CardGame,
    Book,
    PictureBook,
    MixedMedia, // undocumented source...
    Radio,
    Music,
    WebNovel, // undocumented source...
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Rating {
    G,
    PG,
    #[serde(rename = "pg_13")]
    PG13,
    R,
    #[serde(rename = "r+")]
    RP,
    RX,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Studio {
    pub id: u32,
    pub name: String,
}

impl Display for Studio {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

// Wrap everything in Options since user controls what fields should be returned
#[derive(Debug, Deserialize, Serialize)]
pub struct AnimeFields {
    pub id: u32,
    pub title: String,
    pub main_picture: Option<MainPicture>,
    pub alternative_titles: Option<AlternativeTitles>,
    pub start_date: Option<String>,
    pub end_date: Option<String>,
    pub synopsis: Option<String>,
    pub mean: Option<f32>,
    pub rank: Option<u32>,
    pub popularity: Option<u32>,
    pub num_list_users: Option<u32>,
    pub num_scoring_users: Option<u32>,
    pub nsfw: Option<NSFW>,
    pub genres: Option<Vec<Genre>>,
    pub created_at: Option<String>,
    pub updated_at: Option<String>,
    pub media_type: Option<AnimeMediaType>,
    pub status: Option<AnimeStatus>,
    pub my_list_status: Option<AnimeListStatus>,
    pub num_episodes: Option<u32>,
    pub start_season: Option<StartSeason>,
    pub broadcast: Option<Broadcast>,
    pub source: Option<Source>,
    pub average_episode_duration: Option<u32>,
    pub rating: Option<Rating>,
    pub studios: Option<Vec<Studio>>,
}

impl Display for AnimeFields {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct RelatedAnime {
    pub node: AnimeFields,
    pub relation_type: RelationType,
    pub relation_type_formatted: String,
}

impl Display for RelatedAnime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Recommendations {
    pub node: AnimeFields,
    pub num_recommendations: u32,
}

impl Display for Recommendations {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Statistics {
    pub num_list_users: u32,
    pub status: StatisticsStatus,
}

impl Display for Statistics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct StatisticsStatus {
    // MAL returns these as strings, even though docs say they are supposed to be integers
    // Use custom serializer for these fields to turn the strings into u32
    #[serde(deserialize_with = "deserialize_string_to_u32")]
    pub watching: u32,
    #[serde(deserialize_with = "deserialize_string_to_u32")]
    pub completed: u32,
    #[serde(deserialize_with = "deserialize_string_to_u32")]
    pub on_hold: u32,
    #[serde(deserialize_with = "deserialize_string_to_u32")]
    pub dropped: u32,
    #[serde(deserialize_with = "deserialize_string_to_u32")]
    pub plan_to_watch: u32,
}

impl Display for StatisticsStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

fn deserialize_string_to_u32<'de, D>(deserializer: D) -> Result<u32, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value: Value = Deserialize::deserialize(deserializer)?;
    if let Some(number) = value.as_str().and_then(|s| s.parse().ok()) {
        Ok(number)
    } else {
        Err(serde::de::Error::custom("Invalid value for u32"))
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AnimeDetails {
    #[serde(flatten)]
    pub shared_fields: AnimeFields,

    pub pictures: Option<Vec<AnimePicture>>,
    pub background: Option<String>,
    pub related_anime: Option<Vec<RelatedAnime>>,
    pub related_manga: Option<Vec<crate::manga::responses::RelatedManga>>,
    pub recommendations: Option<Vec<Recommendations>>,
    pub statistics: Option<Statistics>,
}

impl Display for AnimeDetails {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AnimeRanking {
    pub data: Vec<AnimeRankingNode>,
    pub paging: Paging,
}

impl Display for AnimeRanking {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

impl PagingIter for AnimeRanking {
    type Item = Self;

    fn next_page(&self) -> Option<&String> {
        self.paging.next.as_ref()
    }

    fn prev_page(&self) -> Option<&String> {
        self.paging.previous.as_ref()
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AnimeRankingNode {
    pub node: AnimeFields,
    pub ranking: Ranking,
}

impl Display for AnimeRankingNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct SeasonalAnime {
    pub data: Vec<SeasonalAnimeNode>,
    pub paging: Paging,
}

impl Display for SeasonalAnime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

impl PagingIter for SeasonalAnime {
    type Item = Self;

    fn next_page(&self) -> Option<&String> {
        self.paging.next.as_ref()
    }

    fn prev_page(&self) -> Option<&String> {
        self.paging.previous.as_ref()
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct SeasonalAnimeNode {
    pub node: AnimeFields,
}

impl Display for SeasonalAnimeNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct SuggestedAnime {
    pub data: Vec<SuggestedAnimeNode>,
    pub paging: Paging,
}

impl Display for SuggestedAnime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}

impl PagingIter for SuggestedAnime {
    type Item = Self;

    fn next_page(&self) -> Option<&String> {
        self.paging.next.as_ref()
    }

    fn prev_page(&self) -> Option<&String> {
        self.paging.previous.as_ref()
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct SuggestedAnimeNode {
    pub node: AnimeFields,
}

impl Display for SuggestedAnimeNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap_or_default())
    }
}