mcapi 0.3.1

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, BMCLAPI, and Java Runtimes)
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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use url::Url;

use crate::util::serialize_option_json;

/// Release channel of a Modrinth version.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum VersionType {
    Release,
    Beta,
    Alpha,
    #[serde(other)]
    Unknown,
}

/// Moderation status of a Modrinth version.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum VersionStatus {
    Listed,
    Archived,
    Draft,
    Unlisted,
    Scheduled,
    #[serde(other)]
    Unknown,
}

/// File hashes embedded in [`VersionFile`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct FileHashes {
    pub sha512: String,
    pub sha1: String,
}

/// Special file role inside a version.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub enum FileType {
    RequiredResourcePack,
    OptionalResourcePack,
    SourcesJar,
    DevJar,
    JavadocJar,
    Signature,
    #[serde(other)]
    Unknown,
}

/// Downloadable file belonging to a [`Version`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct VersionFile {
    pub hashes: FileHashes,
    pub url: Url,
    pub filename: String,
    pub primary: bool,
    pub size: u64,
    #[serde(default)]
    pub file_type: Option<FileType>,
}

/// Dependency relationship declared by a [`Version`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct Dependency {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub file_name: Option<String>,
    pub dependency_type: DependencyType,
}

/// Dependency kind in [`Dependency`].
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum DependencyType {
    Required,
    Optional,
    Incompatible,
    Embedded,
    #[serde(other)]
    Unknown,
}

/// Complete Modrinth project version object.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct Version {
    pub id: String,
    pub project_id: String,
    pub author_id: String,
    pub featured: bool,
    pub name: String,
    pub version_number: String,
    #[serde(default)]
    pub changelog: Option<String>,
    #[serde(default)]
    pub changelog_url: Option<Url>,
    pub date_published: Timestamp,
    pub downloads: u64,
    pub version_type: VersionType,
    pub status: VersionStatus,
    #[serde(default)]
    pub requested_status: Option<VersionStatus>,
    #[serde(default)]
    pub files: Vec<VersionFile>,
    #[serde(default)]
    pub dependencies: Vec<Dependency>,
    #[serde(default)]
    pub game_versions: Vec<String>,
    #[serde(default)]
    pub loaders: Vec<String>,
}

impl Version {
    /// Returns the primary downloadable file, falling back to the first file if none is
    /// explicitly marked `primary`.
    #[must_use]
    pub fn primary_file(&self) -> Option<&VersionFile> {
        self.files
            .iter()
            .find(|f| f.primary)
            .or_else(|| self.files.first())
    }
}

/// Hash algorithm accepted by Modrinth `/v2/version_file` endpoints.
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum HashAlgorithm {
    Sha1,
    Sha512,
}

impl HashAlgorithm {
    /// Returns the query/URL string (`"sha1"` or `"sha512"`).
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            HashAlgorithm::Sha1 => "sha1",
            HashAlgorithm::Sha512 => "sha512",
        }
    }
}

/// Fluent query builder for `GET /v2/project/{id|slug}/version`.
#[non_exhaustive]
#[derive(Clone, Debug, Default, Serialize, Eq, PartialEq)]
pub struct VersionsQuery {
    #[serde(serialize_with = "serialize_option_json")]
    pub loaders: Option<Vec<String>>,
    #[serde(serialize_with = "serialize_option_json")]
    pub game_versions: Option<Vec<String>>,
    pub featured: Option<bool>,
}

impl VersionsQuery {
    /// Creates an empty [`VersionsQuery`].
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Filters by loader IDs (e.g. `["fabric"]`).
    #[must_use]
    pub fn loaders(mut self, loaders: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.loaders = Some(loaders.into_iter().map(Into::into).collect());
        self
    }

    /// Filters by Minecraft game versions (e.g. `["1.20.1"]`).
    #[must_use]
    pub fn game_versions(
        mut self,
        game_versions: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.game_versions = Some(game_versions.into_iter().map(Into::into).collect());
        self
    }

    /// Filters by whether versions are marked as featured.
    #[must_use]
    pub fn featured(mut self, featured: bool) -> Self {
        self.featured = Some(featured);
        self
    }
}

/// Request body for `POST /v2/version_file/{hash}/update`.
#[non_exhaustive]
#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
pub struct LatestVersionRequest {
    pub loaders: Vec<String>,
    pub game_versions: Vec<String>,
}

impl LatestVersionRequest {
    /// Creates an update check request matching `loaders` and `game_versions`.
    #[must_use]
    pub fn new(
        loaders: impl IntoIterator<Item = impl Into<String>>,
        game_versions: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        Self {
            loaders: loaders.into_iter().map(Into::into).collect(),
            game_versions: game_versions.into_iter().map(Into::into).collect(),
        }
    }
}

/// Request body for `POST /v2/version_files/update`.
#[non_exhaustive]
#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
pub struct LatestVersionsRequest {
    pub hashes: Vec<String>,
    pub algorithm: HashAlgorithm,
    pub loaders: Vec<String>,
    pub game_versions: Vec<String>,
}

impl LatestVersionsRequest {
    /// Creates a batch update check request for `hashes` using `algorithm`, `loaders`, and `game_versions`.
    #[must_use]
    pub fn new(
        hashes: impl IntoIterator<Item = impl Into<String>>,
        algorithm: HashAlgorithm,
        loaders: impl IntoIterator<Item = impl Into<String>>,
        game_versions: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        Self {
            hashes: hashes.into_iter().map(Into::into).collect(),
            algorithm,
            loaders: loaders.into_iter().map(Into::into).collect(),
            game_versions: game_versions.into_iter().map(Into::into).collect(),
        }
    }
}

/// JSON metadata part (`data`) for `POST /v2/version`.
#[cfg(feature = "modrinth-write")]
#[cfg_attr(docsrs, doc(cfg(feature = "modrinth-write")))]
#[non_exhaustive]
#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
pub struct CreateVersionRequest {
    pub name: String,
    pub version_number: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub changelog: Option<String>,
    pub dependencies: Vec<Dependency>,
    pub game_versions: Vec<String>,
    pub version_type: VersionType,
    pub loaders: Vec<String>,
    pub featured: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<VersionStatus>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub requested_status: Option<VersionStatus>,
    pub project_id: String,
    pub file_parts: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary_file: Option<String>,
}

#[cfg(feature = "modrinth-write")]
#[cfg_attr(docsrs, doc(cfg(feature = "modrinth-write")))]
impl CreateVersionRequest {
    /// Creates a version creation payload for `project_id` with `name` and `version_number`.
    ///
    /// Defaults `version_type` to [`VersionType::Release`] and `featured` to `false`.
    #[must_use]
    pub fn new(
        project_id: impl Into<String>,
        name: impl Into<String>,
        version_number: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            version_number: version_number.into(),
            changelog: None,
            dependencies: Vec::new(),
            game_versions: Vec::new(),
            version_type: VersionType::Release,
            loaders: Vec::new(),
            featured: false,
            status: None,
            requested_status: None,
            project_id: project_id.into(),
            file_parts: Vec::new(),
            primary_file: None,
        }
    }

    /// Sets the markdown changelog for this version.
    #[must_use]
    pub fn changelog(mut self, changelog: impl Into<String>) -> Self {
        self.changelog = Some(changelog.into());
        self
    }

    /// Sets the version's declared project/version dependencies.
    #[must_use]
    pub fn dependencies(mut self, dependencies: Vec<Dependency>) -> Self {
        self.dependencies = dependencies;
        self
    }

    /// Sets compatible Minecraft game version strings.
    #[must_use]
    pub fn game_versions(
        mut self,
        game_versions: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.game_versions = game_versions.into_iter().map(Into::into).collect();
        self
    }

    /// Sets the release channel (`release`, `beta`, or `alpha`).
    #[must_use]
    pub fn version_type(mut self, version_type: VersionType) -> Self {
        self.version_type = version_type;
        self
    }

    /// Sets supported mod loader slugs (e.g. `["fabric"]`).
    #[must_use]
    pub fn loaders(mut self, loaders: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.loaders = loaders.into_iter().map(Into::into).collect();
        self
    }

    /// Sets whether this version should be marked as featured.
    #[must_use]
    pub fn featured(mut self, featured: bool) -> Self {
        self.featured = featured;
        self
    }

    /// Sets the initial publication status.
    #[must_use]
    pub fn status(mut self, status: VersionStatus) -> Self {
        self.status = Some(status);
        self
    }

    /// Sets the requested publication status for review/scheduling.
    #[must_use]
    pub fn requested_status(mut self, requested_status: VersionStatus) -> Self {
        self.requested_status = Some(requested_status);
        self
    }

    /// Lists the multipart form part names corresponding to uploaded files.
    #[must_use]
    pub fn file_parts(mut self, file_parts: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.file_parts = file_parts.into_iter().map(Into::into).collect();
        self
    }

    /// Identifies the multipart form part name of the primary downloadable file.
    #[must_use]
    pub fn primary_file(mut self, primary_file: impl Into<String>) -> Self {
        self.primary_file = Some(primary_file.into());
        self
    }
}

/// Patch payload for `PATCH /v2/version/{id}`.
#[cfg(feature = "modrinth-write")]
#[cfg_attr(docsrs, doc(cfg(feature = "modrinth-write")))]
#[non_exhaustive]
#[derive(Clone, Debug, Default, Serialize, Eq, PartialEq)]
pub struct ModifyVersionRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version_number: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub changelog: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dependencies: Option<Vec<Dependency>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub game_versions: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version_type: Option<VersionType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub loaders: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub featured: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<VersionStatus>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub requested_status: Option<VersionStatus>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary_file: Option<Vec<String>>,
}

#[cfg(feature = "modrinth-write")]
#[cfg_attr(docsrs, doc(cfg(feature = "modrinth-write")))]
impl ModifyVersionRequest {
    /// Creates an empty [`ModifyVersionRequest`] (no fields modified).
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Updates the version display name.
    #[must_use]
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Updates the semantic/display version number string.
    #[must_use]
    pub fn version_number(mut self, version_number: impl Into<String>) -> Self {
        self.version_number = Some(version_number.into());
        self
    }

    /// Updates the markdown changelog.
    #[must_use]
    pub fn changelog(mut self, changelog: impl Into<String>) -> Self {
        self.changelog = Some(changelog.into());
        self
    }

    /// Replaces the declared dependencies.
    #[must_use]
    pub fn dependencies(mut self, dependencies: Vec<Dependency>) -> Self {
        self.dependencies = Some(dependencies);
        self
    }

    /// Replaces the compatible Minecraft game version strings.
    #[must_use]
    pub fn game_versions(
        mut self,
        game_versions: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.game_versions = Some(game_versions.into_iter().map(Into::into).collect());
        self
    }

    /// Updates the release channel (`release`, `beta`, or `alpha`).
    #[must_use]
    pub fn version_type(mut self, version_type: VersionType) -> Self {
        self.version_type = Some(version_type);
        self
    }

    /// Replaces the supported mod loader slugs.
    #[must_use]
    pub fn loaders(mut self, loaders: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.loaders = Some(loaders.into_iter().map(Into::into).collect());
        self
    }

    /// Updates whether this version is marked as featured.
    #[must_use]
    pub fn featured(mut self, featured: bool) -> Self {
        self.featured = Some(featured);
        self
    }

    /// Updates the version publication status.
    #[must_use]
    pub fn status(mut self, status: VersionStatus) -> Self {
        self.status = Some(status);
        self
    }

    /// Updates the requested publication status.
    #[must_use]
    pub fn requested_status(mut self, requested_status: VersionStatus) -> Self {
        self.requested_status = Some(requested_status);
        self
    }

    /// Sets the primary file as a `[hash_algorithm, hash]` pair per Modrinth v2 `PATCH /v2/version/{id}`.
    #[must_use]
    pub fn primary_file(
        mut self,
        primary_file: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.primary_file = Some(primary_file.into_iter().map(Into::into).collect());
        self
    }
}

/// Request payload for `POST /v2/version/{id}/schedule`.
#[cfg(feature = "modrinth-write")]
#[cfg_attr(docsrs, doc(cfg(feature = "modrinth-write")))]
#[non_exhaustive]
#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
pub struct ScheduleVersionRequest {
    pub time: Timestamp,
    pub requested_status: VersionStatus,
}

#[cfg(feature = "modrinth-write")]
#[cfg_attr(docsrs, doc(cfg(feature = "modrinth-write")))]
impl ScheduleVersionRequest {
    /// Creates a scheduled status transition request for a version at `time`.
    #[must_use]
    pub fn new(time: Timestamp, requested_status: VersionStatus) -> Self {
        Self {
            time,
            requested_status,
        }
    }
}