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
use serde::{Deserialize, Serialize};
use crate::{
directory::{DirectoryItem, DirectoryKind},
tag::TagItem,
utils::name_to_permalink,
DynastyReaderRoute, DYNASTY_READER_BASE,
};
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DirectoryListConfig {
pub name: String,
pub kind: DirectoryKind,
pub view_kind: Option<DirectoryListViewKind>,
pub page_number: u64,
}
impl DirectoryListConfig {
pub fn with_view_kind(self, view_kind: Option<DirectoryListViewKind>) -> DirectoryListConfig {
DirectoryListConfig {
name: self.name,
kind: self.kind,
view_kind,
page_number: self.page_number,
}
}
}
impl From<DirectoryItem> for DirectoryListConfig {
fn from(item: DirectoryItem) -> Self {
DirectoryListConfig {
name: item.name,
kind: item.kind,
view_kind: None,
page_number: 1,
}
}
}
impl From<TagItem> for DirectoryListConfig {
fn from(item: TagItem) -> Self {
DirectoryListConfig {
name: item.name,
kind: item.kind,
view_kind: None,
page_number: 1,
}
}
}
impl DynastyReaderRoute for DirectoryListConfig {
fn request_builder(
&self,
client: &reqwest::Client,
url: reqwest::Url,
) -> reqwest::RequestBuilder {
let builder = client.get(url).query(&[("page", self.page_number)]);
if let Some(view_kind) = self.view_kind {
builder.query(&[("view", view_kind.to_string())])
} else {
builder
}
}
fn request_url(&self) -> reqwest::Url {
let permalink = name_to_permalink(&self.name);
DYNASTY_READER_BASE
.join(&format!("{}/{}.json", self.kind, permalink))
.unwrap()
}
}
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DirectoryListViewKind {
Chapters,
Groupings,
OneShots,
Pairings,
}
impl std::fmt::Display for DirectoryListViewKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = {
use DirectoryListViewKind::*;
match self {
Chapters => "chapters",
Groupings => "groupings",
OneShots => "one_shots",
Pairings => "pairings",
}
};
write!(f, "{s}")
}
}
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct DirectoryList {
pub name: String,
pub kind: DirectoryKind,
pub permalink: String,
pub tags: Vec<TagItem>,
pub status: Option<DirectoryListStatus>,
pub cover: Option<String>,
pub link: Option<String>,
pub description: Option<String>,
pub aliases: Vec<String>,
pub items: Vec<DirectoryListItem>,
pub chapter_items: Vec<DirectoryListChapterItem>,
pub page_number: u64,
pub max_page_number: u64,
}
impl<'de> Deserialize<'de> for DirectoryList {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum DirectoryListChapterItemWrapper {
Chapter {
title: String,
permalink: String,
released_on: String,
tags: Vec<TagItem>,
},
Header {
header: Option<String>,
},
}
#[derive(Deserialize)]
#[serde(untagged)]
enum TagWrapper {
Default(TagItem),
Status(DirectoryListStatus),
}
#[derive(Deserialize)]
struct DirectoryListWrapper {
name: String,
#[serde(rename(deserialize = "type"))]
kind: DirectoryKind,
permalink: String,
tags: Vec<TagWrapper>,
#[serde(deserialize_with = "crate::utils::join_path_with_dynasty_reader_base")]
cover: String,
link: Option<String>,
description: Option<String>,
aliases: Vec<String>,
#[serde(default)]
taggables: Vec<DirectoryListItem>,
#[serde(default)]
taggings: Vec<DirectoryListChapterItemWrapper>,
current_page: Option<u64>,
total_pages: Option<u64>,
}
let wrapper: DirectoryListWrapper = Deserialize::deserialize(deserializer)?;
let DirectoryListWrapper {
name,
kind,
permalink,
tags,
cover,
link,
description,
aliases,
taggables: items,
taggings: chapter_items,
current_page: page_number,
total_pages: max_page_number,
} = wrapper;
let page_number = page_number.unwrap_or(1);
let max_page_number = max_page_number.unwrap_or(1);
let cover = if cover.is_empty() { None } else { Some(cover) };
let status = tags.iter().find_map(|tag| match tag {
TagWrapper::Status(s) => Some(s.clone()),
_ => None,
});
let tags = tags
.iter()
.filter_map(|tag| match tag {
TagWrapper::Default(s) => Some(s.clone()),
_ => None,
})
.collect();
let chapter_items = {
let mut current_header: Option<String> = None;
chapter_items.into_iter().fold(vec![], |mut current, item| {
use DirectoryListChapterItemWrapper::*;
match item {
Chapter {
title,
permalink,
released_on,
tags,
} => current.push(DirectoryListChapterItem {
header: current_header.clone(),
permalink,
released_on,
title,
tags,
}),
Header { header } => current_header = header,
}
current
})
};
Ok(DirectoryList {
name,
kind,
permalink,
tags,
status,
cover,
link,
description,
aliases,
items,
chapter_items,
page_number,
max_page_number,
})
}
}
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub enum DirectoryListStatus {
Abandoned,
Cancelled,
Completed,
Licensed,
Ongoing,
Unknown,
}
impl<'de> Deserialize<'de> for DirectoryListStatus {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct DirectoryListStatusWrapper<'a> {
name: &'a str,
}
let wrapper: DirectoryListStatusWrapper = Deserialize::deserialize(deserializer)?;
Ok({
use DirectoryListStatus::*;
match wrapper.name {
"Abandoned" => Abandoned,
"Cancelled" => Cancelled,
"Completed" => Completed,
"Licensed" => Licensed,
"Ongoing" => Ongoing,
_ => Unknown,
}
})
}
}
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct DirectoryListItem {
pub name: String,
pub kind: DirectoryKind,
pub permalink: String,
pub cover: Option<String>,
}
impl<'de> Deserialize<'de> for DirectoryListItem {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct DirectoryListItemWrapper {
name: String,
#[serde(rename(deserialize = "type"))]
kind: DirectoryKind,
permalink: String,
#[serde(deserialize_with = "crate::utils::join_path_with_dynasty_reader_base")]
cover: String,
}
let wrapper: DirectoryListItemWrapper = Deserialize::deserialize(deserializer)?;
let DirectoryListItemWrapper {
name,
kind,
permalink,
cover,
} = wrapper;
let cover = if cover.is_empty() { None } else { Some(cover) };
Ok(DirectoryListItem {
name,
kind,
permalink,
cover,
})
}
}
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct DirectoryListChapterItem {
pub title: String,
pub header: Option<String>,
pub permalink: String,
pub released_on: String,
pub tags: Vec<TagItem>,
}
#[cfg(test)]
mod tests {
use anyhow::Result;
use crate::test_utils::tryhard_configs;
use super::*;
fn create_config(
c: (
DirectoryKind,
&str,
impl Into<Option<DirectoryListViewKind>>,
),
) -> DirectoryListConfig {
DirectoryListConfig {
name: c.1.to_string(),
kind: c.0,
page_number: 1,
view_kind: c.2.into(),
}
}
fn create_config_with_superset_view_kind(c: (DirectoryKind, &str)) -> Vec<DirectoryListConfig> {
[
DirectoryListViewKind::Chapters,
DirectoryListViewKind::Groupings,
DirectoryListViewKind::OneShots,
DirectoryListViewKind::Pairings,
]
.map(|view_kind| create_config((c.0, c.1, view_kind)))
.to_vec()
}
#[tokio::test]
#[ignore = "requires internet"]
async fn response_structure() -> Result<()> {
let configs = [
(DirectoryKind::Doujin, "Bloom Into You"),
(DirectoryKind::Pairing, "Homura x Madoka"),
(DirectoryKind::Tag, "Aaaaaangst"),
]
.map(create_config_with_superset_view_kind)
.into_iter()
.flatten()
.collect::<Vec<_>>();
tryhard_configs(configs, |client, config| client.directory_list(config)).await?;
Ok(())
}
#[tokio::test]
#[ignore = "requires internet"]
async fn viewless_response_structure() -> Result<()> {
let configs = [
(DirectoryKind::Anthology, "And Then, To You", None),
(DirectoryKind::Author, "Nakatani Nio", None),
(DirectoryKind::Issue, "Aya Yuri Vol 11", None),
(DirectoryKind::Scanlator, "/u/ Scanlations", None),
(
DirectoryKind::Series,
"Arknights Official Comic Anthology",
None,
),
]
.map(create_config);
tryhard_configs(configs, |client, config| client.directory_list(config)).await?;
Ok(())
}
}