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
use std::{
convert::{TryFrom, TryInto},
str::FromStr,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::types::{Post, SearchResults, Topic, User};
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetProfileRequest {
pub short_name: Option<String>,
pub id: Option<String>,
pub get_stats: bool,
pub get_tags: bool,
pub curated: Option<u32>,
pub curable: Option<u32>,
pub ncomments: Option<u32>,
pub get_followed_topics: bool,
pub get_curated_topics: bool,
pub filter_curated_topics_by_creation_date_from: Option<u64>,
pub filter_curated_topics_by_creation_date_to: Option<u64>,
pub get_creator: bool,
}
impl Default for GetProfileRequest {
fn default() -> Self {
Self {
short_name: None,
id: None,
get_stats: false,
get_tags: false,
curated: Some(0),
curable: Some(0),
ncomments: Some(0),
get_followed_topics: false,
get_curated_topics: true,
filter_curated_topics_by_creation_date_from: None,
filter_curated_topics_by_creation_date_to: None,
get_creator: false,
}
}
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetTopicRequest {
pub id: Option<u64>,
pub url_name: Option<String>,
pub curated: Option<u32>,
pub page: Option<u32>,
pub curable: Option<u32>,
pub curable_page: Option<u32>,
pub order: Option<String>,
pub tag: Option<Vec<String>>,
pub q: Option<String>,
pub since: Option<i64>,
pub to: Option<i64>,
pub ncomments: Option<u32>,
pub show_scheduled: bool,
}
impl Default for GetTopicRequest {
fn default() -> Self {
Self {
id: None,
url_name: None,
curated: Some(30),
page: None,
curable: Some(0),
curable_page: None,
order: None,
tag: None,
q: None,
since: None,
to: None,
ncomments: Some(100),
show_scheduled: false,
}
}
}
pub trait GetRequest: Serialize {
type Response: TryInto<Self::Output, Error = anyhow::Error> + DeserializeOwned;
type Output;
fn endpoint() -> &'static str;
}
impl GetRequest for GetTopicRequest {
type Response = TopicResponse;
type Output = Topic;
fn endpoint() -> &'static str {
"topic"
}
}
impl GetRequest for GetProfileRequest {
type Response = UserResponse;
type Output = User;
fn endpoint() -> &'static str {
"profile"
}
}
#[derive(Deserialize)]
pub struct TopicResponse {
pub topic: Option<Topic>,
pub error: Option<String>,
}
#[derive(Deserialize)]
pub struct UserResponse {
pub user: Option<User>,
pub error: Option<String>,
}
impl TryFrom<UserResponse> for User {
type Error = anyhow::Error;
fn try_from(value: UserResponse) -> Result<Self, Self::Error> {
if let Some(error) = value.error {
Err(anyhow::anyhow!("Server returned an error: {}", error))
} else {
value
.user
.ok_or(anyhow::anyhow!("No user nor error in response body!"))
}
}
}
impl TryFrom<TopicResponse> for Topic {
type Error = anyhow::Error;
fn try_from(value: TopicResponse) -> Result<Self, Self::Error> {
if let Some(error) = value.error {
Err(anyhow::anyhow!("Server returned an error: {}", error))
} else {
value
.topic
.ok_or(anyhow::anyhow!("No user no topic in response body!"))
}
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum SearchRequestType {
User,
Topic,
Post,
}
impl FromStr for SearchRequestType {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"user" => Ok(SearchRequestType::User),
"topic" => Ok(SearchRequestType::Topic),
"post" => Ok(SearchRequestType::Post),
other => Err(anyhow::anyhow!("Invalid request type: {}", other)),
}
}
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SearchRequest {
#[serde(rename = "type")]
pub search_type: SearchRequestType,
pub query: String,
pub count: Option<u32>,
pub page: Option<u32>,
pub lang: Option<String>,
pub topic_id: Option<u32>,
pub get_tags: bool,
pub get_creator: bool,
pub get_stats: bool,
pub get_tags_for_topic: bool,
pub get_stats_for_topic: bool,
}
impl Default for SearchRequest {
fn default() -> Self {
Self {
search_type: SearchRequestType::Post,
query: "".to_string(),
count: Some(50),
page: None,
lang: None,
topic_id: None,
get_tags: false,
get_creator: true,
get_stats: false,
get_tags_for_topic: false,
get_stats_for_topic: false,
}
}
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SearchResponse {
pub users: Option<Vec<User>>,
pub topics: Option<Vec<Topic>>,
pub posts: Option<Vec<Post>>,
pub total_found: i32,
}
impl GetRequest for SearchRequest {
type Response = SearchResponse;
type Output = SearchResults;
fn endpoint() -> &'static str {
"search"
}
}
impl TryFrom<SearchResponse> for SearchResults {
type Error = anyhow::Error;
fn try_from(value: SearchResponse) -> Result<Self, Self::Error> {
let SearchResponse {
users,
topics,
posts,
total_found,
} = value;
Ok(SearchResults {
users,
topics,
posts,
total_found,
})
}
}
#[derive(Serialize, Debug, Default)]
pub struct TestRequest {
_dummy: (),
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TestResponse {
connected_user: Option<User>,
error: Option<String>,
}
impl GetRequest for TestRequest {
type Response = TestResponse;
type Output = Option<User>;
fn endpoint() -> &'static str {
"test"
}
}
impl TryFrom<TestResponse> for Option<User> {
type Error = anyhow::Error;
fn try_from(value: TestResponse) -> Result<Self, Self::Error> {
if let Some(error) = value.error {
Err(anyhow::anyhow!("Server returned an error: {}", error))
} else {
Ok(value.connected_user)
}
}
}