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
#[allow(unused_imports)]
use std::collections::HashMap;
use std::convert::From;
use std::error::Error;
use std::fmt;
use serde_json;
use requests::SlackWebRequestSender;
pub fn add<R>(client: &R,
token: &str,
request: &AddRequest)
-> Result<AddResponse, AddError<R::Error>>
where R: SlackWebRequestSender
{
let params = vec![Some(("token", token)),
Some(("file", request.file)),
Some(("comment", request.comment))];
let params = params.into_iter().filter_map(|x| x).collect::<Vec<_>>();
let url = ::get_slack_url_for_method("files.comments.add");
client
.send(&url, ¶ms[..])
.map_err(|err| AddError::Client(err))
.and_then(|result| {
serde_json::from_str::<AddResponse>(&result)
.map_err(|e| AddError::MalformedResponse(e))
})
.and_then(|o| o.into())
}
#[derive(Clone, Default, Debug)]
pub struct AddRequest<'a> {
pub file: &'a str,
pub comment: &'a str,
}
#[derive(Clone, Debug, Deserialize)]
pub struct AddResponse {
pub comment: Option<::FileComment>,
error: Option<String>,
#[serde(default)]
ok: bool,
}
impl<E: Error> Into<Result<AddResponse, AddError<E>>> for AddResponse {
fn into(self) -> Result<AddResponse, AddError<E>> {
if self.ok {
Ok(self)
} else {
Err(self.error
.as_ref()
.map(String::as_ref)
.unwrap_or("")
.into())
}
}
}
#[derive(Debug)]
pub enum AddError<E: Error> {
FileNotFound,
FileDeleted,
NoComment,
NotAuthed,
InvalidAuth,
AccountInactive,
InvalidArgName,
InvalidArrayArg,
InvalidCharset,
InvalidFormData,
InvalidPostType,
MissingPostType,
TeamAddedToOrg,
RequestTimeout,
MalformedResponse(serde_json::error::Error),
Unknown(String),
Client(E),
}
impl<'a, E: Error> From<&'a str> for AddError<E> {
fn from(s: &'a str) -> Self {
match s {
"file_not_found" => AddError::FileNotFound,
"file_deleted" => AddError::FileDeleted,
"no_comment" => AddError::NoComment,
"not_authed" => AddError::NotAuthed,
"invalid_auth" => AddError::InvalidAuth,
"account_inactive" => AddError::AccountInactive,
"invalid_arg_name" => AddError::InvalidArgName,
"invalid_array_arg" => AddError::InvalidArrayArg,
"invalid_charset" => AddError::InvalidCharset,
"invalid_form_data" => AddError::InvalidFormData,
"invalid_post_type" => AddError::InvalidPostType,
"missing_post_type" => AddError::MissingPostType,
"team_added_to_org" => AddError::TeamAddedToOrg,
"request_timeout" => AddError::RequestTimeout,
_ => AddError::Unknown(s.to_owned()),
}
}
}
impl<E: Error> fmt::Display for AddError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl<E: Error> Error for AddError<E> {
fn description(&self) -> &str {
match self {
&AddError::FileNotFound => "file_not_found: The requested file could not be found.",
&AddError::FileDeleted => "file_deleted: The requested file was previously deleted.",
&AddError::NoComment => "no_comment: The comment field was empty.",
&AddError::NotAuthed => "not_authed: No authentication token provided.",
&AddError::InvalidAuth => "invalid_auth: Invalid authentication token.",
&AddError::AccountInactive => "account_inactive: Authentication token is for a deleted user or team.",
&AddError::InvalidArgName => "invalid_arg_name: The method was passed an argument whose name falls outside the bounds of common decency. This includes very long names and names with non-alphanumeric characters other than _. If you get this error, it is typically an indication that you have made a very malformed API call.",
&AddError::InvalidArrayArg => "invalid_array_arg: The method was passed a PHP-style array argument (e.g. with a name like foo[7]). These are never valid with the Slack API.",
&AddError::InvalidCharset => "invalid_charset: The method was called via a POST request, but the charset specified in the Content-Type header was invalid. Valid charset names are: utf-8 iso-8859-1.",
&AddError::InvalidFormData => "invalid_form_data: The method was called via a POST request with Content-Type application/x-www-form-urlencoded or multipart/form-data, but the form data was either missing or syntactically invalid.",
&AddError::InvalidPostType => "invalid_post_type: The method was called via a POST request, but the specified Content-Type was invalid. Valid types are: application/x-www-form-urlencoded multipart/form-data text/plain.",
&AddError::MissingPostType => "missing_post_type: The method was called via a POST request and included a data payload, but the request did not include a Content-Type header.",
&AddError::TeamAddedToOrg => "team_added_to_org: The team associated with your request is currently undergoing migration to an Enterprise Organization. Web API and other platform operations will be intermittently unavailable until the transition is complete.",
&AddError::RequestTimeout => "request_timeout: The method was called via a POST request, but the POST data was either missing or truncated.",
&AddError::MalformedResponse(ref e) => e.description(),
&AddError::Unknown(ref s) => s,
&AddError::Client(ref inner) => inner.description(),
}
}
fn cause(&self) -> Option<&Error> {
match self {
&AddError::MalformedResponse(ref e) => Some(e),
&AddError::Client(ref inner) => Some(inner),
_ => None,
}
}
}
pub fn delete<R>(client: &R,
token: &str,
request: &DeleteRequest)
-> Result<DeleteResponse, DeleteError<R::Error>>
where R: SlackWebRequestSender
{
let params = vec![Some(("token", token)),
Some(("file", request.file)),
Some(("id", request.id))];
let params = params.into_iter().filter_map(|x| x).collect::<Vec<_>>();
let url = ::get_slack_url_for_method("files.comments.delete");
client
.send(&url, ¶ms[..])
.map_err(|err| DeleteError::Client(err))
.and_then(|result| {
serde_json::from_str::<DeleteResponse>(&result)
.map_err(|e| DeleteError::MalformedResponse(e))
})
.and_then(|o| o.into())
}
#[derive(Clone, Default, Debug)]
pub struct DeleteRequest<'a> {
pub file: &'a str,
pub id: &'a str,
}
#[derive(Clone, Debug, Deserialize)]
pub struct DeleteResponse {
error: Option<String>,
#[serde(default)]
ok: bool,
}
impl<E: Error> Into<Result<DeleteResponse, DeleteError<E>>> for DeleteResponse {
fn into(self) -> Result<DeleteResponse, DeleteError<E>> {
if self.ok {
Ok(self)
} else {
Err(self.error
.as_ref()
.map(String::as_ref)
.unwrap_or("")
.into())
}
}
}
#[derive(Debug)]
pub enum DeleteError<E: Error> {
FileNotFound,
FileDeleted,
CantDelete,
NotAuthed,
InvalidAuth,
AccountInactive,
InvalidArgName,
InvalidArrayArg,
InvalidCharset,
InvalidFormData,
InvalidPostType,
MissingPostType,
TeamAddedToOrg,
RequestTimeout,
MalformedResponse(serde_json::error::Error),
Unknown(String),
Client(E),
}
impl<'a, E: Error> From<&'a str> for DeleteError<E> {
fn from(s: &'a str) -> Self {
match s {
"file_not_found" => DeleteError::FileNotFound,
"file_deleted" => DeleteError::FileDeleted,
"cant_delete" => DeleteError::CantDelete,
"not_authed" => DeleteError::NotAuthed,
"invalid_auth" => DeleteError::InvalidAuth,
"account_inactive" => DeleteError::AccountInactive,
"invalid_arg_name" => DeleteError::InvalidArgName,
"invalid_array_arg" => DeleteError::InvalidArrayArg,
"invalid_charset" => DeleteError::InvalidCharset,
"invalid_form_data" => DeleteError::InvalidFormData,
"invalid_post_type" => DeleteError::InvalidPostType,
"missing_post_type" => DeleteError::MissingPostType,
"team_added_to_org" => DeleteError::TeamAddedToOrg,
"request_timeout" => DeleteError::RequestTimeout,
_ => DeleteError::Unknown(s.to_owned()),
}
}
}
impl<E: Error> fmt::Display for DeleteError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl<E: Error> Error for DeleteError<E> {
fn description(&self) -> &str {
match self {
&DeleteError::FileNotFound => "file_not_found: The requested file could not be found.",
&DeleteError::FileDeleted => "file_deleted: The requested file was previously deleted.",
&DeleteError::CantDelete => "cant_delete: The requested comment could not be deleted.",
&DeleteError::NotAuthed => "not_authed: No authentication token provided.",
&DeleteError::InvalidAuth => "invalid_auth: Invalid authentication token.",
&DeleteError::AccountInactive => "account_inactive: Authentication token is for a deleted user or team.",
&DeleteError::InvalidArgName => "invalid_arg_name: The method was passed an argument whose name falls outside the bounds of common decency. This includes very long names and names with non-alphanumeric characters other than _. If you get this error, it is typically an indication that you have made a very malformed API call.",
&DeleteError::InvalidArrayArg => "invalid_array_arg: The method was passed a PHP-style array argument (e.g. with a name like foo[7]). These are never valid with the Slack API.",
&DeleteError::InvalidCharset => "invalid_charset: The method was called via a POST request, but the charset specified in the Content-Type header was invalid. Valid charset names are: utf-8 iso-8859-1.",
&DeleteError::InvalidFormData => "invalid_form_data: The method was called via a POST request with Content-Type application/x-www-form-urlencoded or multipart/form-data, but the form data was either missing or syntactically invalid.",
&DeleteError::InvalidPostType => "invalid_post_type: The method was called via a POST request, but the specified Content-Type was invalid. Valid types are: application/x-www-form-urlencoded multipart/form-data text/plain.",
&DeleteError::MissingPostType => "missing_post_type: The method was called via a POST request and included a data payload, but the request did not include a Content-Type header.",
&DeleteError::TeamAddedToOrg => "team_added_to_org: The team associated with your request is currently undergoing migration to an Enterprise Organization. Web API and other platform operations will be intermittently unavailable until the transition is complete.",
&DeleteError::RequestTimeout => "request_timeout: The method was called via a POST request, but the POST data was either missing or truncated.",
&DeleteError::MalformedResponse(ref e) => e.description(),
&DeleteError::Unknown(ref s) => s,
&DeleteError::Client(ref inner) => inner.description(),
}
}
fn cause(&self) -> Option<&Error> {
match self {
&DeleteError::MalformedResponse(ref e) => Some(e),
&DeleteError::Client(ref inner) => Some(inner),
_ => None,
}
}
}
pub fn edit<R>(client: &R,
token: &str,
request: &EditRequest)
-> Result<EditResponse, EditError<R::Error>>
where R: SlackWebRequestSender
{
let params = vec![Some(("token", token)),
Some(("file", request.file)),
Some(("id", request.id)),
Some(("comment", request.comment))];
let params = params.into_iter().filter_map(|x| x).collect::<Vec<_>>();
let url = ::get_slack_url_for_method("files.comments.edit");
client
.send(&url, ¶ms[..])
.map_err(|err| EditError::Client(err))
.and_then(|result| {
serde_json::from_str::<EditResponse>(&result)
.map_err(|e| EditError::MalformedResponse(e))
})
.and_then(|o| o.into())
}
#[derive(Clone, Default, Debug)]
pub struct EditRequest<'a> {
pub file: &'a str,
pub id: &'a str,
pub comment: &'a str,
}
#[derive(Clone, Debug, Deserialize)]
pub struct EditResponse {
pub comment: Option<::FileComment>,
error: Option<String>,
#[serde(default)]
ok: bool,
}
impl<E: Error> Into<Result<EditResponse, EditError<E>>> for EditResponse {
fn into(self) -> Result<EditResponse, EditError<E>> {
if self.ok {
Ok(self)
} else {
Err(self.error
.as_ref()
.map(String::as_ref)
.unwrap_or("")
.into())
}
}
}
#[derive(Debug)]
pub enum EditError<E: Error> {
FileNotFound,
FileDeleted,
NoComment,
EditWindowClosed,
CantEdit,
NotAuthed,
InvalidAuth,
AccountInactive,
InvalidArgName,
InvalidArrayArg,
InvalidCharset,
InvalidFormData,
InvalidPostType,
MissingPostType,
TeamAddedToOrg,
RequestTimeout,
MalformedResponse(serde_json::error::Error),
Unknown(String),
Client(E),
}
impl<'a, E: Error> From<&'a str> for EditError<E> {
fn from(s: &'a str) -> Self {
match s {
"file_not_found" => EditError::FileNotFound,
"file_deleted" => EditError::FileDeleted,
"no_comment" => EditError::NoComment,
"edit_window_closed" => EditError::EditWindowClosed,
"cant_edit" => EditError::CantEdit,
"not_authed" => EditError::NotAuthed,
"invalid_auth" => EditError::InvalidAuth,
"account_inactive" => EditError::AccountInactive,
"invalid_arg_name" => EditError::InvalidArgName,
"invalid_array_arg" => EditError::InvalidArrayArg,
"invalid_charset" => EditError::InvalidCharset,
"invalid_form_data" => EditError::InvalidFormData,
"invalid_post_type" => EditError::InvalidPostType,
"missing_post_type" => EditError::MissingPostType,
"team_added_to_org" => EditError::TeamAddedToOrg,
"request_timeout" => EditError::RequestTimeout,
_ => EditError::Unknown(s.to_owned()),
}
}
}
impl<E: Error> fmt::Display for EditError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl<E: Error> Error for EditError<E> {
fn description(&self) -> &str {
match self {
&EditError::FileNotFound => "file_not_found: The requested file could not be found.",
&EditError::FileDeleted => "file_deleted: The requested file was previously deleted.",
&EditError::NoComment => "no_comment: The comment field was empty.",
&EditError::EditWindowClosed => "edit_window_closed: The timeframe for editing the comment has expired.",
&EditError::CantEdit => "cant_edit: The requested file could not be found.",
&EditError::NotAuthed => "not_authed: No authentication token provided.",
&EditError::InvalidAuth => "invalid_auth: Invalid authentication token.",
&EditError::AccountInactive => "account_inactive: Authentication token is for a deleted user or team.",
&EditError::InvalidArgName => "invalid_arg_name: The method was passed an argument whose name falls outside the bounds of common decency. This includes very long names and names with non-alphanumeric characters other than _. If you get this error, it is typically an indication that you have made a very malformed API call.",
&EditError::InvalidArrayArg => "invalid_array_arg: The method was passed a PHP-style array argument (e.g. with a name like foo[7]). These are never valid with the Slack API.",
&EditError::InvalidCharset => "invalid_charset: The method was called via a POST request, but the charset specified in the Content-Type header was invalid. Valid charset names are: utf-8 iso-8859-1.",
&EditError::InvalidFormData => "invalid_form_data: The method was called via a POST request with Content-Type application/x-www-form-urlencoded or multipart/form-data, but the form data was either missing or syntactically invalid.",
&EditError::InvalidPostType => "invalid_post_type: The method was called via a POST request, but the specified Content-Type was invalid. Valid types are: application/x-www-form-urlencoded multipart/form-data text/plain.",
&EditError::MissingPostType => "missing_post_type: The method was called via a POST request and included a data payload, but the request did not include a Content-Type header.",
&EditError::TeamAddedToOrg => "team_added_to_org: The team associated with your request is currently undergoing migration to an Enterprise Organization. Web API and other platform operations will be intermittently unavailable until the transition is complete.",
&EditError::RequestTimeout => "request_timeout: The method was called via a POST request, but the POST data was either missing or truncated.",
&EditError::MalformedResponse(ref e) => e.description(),
&EditError::Unknown(ref s) => s,
&EditError::Client(ref inner) => inner.description(),
}
}
fn cause(&self) -> Option<&Error> {
match self {
&EditError::MalformedResponse(ref e) => Some(e),
&EditError::Client(ref inner) => Some(inner),
_ => None,
}
}
}