Skip to content

Commit 7454635

Browse files
Docs add docs for ModProvider and Checks
1 parent b268b15 commit 7454635

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

src/add/mod.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,43 @@ pub enum Error {
3333
pub type Result<T> = std::result::Result<T, Error>;
3434

3535
bitflags! {
36+
/// Represents a set of boolean flags used for checking conditions while adding Mods using
37+
/// [ModProvider]
38+
///
39+
/// The `Checks` struct is a bitflag representation of different checks that can be performed.
40+
///
41+
/// # Examples
42+
///
43+
/// ```
44+
/// use libium::Checks;
45+
///
46+
/// // Create a set of checks
47+
/// let checks = Checks::ENABLED | Checks::GAME_VERSION;
48+
/// // or
49+
/// let mut checks = Checks::empty();
50+
/// checks.insert(Checks::ENABLED);
51+
///
52+
///
53+
/// // Check if a specific flag is set
54+
/// if checks.contains(Checks::ENABLED) {
55+
/// println!("The feature is enabled.");
56+
/// }
57+
///
58+
/// // Add additional checks
59+
/// let updated_checks = checks | Checks::MOD_LOADER;
60+
/// ```
3661
pub struct Checks: u8 {
62+
/// Should we perform checks?
3763
const ENABLED = 0b00000001;
64+
/// Should we check game version?
3865
const GAME_VERSION = 0b00000010;
66+
/// Should we check mod loader?
3967
const MOD_LOADER = 0b00000100;
4068
}
4169
}
4270

71+
/// Collects all mod providers (i.e Modrinth, Cursefore and github wrappers) and abstracts away the
72+
/// add method
4373
pub struct ModProvider<'p> {
4474
modrinth: &'p ferinth::Ferinth,
4575
curseforge: &'p furse::Furse,
@@ -49,6 +79,40 @@ pub struct ModProvider<'p> {
4979
}
5080

5181
impl<'p> ModProvider<'p> {
82+
/// Creates a new instance of `ModManager` with the provided API wrappers, checks, and profile.
83+
///
84+
/// This function constructs a new `ModManager` instance, which serves as a utility for adding mods.
85+
/// It takes API wrappers for Modrinth, CurseForge, and GitHub, along with references to checks
86+
/// and a mutable reference to a profile. These components are used internally for
87+
/// for adding mods, performing checks, and managing profiles.
88+
///
89+
/// # Arguments
90+
///
91+
/// * `modrinth` - A reference to the Modrinth API wrapper (`ferinth::Ferinth`).
92+
/// * `curseforge` - A reference to the CurseForge API wrapper (`furse::Furse`).
93+
/// * `github` - A reference to the GitHub API wrapper (`octocrab::Octocrab`).
94+
/// * `checks` - Checks to perform while adding mods
95+
/// * `profile` - The profile to make changes in
96+
///
97+
/// # Returns
98+
///
99+
/// A new instance of `ModProvider` configured with the provided components.
100+
///
101+
/// # Example
102+
///
103+
/// ```
104+
/// // Create API wrappers
105+
/// let modrinth = Ferinth::new();
106+
/// let curseforge = Furse::new();
107+
/// let github = Octocrab::builder().build();
108+
///
109+
/// // Create checks and profile
110+
/// let checks = Checks::empty();
111+
/// let mut profile = Profile::new();
112+
///
113+
/// // Create a new ModProvider instance
114+
/// let mod_provider = ModProvider::new(&modrinth, &curseforge, &github, &checks, &mut profile);
115+
/// ```
52116
pub fn new(
53117
modrinth: &'p ferinth::Ferinth,
54118
curseforge: &'p furse::Furse,
@@ -65,6 +129,28 @@ impl<'p> ModProvider<'p> {
65129
}
66130
}
67131

132+
/// Add a mod to the profile based on the identifier.
133+
/// The identifier can be:
134+
/// - A numeric ID representing a project on CurseForge.
135+
/// - A GitHub repository identifier in the form "username/repository".
136+
/// - Any other string, which is assumed to be a mod ID on Modrinth.
137+
///
138+
/// # Arguments
139+
///
140+
/// * `identifier` - A string representing the identifier of the mod.
141+
///
142+
/// # Returns
143+
///
144+
/// A Result containing a String representing the added mod's information,
145+
/// or an error if the addition failed.
146+
///
147+
/// # Examples
148+
///
149+
/// ```
150+
/// let mod_provider = ModProvider::new(&modrinth, &curseforge, &github, &checks, &mut profile);
151+
/// let result = manager.add("123456");
152+
/// assert!(result.is_ok());
153+
/// ```
68154
pub async fn add(&mut self, identifier: &str) -> Result<String> {
69155
if let Ok(project_id) = identifier.parse() {
70156
self.curseforge(project_id).await
@@ -75,14 +161,19 @@ impl<'p> ModProvider<'p> {
75161
}
76162
}
77163

164+
/// Fetches mod information from CurseForge using the provided project ID.
78165
pub async fn curseforge(&mut self, project_id: i32) -> Result<String> {
79166
curseforge::curseforge(self.curseforge, project_id, self.profile, self.checks).await
80167
}
168+
169+
/// Fetches mod information from GitHub using the provided repository identifier.
81170
pub async fn github(&mut self, identifier: &str) -> Result<String> {
82171
let split = identifier.split('/').collect::<Vec<_>>();
83172
let repo_handler = self.github.repos(split[0], split[1]);
84173
github::github(&repo_handler, self.profile, self.checks).await
85174
}
175+
///
176+
/// Fetches mod information from Modrinth using the provided identifier.
86177
pub async fn modrinth(&mut self, identifier: &str) -> Result<String> {
87178
modrinth::modrinth(self.modrinth, identifier, self.profile, self.checks)
88179
.await
@@ -129,6 +220,32 @@ impl From<octocrab::Error> for Error {
129220
}
130221
}
131222

223+
/// Adds multiple mods to the profile using the provided `ModProvider` and a list of identifiers.
224+
///
225+
/// # Arguments
226+
///
227+
/// * `mod_provider` - A mutable reference to a `ModProvider` instance used for adding mods.
228+
/// * `identifiers` - A vector of strings representing mod identifiers to be added.
229+
///
230+
/// # Returns
231+
///
232+
/// A tuple containing two vectors:
233+
/// - The names of the mods successfully added to the profile.
234+
/// - Tuples of identifiers of mods that failed to be added along with the corresponding errors.
235+
///
236+
/// # Examples
237+
///
238+
/// ```
239+
/// async fn example(mod_provider: &mut ModProvider<'_>, identifiers: Vec<String>) {
240+
/// let (success_names, failures) = ModProvider::add_multiple(mod_provider, identifiers).await;
241+
///
242+
/// println!("Successfully added mods: {:?}", success_names);
243+
/// println!("Failed to add mods:");
244+
/// for (identifier, error) in failures {
245+
/// println!("Identifier: {}, Error: {:?}", identifier, error);
246+
/// }
247+
/// }
248+
/// ```
132249
pub async fn add_multiple<'p>(
133250
mod_provider: &mut ModProvider<'p>,
134251
identifiers: Vec<String>,

0 commit comments

Comments
 (0)