mcapi 0.3.0

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
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use url::Url;

use super::common::{MINECRAFT_GAME_ID, SortOrder, define_u8_enum};
use super::files::{File, FileReleaseType};
use crate::util::{deserialize_optional_url, serialize_option_json};

/// A CurseForge mod/project returned by `/v1/mods/{modId}` and search endpoints.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Mod {
    pub id: u32,
    pub game_id: u32,
    pub name: String,
    pub slug: String,
    pub links: ModLinks,
    pub summary: String,
    pub status: ModStatus,
    pub download_count: u64,
    pub is_featured: bool,
    pub primary_category_id: u32,
    #[serde(default)]
    pub categories: Vec<super::Category>,
    #[serde(default)]
    pub class_id: Option<u32>,
    #[serde(default)]
    pub authors: Vec<ModAuthor>,
    #[serde(default)]
    pub logo: Option<ModAsset>,
    #[serde(default)]
    pub screenshots: Vec<ModAsset>,
    pub main_file_id: u32,
    #[serde(default)]
    pub latest_files: Vec<File>,
    #[serde(default)]
    pub latest_files_indexes: Vec<FileIndex>,
    #[serde(default)]
    pub latest_early_access_files_indexes: Vec<FileIndex>,
    pub date_created: Timestamp,
    pub date_modified: Timestamp,
    pub date_released: Timestamp,
    #[serde(default)]
    pub allow_mod_distribution: Option<bool>,
    #[serde(default)]
    pub game_popularity_rank: Option<u32>,
    #[serde(default)]
    pub is_available: Option<bool>,
    #[serde(default)]
    pub thumbs_up_count: Option<u64>,
    #[serde(default)]
    pub rating: Option<f64>,
    #[serde(default)]
    pub featured_project_tag: Option<i32>,
    #[serde(default)]
    pub server_affiliation: Option<serde_json::Value>,
    #[serde(default)]
    pub social_links: Vec<serde_json::Value>,
}

/// Compact file index attached to [`Mod::latest_files_indexes`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FileIndex {
    pub game_version: String,
    pub file_id: u32,
    pub filename: String,
    pub release_type: FileReleaseType,
    #[serde(default)]
    pub game_version_type_id: Option<u32>,
    #[serde(default)]
    pub mod_loader: Option<ModLoaderType>,
}

/// External project links on a [`Mod`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ModLinks {
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub website_url: Option<Url>,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub wiki_url: Option<Url>,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub issues_url: Option<Url>,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub source_url: Option<Url>,
}

define_u8_enum! {
    /// Moderation/publication status of a [`Mod`].
    pub enum ModStatus {
        New = 1,
        ChangesRequired = 2,
        UnderSoftReview = 3,
        Approved = 4,
        Rejected = 5,
        ChangesMade = 6,
        Inactive = 7,
        Abandoned = 8,
        Deleted = 9,
        UnderReview = 10,
    }
}

/// Author information on a [`Mod`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ModAuthor {
    pub id: u32,
    pub name: String,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub url: Option<Url>,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub avatar_url: Option<Url>,
}

/// Logo or screenshot asset on a [`Mod`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ModAsset {
    pub id: u32,
    pub mod_id: u32,
    pub title: String,
    pub description: String,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub thumbnail_url: Option<Url>,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub url: Option<Url>,
}

define_u8_enum! {
    /// Sort fields supported by `GET /v1/mods/search`.
    pub enum ModsSearchSortField {
        Featured = 1,
        Popularity = 2,
        LastUpdated = 3,
        Name = 4,
        Author = 5,
        TotalDownloads = 6,
        Category = 7,
        GameVersion = 8,
        EarlyAccess = 9,
        FeaturedReleased = 10,
        ReleasedDate = 11,
        Rating = 12,
    }
}

define_u8_enum! {
    /// Mod loader filter enum defined by CurseForge.
    pub enum ModLoaderType {
        Any = 0,
        Forge = 1,
        Cauldron = 2,
        LiteLoader = 3,
        Fabric = 4,
        Quilt = 5,
        NeoForge = 6,
    }
}

/// Fluent query builder for `GET /v1/mods/search`.
#[non_exhaustive]
#[derive(Clone, Debug, Default, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ModSearchQuery {
    pub game_id: u32,
    pub class_id: Option<u32>,
    pub category_id: Option<u32>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_option_json"
    )]
    pub category_ids: Option<Vec<u32>>,
    pub game_version: Option<String>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_option_json"
    )]
    pub game_versions: Option<Vec<String>>,
    pub search_filter: Option<String>,
    pub sort_field: Option<ModsSearchSortField>,
    pub sort_order: Option<SortOrder>,
    pub mod_loader_type: Option<ModLoaderType>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_option_json"
    )]
    pub mod_loader_types: Option<Vec<ModLoaderType>>,
    pub game_version_type_id: Option<u32>,
    pub author_id: Option<u32>,
    pub primary_author_id: Option<u32>,
    pub slug: Option<String>,
    pub index: Option<u32>,
    pub page_size: Option<u32>,
}

impl ModSearchQuery {
    /// Creates a mod search query scoped to `game_id`.
    #[must_use]
    pub fn new(game_id: u32) -> Self {
        Self {
            game_id,
            ..Default::default()
        }
    }

    /// Creates a new search query scoped to Minecraft (`432`).
    #[must_use]
    pub fn minecraft() -> Self {
        Self::new(MINECRAFT_GAME_ID)
    }

    /// Filters by class ID (e.g., `6` for Mods, `4471` for Modpacks).
    #[must_use]
    pub fn class_id(mut self, class_id: u32) -> Self {
        self.class_id = Some(class_id);
        self
    }

    /// Filters by a single category ID.
    #[must_use]
    pub fn category_id(mut self, category_id: u32) -> Self {
        self.category_id = Some(category_id);
        self
    }

    /// Filters by multiple category IDs (`categoryIds`).
    #[must_use]
    pub fn category_ids(mut self, category_ids: impl IntoIterator<Item = u32>) -> Self {
        self.category_ids = Some(category_ids.into_iter().collect());
        self
    }

    /// Filters by a game version string (e.g., `"1.20.1"`).
    #[must_use]
    pub fn game_version(mut self, game_version: impl Into<String>) -> Self {
        self.game_version = Some(game_version.into());
        self
    }

    /// Filters by multiple game version strings (`gameVersions`).
    #[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 search keywords (`searchFilter`).
    #[must_use]
    pub fn search_filter(mut self, filter: impl Into<String>) -> Self {
        self.search_filter = Some(filter.into());
        self
    }

    /// Sets the sort field (`sortField`).
    #[must_use]
    pub fn sort_field(mut self, sort_field: ModsSearchSortField) -> Self {
        self.sort_field = Some(sort_field);
        self
    }

    /// Sets the sort direction (`sortOrder`).
    #[must_use]
    pub fn sort_order(mut self, sort_order: SortOrder) -> Self {
        self.sort_order = Some(sort_order);
        self
    }

    /// Filters by a single mod loader type (`modLoaderType`).
    #[must_use]
    pub fn mod_loader_type(mut self, loader: ModLoaderType) -> Self {
        self.mod_loader_type = Some(loader);
        self
    }

    /// Filters by multiple mod loader types (`modLoaderTypes`).
    #[must_use]
    pub fn mod_loader_types(mut self, loaders: impl IntoIterator<Item = ModLoaderType>) -> Self {
        self.mod_loader_types = Some(loaders.into_iter().collect());
        self
    }

    /// Filters by CurseForge game version type ID.
    #[must_use]
    pub fn game_version_type_id(mut self, game_version_type_id: u32) -> Self {
        self.game_version_type_id = Some(game_version_type_id);
        self
    }

    /// Filters mods by an author ID (`authorId`).
    #[must_use]
    pub fn author_id(mut self, author_id: u32) -> Self {
        self.author_id = Some(author_id);
        self
    }

    /// Filters mods by primary author ID (`primaryAuthorId`).
    #[must_use]
    pub fn primary_author_id(mut self, primary_author_id: u32) -> Self {
        self.primary_author_id = Some(primary_author_id);
        self
    }

    /// Filters by exact mod slug.
    #[must_use]
    pub fn slug(mut self, slug: impl Into<String>) -> Self {
        self.slug = Some(slug.into());
        self
    }

    /// Sets the zero-based pagination offset.
    #[must_use]
    pub fn index(mut self, index: u32) -> Self {
        self.index = Some(index);
        self
    }

    /// Sets the page size (up to 50 per CurseForge API rules).
    #[must_use]
    pub fn page_size(mut self, page_size: u32) -> Self {
        self.page_size = Some(page_size);
        self
    }
}

/// Fluent query builder for `GET /v1/mods/{modId}/description`.
#[non_exhaustive]
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
pub struct ModDescriptionQuery {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub raw: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stripped: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub markup: Option<bool>,
}

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

    /// Requests the raw unrendered HTML/markdown description when `true`.
    #[must_use]
    pub fn raw(mut self, raw: bool) -> Self {
        self.raw = Some(raw);
        self
    }

    /// Requests HTML tags to be stripped when `true`.
    #[must_use]
    pub fn stripped(mut self, stripped: bool) -> Self {
        self.stripped = Some(stripped);
        self
    }

    /// Requests markup formatting when `true`.
    #[must_use]
    pub fn markup(mut self, markup: bool) -> Self {
        self.markup = Some(markup);
        self
    }
}

/// Request body for `POST /v1/mods/featured`.
#[non_exhaustive]
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FeaturedModsRequest {
    pub game_id: u32,
    #[serde(default)]
    pub excluded_mod_ids: Vec<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub game_version_type_id: Option<u32>,
}

impl FeaturedModsRequest {
    /// Creates a featured mods request scoped to `game_id`.
    #[must_use]
    pub fn new(game_id: u32) -> Self {
        Self {
            game_id,
            excluded_mod_ids: Vec::new(),
            game_version_type_id: None,
        }
    }

    /// Creates a featured mods request scoped to Minecraft (`432`).
    #[must_use]
    pub fn minecraft() -> Self {
        Self::new(MINECRAFT_GAME_ID)
    }

    /// Sets mod IDs to exclude from the featured/popular results (`excludedModIds`).
    #[must_use]
    pub fn excluded_mod_ids(mut self, mod_ids: impl IntoIterator<Item = u32>) -> Self {
        self.excluded_mod_ids = mod_ids.into_iter().collect();
        self
    }

    /// Filters featured mods by CurseForge game version type ID.
    #[must_use]
    pub fn game_version_type_id(mut self, game_version_type_id: u32) -> Self {
        self.game_version_type_id = Some(game_version_type_id);
        self
    }
}

/// Response payload inside `POST /v1/mods/featured`.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FeaturedModsResponse {
    #[serde(default)]
    pub featured: Vec<Mod>,
    #[serde(default)]
    pub popular: Vec<Mod>,
    #[serde(default)]
    pub recently_updated: Vec<Mod>,
}