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
#[cfg(feature = "chrono")]
extern crate chrono;
extern crate itoa;
extern crate serde;
extern crate serde_repr;
pub mod chapter_api;
pub mod genres;
pub mod langs;
pub mod manga_api;
pub mod manga_follows_api;
pub mod manga_norm;
mod set;
pub use chapter_api::Chapter;
pub use genres::{Genre, GenreSet};
pub use langs::{Language, LanguageSet};
pub use manga_api::Manga;
use serde::Deserialize;
#[derive(Debug, Deserialize, Clone, PartialEq)]
#[serde(untagged)]
pub enum APIResponse {
Chapter(Chapter),
Manga(Manga),
}
impl APIResponse {
pub fn is_chapter(&self) -> bool {
match self {
APIResponse::Chapter(_) => true,
_ => false,
}
}
pub fn is_manga(&self) -> bool {
!self.is_chapter()
}
pub fn chapter(self) -> Option<Chapter> {
match self {
APIResponse::Chapter(ch) => Some(ch),
_ => None,
}
}
pub fn chapter_ok(self) -> Option<chapter_api::OK> {
if let APIResponse::Chapter(ch) = self {
ch.ok()
} else {
None
}
}
pub fn manga(self) -> Option<Manga> {
match self {
APIResponse::Manga(mn) => Some(mn),
_ => None,
}
}
pub fn manga_ok(self) -> Option<manga_api::Data> {
if let APIResponse::Manga(mng) = self {
mng.ok()
} else {
None
}
}
}
#[cfg(test)]
pub mod test_data {
pub type R = Result<(), Box<serde_json::Error>>;
pub const TEST_CHAPTER_DELETED: &str =
include_str!("../testdata/chapter/1.json");
pub const TEST_CHAPTER_OK: &str = include_str!("../testdata/chapter/2.json");
pub const TEST_CHAPTER_OK_2: &str =
include_str!("../testdata/chapter/42.json");
pub const TEST_CHAPTER_UNAVAILABLE: &str =
include_str!("../testdata/chapter/614094.json");
pub const TEST_CHAPTER_ERROR: &str =
include_str!("../testdata/chapter/9999999.json");
pub const TEST_CHAPTER_DELAYED: &str =
include_str!("../testdata/chapter/672314.json");
pub const TEST_CHAPTER_EXTERNAL: &str =
include_str!("../testdata/chapter/670622.json");
pub const TEST_CHAPTER_ALL_TESTS: [&str; 7] = [
TEST_CHAPTER_DELETED,
TEST_CHAPTER_OK,
TEST_CHAPTER_OK_2,
TEST_CHAPTER_UNAVAILABLE,
TEST_CHAPTER_ERROR,
TEST_CHAPTER_DELAYED,
TEST_CHAPTER_EXTERNAL,
];
pub const TEST_MANGA_OK: &str = include_str!("../testdata/manga/47.json");
pub const TEST_MANGA_OK_2: &str = include_str!("../testdata/manga/99.json");
pub const TEST_MANGA_UNAVAILABLE: &str =
include_str!("../testdata/manga/420.json");
pub const TEST_MANGA_LARGE: &str = include_str!("../testdata/manga/1.json");
pub const TEST_MANGA_NO_META_OR_CH: &str =
include_str!("../testdata/manga/39000.json");
pub const TEST_MANGA_ALL_TESTS: [&str; 5] = [
TEST_MANGA_OK,
TEST_MANGA_OK_2,
TEST_MANGA_UNAVAILABLE,
TEST_MANGA_LARGE,
TEST_MANGA_NO_META_OR_CH,
];
pub const TEST_FOLLOWS_OK: &str =
include_str!("../testdata/follows/data.json");
pub const TEST_FOLLOWS_ERR: &str =
include_str!("../testdata/follows/err.json");
}
#[cfg(test)]
mod api_response {
use super::APIResponse;
use crate::test_data::*;
use serde_json::from_str;
#[test]
fn it_decodes_a_manga_generically() -> R {
for mn in &TEST_MANGA_ALL_TESTS {
let manga: APIResponse = from_str(mn)?;
assert!(manga.is_manga());
eprintln!("{:?}", manga);
}
Ok(())
}
#[test]
fn it_decodes_a_chapter_generically() -> R {
for ch in &TEST_CHAPTER_ALL_TESTS {
let chapter: APIResponse = from_str(ch)?;
assert!(chapter.is_chapter());
eprintln!("{:?}", chapter);
}
Ok(())
}
#[test]
fn it_decodes_a_manga_generically_and_gives_an_ok_manga() -> R {
let manga: APIResponse = from_str(TEST_MANGA_OK)?;
let mng = manga.manga_ok();
assert!(mng.is_some());
eprintln!("{:?}", mng);
Ok(())
}
#[test]
fn it_decodes_a_chapter_generically_and_gives_an_ok_chapter() -> R {
let chapter: APIResponse = from_str(TEST_CHAPTER_OK)?;
let ch = chapter.chapter_ok();
assert!(ch.is_some());
eprintln!("{:?}", ch);
Ok(())
}
}