scratch-io 0.1.4

A rust library for managing, downloading, and launching games from itch.io
Documentation
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
use thiserror::Error;

const ERROR_INVALID_API_KEY: &str = "invalid key";
const ERROR_INVALID_USER_OR_PASSWORD: &[&str] = &[
  "Incorrect username or password",
  "username must be provided",
  "password must be provided",
];
const ERROR_INVALID_CAPTCHA_CODE: &[&str] = &[
  "Please correctly complete reCAPTCHA",
  "Please complete reCAPTCHA to continue",
];
const ERROR_INVALID_TOTP_CODE: &str = "invalid code";
const ERROR_TOTP_TOKEN_TIMED_OUT: &str = "two-factor login attempt timed out";
const ERROR_INVALID_TOTP_TOKEN: &str = "invalid token";
const ERROR_INVALID_USER: &[&str] = &["invalid user", "user_id: expected database ID integer"];
const ERROR_INVALID_COLLECTION: &[&str] = &[
  "invalid collection",
  "collection_id: expected database id",
  "collection_id: expected integer",
];
const ERROR_INVALID_GAME: &[&str] = &[
  "invalid game",
  "game_id: expected database id",
  "game_id: expected integer",
];
const ERROR_INVALID_UPLOAD: &[&str] = &[
  "invalid upload",
  "upload_id: expected database id",
  "upload_id: expected integer",
];
const ERROR_INVALID_BUILD: &[&str] = &[
  "invalid build",
  "build_id: expected database id",
  "build_id: expected integer",
];
const ERROR_INVALID_TARGET_BUILD: &str =
  "target_build_id: expected empty, or integer then database id";
const ERROR_NO_UPGRADE_PATH: &str = "no upgrade path";

/// Error returned from [`super::ItchClient::itch_request_json`]
#[derive(Error, Debug)]
#[error("An API call to \"{url}\" failed:\n{kind}")]
pub struct ItchRequestJSONError<T>
where
  T: std::error::Error + std::fmt::Debug,
{
  pub url: String,
  #[source]
  pub kind: ItchRequestJSONErrorKind<T>,
}

#[derive(Error, Debug)]
pub enum ItchRequestJSONErrorKind<T>
where
  T: std::error::Error + std::fmt::Debug,
{
  #[error(
    "Error while sending request, redirect loop was detected or redirect limit was exhausted:\n{0}"
  )]
  CouldntSend(#[source] reqwest::Error),

  #[error("Couldn't get the network request response body:\n{0}")]
  CouldntGetText(#[source] reqwest::Error),

  #[error("Couldn't parse the request response body into JSON:\n{body}\n\n{error}")]
  InvalidJSON {
    body: String,
    #[source]
    error: serde_json::Error,
  },

  #[error("The itch.io API server returned an error:\n{0}")]
  ServerRepliedWithError(T),
}

#[derive(Error, Debug)]
#[error("The provided API key is invalid!")]
pub struct InvalidApiKey;

#[derive(Error, Debug)]
#[error("The username or the password is incorrect.")]
pub struct IncorrectUsernameOrPassword;

#[derive(Error, Debug)]
#[error("The reCAPTCHA response code is incorrect!")]
pub struct IncorrectCaptchaCode;

#[derive(Error, Debug)]
#[error("The TOTP code is incorrect!")]
pub struct IncorrectTOTPCode;

#[derive(Error, Debug)]
#[error(
  "The TOTP token timed out!
Login again with a username and password to get another TOTP token."
)]
pub struct TOTPTokenTimedOut;

#[derive(Error, Debug)]
#[error("The TOTP token is invalid!")]
pub struct InvalidTOTPToken;

#[derive(Error, Debug)]
#[error("The provided user ID is invalid.")]
pub struct InvalidUserID;

#[derive(Error, Debug)]
#[error("The provided collection ID is invalid.")]
pub struct InvalidCollectionID;

#[derive(Error, Debug)]
#[error("The provided game ID is invalid.")]
pub struct InvalidGameID;

#[derive(Error, Debug)]
#[error("The provided upload ID is invalid.")]
pub struct InvalidUploadID;

#[derive(Error, Debug)]
#[error("The provided build ID is invalid.")]
pub struct InvalidBuildID;

#[derive(Error, Debug)]
#[error("The provided target build ID is invalid.")]
pub struct InvalidTargetBuildID;

#[derive(Error, Debug)]
#[error("No upgrade path was found.")]
pub struct NoUpgradePath;

/// All possible errors returned from the Itch.io API
#[derive(Error, Debug)]
pub enum ApiResponseErrorKind {
  #[error(transparent)]
  InvalidApiKey(#[from] InvalidApiKey),

  #[error(transparent)]
  IncorrectUsernameOrPassword(#[from] IncorrectUsernameOrPassword),

  #[error(transparent)]
  IncorrectCaptchaCode(#[from] IncorrectCaptchaCode),

  #[error(transparent)]
  IncorrectTOTPCode(#[from] IncorrectTOTPCode),

  #[error(transparent)]
  TOTPTokenTimedOut(#[from] TOTPTokenTimedOut),

  #[error(transparent)]
  InvalidTOTPToken(#[from] InvalidTOTPToken),

  #[error(transparent)]
  InvalidUserID(#[from] InvalidUserID),

  #[error(transparent)]
  InvalidCollectionID(#[from] InvalidCollectionID),

  #[error(transparent)]
  InvalidGameID(#[from] InvalidGameID),

  #[error(transparent)]
  InvalidUploadID(#[from] InvalidUploadID),

  #[error(transparent)]
  InvalidBuildID(#[from] InvalidBuildID),

  #[error(transparent)]
  InvalidTargetBuildID(#[from] InvalidTargetBuildID),

  #[error(transparent)]
  NoUpgradePath(#[from] NoUpgradePath),

  #[error("An unknown error occurred!")]
  Other,
}

impl From<&[String]> for ApiResponseErrorKind {
  fn from(value: &[String]) -> Self {
    match value {
      [v] if v == ERROR_INVALID_API_KEY => Self::InvalidApiKey(InvalidApiKey),
      [v, ..] if ERROR_INVALID_USER_OR_PASSWORD.contains(&&**v) => {
        Self::IncorrectUsernameOrPassword(IncorrectUsernameOrPassword)
      }
      [v] if ERROR_INVALID_CAPTCHA_CODE.contains(&&**v) => {
        Self::IncorrectCaptchaCode(IncorrectCaptchaCode)
      }
      [v] if v == ERROR_INVALID_TOTP_CODE => Self::IncorrectTOTPCode(IncorrectTOTPCode),
      [v] if v == ERROR_TOTP_TOKEN_TIMED_OUT => Self::TOTPTokenTimedOut(TOTPTokenTimedOut),
      [v] if v == ERROR_INVALID_TOTP_TOKEN => Self::InvalidTOTPToken(InvalidTOTPToken),
      [v] if ERROR_INVALID_USER.contains(&&**v) => Self::InvalidUserID(InvalidUserID),
      [v] if ERROR_INVALID_COLLECTION.contains(&&**v) => {
        Self::InvalidCollectionID(InvalidCollectionID)
      }
      [v] if ERROR_INVALID_GAME.contains(&&**v) => Self::InvalidGameID(InvalidGameID),
      [v] if ERROR_INVALID_UPLOAD.contains(&&**v) => Self::InvalidUploadID(InvalidUploadID),
      [v] if ERROR_INVALID_BUILD.contains(&&**v) => Self::InvalidBuildID(InvalidBuildID),
      [v] if ERROR_INVALID_TARGET_BUILD == v => Self::InvalidTargetBuildID(InvalidTargetBuildID),
      [v] if v == ERROR_NO_UPGRADE_PATH => Self::NoUpgradePath(NoUpgradePath),
      _ => Self::Other,
    }
  }
}

#[derive(Error, Debug)]
#[error("{kind}\n{errors:#?}")]
pub struct ApiResponseError {
  pub errors: Vec<String>,
  #[source]
  pub kind: ApiResponseErrorKind,
}

impl From<Vec<String>> for ApiResponseError {
  fn from(value: Vec<String>) -> Self {
    Self {
      kind: value.as_slice().into(),
      errors: value,
    }
  }
}

/// Common errors to every API call
#[derive(Error, Debug)]
pub enum ApiResponseCommonErrors {
  #[error(transparent)]
  InvalidApiKey(#[from] InvalidApiKey),

  #[error("An unknown error occurred:\n{0:#?}")]
  Other(Vec<String>),
}

impl From<ApiResponseError> for ApiResponseCommonErrors {
  fn from(value: ApiResponseError) -> Self {
    match value.kind {
      ApiResponseErrorKind::InvalidApiKey(v) => v.into(),
      _ => Self::Other(value.errors),
    }
  }
}

/// Errors returned from the login API call
#[derive(Error, Debug)]
pub enum LoginResponseError {
  #[error(transparent)]
  IncorrectUsernameOrPassword(#[from] IncorrectUsernameOrPassword),

  #[error(transparent)]
  IncorrectCaptchaCode(#[from] IncorrectCaptchaCode),

  #[error(transparent)]
  Other(#[from] ApiResponseCommonErrors),
}

impl From<ApiResponseError> for LoginResponseError {
  fn from(value: ApiResponseError) -> Self {
    match value.kind {
      ApiResponseErrorKind::IncorrectUsernameOrPassword(v) => v.into(),
      ApiResponseErrorKind::IncorrectCaptchaCode(v) => v.into(),
      _ => Self::Other(value.into()),
    }
  }
}

/// Errors returned from the TOTP verification API call
#[derive(Error, Debug)]
pub enum TOTPResponseError {
  #[error(transparent)]
  IncorrectTOTPCode(#[from] IncorrectTOTPCode),

  #[error(transparent)]
  TOTPTokenTimedOut(#[from] TOTPTokenTimedOut),

  #[error(transparent)]
  InvalidTOTPToken(#[from] InvalidTOTPToken),

  #[error(transparent)]
  Other(#[from] ApiResponseCommonErrors),
}

impl From<ApiResponseError> for TOTPResponseError {
  fn from(value: ApiResponseError) -> Self {
    match value.kind {
      ApiResponseErrorKind::IncorrectTOTPCode(v) => v.into(),
      ApiResponseErrorKind::TOTPTokenTimedOut(v) => v.into(),
      ApiResponseErrorKind::InvalidTOTPToken(v) => v.into(),
      _ => Self::Other(value.into()),
    }
  }
}

/// Errors returned from all the API calls that require a user ID as a parameter
#[derive(Error, Debug)]
pub enum UserResponseError {
  #[error(transparent)]
  InvalidUserID(#[from] InvalidUserID),

  #[error(transparent)]
  Other(#[from] ApiResponseCommonErrors),
}

impl From<ApiResponseError> for UserResponseError {
  fn from(value: ApiResponseError) -> Self {
    match value.kind {
      ApiResponseErrorKind::InvalidUserID(v) => v.into(),
      _ => Self::Other(value.into()),
    }
  }
}

/// Errors returned from all the API calls that require a collection ID as a parameter
#[derive(Error, Debug)]
pub enum CollectionResponseError {
  #[error(transparent)]
  InvalidCollectionID(#[from] InvalidCollectionID),

  #[error(transparent)]
  Other(#[from] ApiResponseCommonErrors),
}

impl From<ApiResponseError> for CollectionResponseError {
  fn from(value: ApiResponseError) -> Self {
    match value.kind {
      ApiResponseErrorKind::InvalidCollectionID(v) => v.into(),
      _ => Self::Other(value.into()),
    }
  }
}

/// Errors returned from all the API calls that require a game ID as a parameter
#[derive(Error, Debug)]
pub enum GameResponseError {
  #[error(transparent)]
  InvalidGameID(#[from] InvalidGameID),

  #[error(transparent)]
  Other(#[from] ApiResponseCommonErrors),
}

impl From<ApiResponseError> for GameResponseError {
  fn from(value: ApiResponseError) -> Self {
    match value.kind {
      ApiResponseErrorKind::InvalidGameID(v) => v.into(),
      _ => Self::Other(value.into()),
    }
  }
}

/// Errors returned from all the API calls that require an upload ID as a parameter
#[derive(Error, Debug)]
pub enum UploadResponseError {
  #[error(transparent)]
  InvalidUploadID(#[from] InvalidUploadID),

  #[error(transparent)]
  Other(#[from] ApiResponseCommonErrors),
}

impl From<ApiResponseError> for UploadResponseError {
  fn from(value: ApiResponseError) -> Self {
    match value.kind {
      ApiResponseErrorKind::InvalidUploadID(v) => v.into(),
      ApiResponseErrorKind::InvalidGameID(_) => Self::InvalidUploadID(InvalidUploadID),
      _ => Self::Other(value.into()),
    }
  }
}

/// Errors returned from all the API calls that require a build ID as a parameter
#[derive(Error, Debug)]
pub enum BuildResponseError {
  #[error(transparent)]
  InvalidBuildID(#[from] InvalidBuildID),

  #[error(transparent)]
  Other(#[from] ApiResponseCommonErrors),
}

impl From<ApiResponseError> for BuildResponseError {
  fn from(value: ApiResponseError) -> Self {
    match value.kind {
      ApiResponseErrorKind::InvalidBuildID(v) => v.into(),
      ApiResponseErrorKind::InvalidUploadID(_) | ApiResponseErrorKind::InvalidGameID(_) => {
        Self::InvalidBuildID(InvalidBuildID)
      }
      _ => Self::Other(value.into()),
    }
  }
}

/// Errors returned from the upgrade path API call
#[derive(Error, Debug)]
pub enum UpgradePathResponseError {
  #[error(transparent)]
  NoUpgradePath(#[from] NoUpgradePath),

  #[error(transparent)]
  InvalidBuildID(#[from] InvalidBuildID),

  #[error(transparent)]
  InvalidTargetBuildID(#[from] InvalidTargetBuildID),

  #[error(transparent)]
  Other(#[from] ApiResponseCommonErrors),
}

impl From<ApiResponseError> for UpgradePathResponseError {
  fn from(value: ApiResponseError) -> Self {
    match value.kind {
      ApiResponseErrorKind::NoUpgradePath(v) => v.into(),
      ApiResponseErrorKind::InvalidBuildID(v) => v.into(),
      ApiResponseErrorKind::InvalidUploadID(_) | ApiResponseErrorKind::InvalidGameID(_) => {
        Self::InvalidBuildID(InvalidBuildID)
      }
      ApiResponseErrorKind::InvalidTargetBuildID(v) => v.into(),
      _ => Self::Other(value.into()),
    }
  }
}