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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
mod at_home_server;
mod auth_tokens;
mod author;
mod chapter;
mod check_token_response;
mod check_username_available;
mod cover;
mod custom_list;
mod error;
mod is_following_response;
mod legacy_id_mapping;
mod login_response;
mod manga;
mod manga_aggregate;
mod manga_links;
mod manga_read_markers;
mod manga_reading_status;
mod manga_reading_statuses;
mod manga_relation;
mod ratings;
mod refresh_token_response;
mod report;
mod scanlation_group;
mod statistics;
mod tag;
mod upload_session;
mod upload_session_file;
mod user;
mod user_report;
mod user_settings;
use std::collections::HashMap;
use mangadex_api_types as types;
use serde::Deserialize;
use uuid::Uuid;
use crate::{ApiData, ApiObject, ApiObjectNoRelationships};
pub use at_home_server::AtHomeServer;
pub use auth_tokens::AuthTokens;
pub use author::AuthorAttributes;
pub use chapter::ChapterAttributes;
pub use check_token_response::CheckTokenResponse;
pub use check_username_available::CheckUsernameAvailableResponse;
pub use cover::CoverAttributes;
pub use custom_list::CustomListAttributes;
pub use is_following_response::IsFollowingResponse;
pub use legacy_id_mapping::LegacyMappingIdAttributes;
pub use login_response::LoginResponse;
pub use manga::MangaAttributes;
pub use manga_aggregate::MangaAggregate;
pub use manga_links::MangaLinks;
pub use manga_read_markers::{MangaReadMarkers, UngroupedMangaReadMarkers};
pub use manga_reading_status::MangaReadingStatus;
pub use manga_reading_statuses::MangaReadingStatuses;
pub use manga_relation::MangaRelationAttributes;
pub use ratings::RatingsList;
pub use refresh_token_response::RefreshTokenResponse;
pub use report::ReportReasonAttributes;
pub use scanlation_group::ScanlationGroupAttributes;
pub use statistics::manga::MangaStatisticsObject;
pub use tag::TagAttributes;
pub use types::error::schema::MangaDexErrorResponse;
use types::error::Result;
use types::{Language, MangaRelation, RelationshipType, ResponseType};
pub use upload_session::UploadSessionResponse;
pub use upload_session_file::{UploadSessionFileAttributes, UploadSessionFileData};
pub use user::UserAttributes;
pub use user_report::UserReportAttributes;
pub use user_settings::UserSettingsAttributes;
pub type AtHomeServerResponse = Result<AtHomeServer>;
pub type AuthorObject = ApiObject<AuthorAttributes>;
pub type AuthorData = ApiData<AuthorObject>;
pub type AuthorResponse = Result<AuthorData>;
pub type AuthorListResponse = Result<Results<AuthorObject>>;
pub type ChapterObject = ApiObject<ChapterAttributes>;
pub type ChapterData = ApiData<ChapterObject>;
pub type ChapterResponse = Result<ChapterData>;
pub type ChapterListResponse = Result<Results<ChapterObject>>;
pub type CoverObject = ApiObject<CoverAttributes>;
pub type CoverData = ApiData<CoverObject>;
pub type CoverResponse = Result<CoverData>;
pub type CoverListResponse = Result<Results<CoverObject>>;
pub type CustomListObject = ApiObject<CustomListAttributes>;
pub type CustomListData = ApiData<CustomListObject>;
pub type CustomListResponse = Result<CustomListData>;
pub type CustomListListResponse = Result<Results<CustomListObject>>;
pub type GroupObject = ApiObject<ScanlationGroupAttributes>;
pub type GroupData = ApiData<GroupObject>;
pub type GroupResponse = Result<GroupData>;
pub type GroupListResponse = Result<Results<GroupObject>>;
pub type IdMappingObject = ApiObject<LegacyMappingIdAttributes>;
pub type IdMappingData = ApiData<IdMappingObject>;
pub type IdMappingListResponse = Result<Results<IdMappingObject>>;
pub type MangaObject = ApiObject<MangaAttributes>;
pub type MangaData = ApiData<MangaObject>;
pub type MangaResponse = Result<MangaData>;
pub type MangaListResponse = Result<Results<MangaObject>>;
pub type MangaAggregateResponse = Result<MangaAggregate>;
pub type UngroupedMangaReadMarkersResponse = Result<UngroupedMangaReadMarkers>;
pub type MangaReadMarkersResponse = Result<MangaReadMarkers>;
pub type MangaReadingStatusResponse = Result<MangaReadingStatus>;
pub type MangaReadingStatusesResponse = Result<MangaReadingStatuses>;
pub type MangaRelationObject = ApiObject<MangaRelationAttributes>;
pub type MangaRelationListResponse = Result<Results<MangaRelationObject>>;
pub type MangaStatisticsResponse = Result<MangaStatisticsObject>;
pub type RatingsResponse = Result<RatingsList>;
pub type ReportReasonObject = ApiObjectNoRelationships<ReportReasonAttributes>;
pub type ReportReasonListResponse = Result<Results<ReportReasonObject>>;
pub type TagObject = ApiObject<TagAttributes>;
pub type TagData = ApiData<TagObject>;
pub type TagResponse = Result<TagData>;
pub type TagListResponse = Result<Results<TagObject>>;
pub type UploadSessionFileObject = ApiObject<UploadSessionFileAttributes>;
pub type UploadSessionFileResponse = Result<UploadSessionFileData<UploadSessionFileObject>>;
pub type UserObject = ApiObject<UserAttributes>;
pub type UserData = ApiData<UserObject>;
pub type UserResponse = Result<UserData>;
pub type UserListResponse = Result<Results<UserObject>>;
pub type UserReportsObject = ApiObject<UserReportAttributes>;
pub type UserReportsData = ApiData<UserReportsObject>;
pub type UserReportsListResponse = Result<Results<UserReportsObject>>;
pub type UserSettingsResponse = Result<UserSettingsAttributes>;
#[derive(Debug, Deserialize, Clone)]
#[allow(clippy::large_enum_variant)]
#[serde(untagged)]
pub enum RelatedAttributes {
Manga(MangaAttributes),
Chapter(ChapterAttributes),
CoverArt(CoverAttributes),
Author(AuthorAttributes),
ScanlationGroup(ScanlationGroupAttributes),
Tag(TagAttributes),
User(UserAttributes),
CustomList(CustomListAttributes),
}
#[derive(Debug, Deserialize, Clone)]
pub struct Relationship {
pub id: Uuid,
#[serde(rename = "type")]
pub type_: RelationshipType,
pub related: Option<MangaRelation>,
pub attributes: Option<RelatedAttributes>,
}
#[derive(Debug, Deserialize, Clone)]
pub struct Results<T> {
pub response: ResponseType,
pub data: Vec<T>,
pub limit: u32,
pub offset: u32,
pub total: u32,
}
pub type LocalizedString = HashMap<Language, String>;
pub(crate) mod localizedstring_array_or_map {
use std::collections::HashMap;
use serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
use super::LocalizedString;
pub fn deserialize<'de, D>(deserializer: D) -> Result<LocalizedString, D::Error>
where
D: Deserializer<'de>,
{
struct V;
impl<'de> Visitor<'de> for V {
type Value = LocalizedString;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("array or object")
}
fn visit_seq<A>(self, mut _seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
Ok(HashMap::new())
}
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let de = serde::de::value::MapAccessDeserializer::new(map);
let helper = LocalizedString::deserialize(de)?;
Ok(helper)
}
}
deserializer.deserialize_any(V)
}
}
pub(crate) mod volume_aggregate_array_or_map {
use std::collections::BTreeMap;
use serde::de::{Deserializer, MapAccess, SeqAccess, Visitor};
use super::manga_aggregate::VolumeAggregate;
type VolumeAggregateCollection = Vec<VolumeAggregate>;
const PAD_WIDTH: usize = 5;
pub fn deserialize<'de, D>(deserializer: D) -> Result<VolumeAggregateCollection, D::Error>
where
D: Deserializer<'de>,
{
struct V;
impl<'de> Visitor<'de> for V {
type Value = VolumeAggregateCollection;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("array or object")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut volumes = Vec::new();
while let Some(volume) = seq.next_element::<VolumeAggregate>()? {
volumes.push(volume);
}
Ok(volumes)
}
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let mut sorting_map = BTreeMap::new();
while let Some((volume_number, volume)) =
map.next_entry::<String, VolumeAggregate>()?
{
let volume_number = if volume_number.contains('.') {
match volume_number.parse::<f64>() {
Ok(_) => {
let (i, f) = volume_number.split_once('.').unwrap();
let i = i.parse::<i32>().unwrap();
format!("{i:0PAD_WIDTH$}.{f}")
}
Err(_) => volume_number,
}
} else {
match volume_number.parse::<i32>() {
Ok(n) => format!("{n:0PAD_WIDTH$}"),
Err(_) => volume_number,
}
};
sorting_map.insert(volume_number, volume);
}
Ok(sorting_map.values().cloned().collect())
}
}
deserializer.deserialize_any(V)
}
}
pub(crate) mod chapter_aggregate_array_or_map {
use std::collections::BTreeMap;
use serde::de::{Deserializer, MapAccess, SeqAccess, Visitor};
use super::manga_aggregate::ChapterAggregate;
const PAD_WIDTH: usize = 5;
type ChapterAggregateCollection = Vec<ChapterAggregate>;
pub fn deserialize<'de, D>(deserializer: D) -> Result<ChapterAggregateCollection, D::Error>
where
D: Deserializer<'de>,
{
struct V;
impl<'de> Visitor<'de> for V {
type Value = ChapterAggregateCollection;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("array or object")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut chapters = Vec::new();
while let Some(chapter) = seq.next_element::<ChapterAggregate>()? {
chapters.push(chapter);
}
Ok(chapters)
}
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let mut sorting_map = BTreeMap::new();
while let Some((chapter_number, chapter)) =
map.next_entry::<String, ChapterAggregate>()?
{
let chapter_number = if chapter_number.contains('.') {
match chapter_number.parse::<f64>() {
Ok(_) => {
let (i, f) = chapter_number.split_once('.').unwrap();
let i = i.parse::<i32>().unwrap();
format!("{i:0PAD_WIDTH$}.{f}")
}
Err(_) => chapter_number,
}
} else {
match chapter_number.parse::<i32>() {
Ok(n) => format!("{n:0PAD_WIDTH$}"),
Err(_) => chapter_number,
}
};
sorting_map.insert(chapter_number, chapter);
}
Ok(sorting_map.values().cloned().collect())
}
}
deserializer.deserialize_any(V)
}
}
pub(crate) mod manga_links_array_or_struct {
use serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
use crate::v5::MangaLinks;
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<MangaLinks>, D::Error>
where
D: Deserializer<'de>,
{
struct OptionMangaLinksVisitor;
impl<'de> Visitor<'de> for OptionMangaLinksVisitor {
type Value = Option<MangaLinks>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("some or none")
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(None)
}
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
let manga_links = d.deserialize_any(MangaLinksVisitor)?;
let manga_links = if manga_links.has_no_links() {
None
} else {
Some(manga_links)
};
Ok(manga_links)
}
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(None)
}
}
struct MangaLinksVisitor;
impl<'de> Visitor<'de> for MangaLinksVisitor {
type Value = MangaLinks;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("array or map")
}
fn visit_seq<A>(self, mut _seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
Ok(Self::Value::default())
}
fn visit_map<M>(self, map: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
Deserialize::deserialize(serde::de::value::MapAccessDeserializer::new(map))
}
}
deserializer.deserialize_option(OptionMangaLinksVisitor)
}
}
pub(crate) mod language_array_or_skip_null {
use mangadex_api_types::Language;
use serde::de::{Deserializer, SeqAccess, Visitor};
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Language>, D::Error>
where
D: Deserializer<'de>,
{
struct V;
impl<'de> Visitor<'de> for V {
type Value = Vec<Language>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a sequence of languages")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut languages = Vec::new();
while let Some(language) = seq.next_element::<Option<Language>>()? {
if let Some(language) = language {
languages.push(language);
}
}
Ok(languages)
}
}
deserializer.deserialize_seq(V)
}
}