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
pub mod v5;
use std::borrow::Cow;
use mangadex_api_types::error::schema::MangaDexErrorResponse;
use mangadex_api_types::error::Error;
use mangadex_api_types::{RelationshipType, ResponseType};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Deserializer, Serialize};
use uuid::Uuid;
use crate::v5::Relationship;
pub trait UrlSerdeQS {
fn query_qs<T: Serialize>(self, query: &T) -> Self;
}
impl UrlSerdeQS for url::Url {
fn query_qs<T: Serialize>(mut self, query: &T) -> Self {
self.set_query(Some(
&serde_qs::to_string(query).expect("failed to encode query string"),
));
self
}
}
pub trait FromResponse: Sized {
type Response;
fn from_response(res: Self::Response) -> Self;
}
pub trait Endpoint {
type Query: Serialize;
type Body: Serialize;
type Response: FromResponse;
fn method(&self) -> reqwest::Method {
reqwest::Method::GET
}
fn path(&self) -> Cow<str>;
fn require_auth(&self) -> bool {
false
}
fn query(&self) -> Option<&Self::Query> {
None
}
fn body(&self) -> Option<&Self::Body> {
None
}
fn multipart(&self) -> Option<reqwest::multipart::Form> {
None
}
}
#[derive(Deserialize)]
#[serde(tag = "result", remote = "std::result::Result")]
enum ApiResultDef<T, E> {
#[serde(rename = "ok")]
Ok(T),
#[serde(rename = "error")]
Err(E),
}
#[derive(Deserialize)]
#[serde(bound = "T: DeserializeOwned, E: DeserializeOwned")]
pub struct ApiResult<T, E = MangaDexErrorResponse>(
#[serde(with = "ApiResultDef")] std::result::Result<T, E>,
);
impl<T, E> ApiResult<T, E> {
pub fn into_result(self) -> Result<T, E> {
self.0
}
}
#[derive(Debug, Deserialize, Clone)]
pub struct ApiData<T> {
pub response: ResponseType,
pub data: T,
}
#[derive(Debug, Default, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ApiObject<A, T = RelationshipType> {
pub id: Uuid,
pub type_: T,
pub attributes: A,
pub relationships: Vec<Relationship>,
}
impl<A, T> FromResponse for ApiObject<A, T> {
type Response = Self;
fn from_response(value: Self::Response) -> Self {
value
}
}
#[derive(Debug, Default, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ApiObjectNoRelationships<A, T = RelationshipType> {
pub id: Uuid,
pub type_: T,
pub attributes: A,
}
impl<A, T> FromResponse for ApiObjectNoRelationships<A, T> {
type Response = Self;
fn from_response(value: Self::Response) -> Self {
value
}
}
#[derive(Debug, Default, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct NoData;
impl<T> FromResponse for Result<T, Error> {
type Response = ApiResult<T, MangaDexErrorResponse>;
fn from_response(value: Self::Response) -> Self {
value.into_result().map_err(|e| e.into())
}
}
impl<T> FromResponse for Vec<Result<T, Error>> {
type Response = Vec<ApiResult<T, MangaDexErrorResponse>>;
fn from_response(value: Self::Response) -> Self {
value
.into_iter()
.map(|r| r.into_result().map_err(|e| e.into()))
.collect()
}
}
pub(crate) fn deserialize_null_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
T: Default + Deserialize<'de>,
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or_default())
}