Skip to content

Commit f58eac5

Browse files
Refactor(WIP)! Condense different mod providers in to one API
1 parent 097c583 commit f58eac5

File tree

1 file changed

+74
-40
lines changed

1 file changed

+74
-40
lines changed

src/add/mod.rs

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,56 @@ impl Checks {
115115
}
116116
}
117117

118+
pub struct ModProvider<'p> {
119+
modrinth: &'p ferinth::Ferinth,
120+
curseforge: &'p furse::Furse,
121+
github: &'p octocrab::Octocrab,
122+
checks: &'p Checks,
123+
profile: &'p mut Profile,
124+
}
125+
126+
impl<'p> ModProvider<'p> {
127+
pub fn new(
128+
modrinth: &'p ferinth::Ferinth,
129+
curseforge: &'p furse::Furse,
130+
github: &'p octocrab::Octocrab,
131+
checks: &'p Checks,
132+
profile: &'p mut Profile,
133+
) -> Self {
134+
Self {
135+
modrinth,
136+
curseforge,
137+
github,
138+
checks,
139+
profile,
140+
}
141+
}
142+
143+
pub async fn add(&mut self, identifier: &str) -> Result<String> {
144+
if let Ok(project_id) = identifier.parse() {
145+
self.curseforge(project_id).await
146+
} else if identifier.matches('/').count() == 1 {
147+
self.github(identifier).await
148+
} else {
149+
self.modrinth(identifier).await
150+
}
151+
}
152+
153+
pub async fn curseforge(&mut self, project_id: i32) -> Result<String> {
154+
curseforge::curseforge(self.curseforge, project_id, self.profile, self.checks).await
155+
}
156+
pub async fn github(&mut self, identifier: &str) -> Result<String> {
157+
let split = identifier.split('/').collect::<Vec<_>>();
158+
let repo_handler = self.github.repos(split[0], split[1]);
159+
github::github(&repo_handler, self.profile, self.checks).await
160+
}
161+
pub async fn modrinth(&mut self, identifier: &str) -> Result<String> {
162+
modrinth::modrinth(self.modrinth, identifier, self.profile, self.checks)
163+
.await
164+
.map(|o| o.0)
165+
}
166+
}
167+
118168
impl From<furse::Error> for Error {
119169
fn from(err: furse::Error) -> Self {
120170
if let furse::Error::ReqwestError(source) = &err {
@@ -154,37 +204,28 @@ impl From<octocrab::Error> for Error {
154204
}
155205
}
156206

157-
pub async fn add_multiple(
158-
modrinth: &ferinth::Ferinth,
159-
curseforge: &furse::Furse,
160-
github: &octocrab::Octocrab,
161-
profile: &mut Profile,
207+
pub async fn add_multiple<'p>(
208+
mod_provider: &mut ModProvider<'p>,
162209
identifiers: Vec<String>,
163210
) -> (Vec<String>, Vec<(String, Error)>) {
164211
let mut success_names = Vec::new();
165212
let mut failures = Vec::new();
166213

167214
for identifier in identifiers {
168-
match add_single(
169-
modrinth,
170-
curseforge,
171-
github,
172-
profile,
173-
&identifier,
174-
&Checks::new_all_set(),
175-
)
176-
.await
177-
{
178-
Ok(name) => success_names.push(name),
179-
Err(err) => failures.push((
180-
identifier,
181-
if matches!(err, Error::ModrinthError(ferinth::Error::InvalidIDorSlug)) {
182-
Error::InvalidIdentifier
183-
} else {
184-
err
185-
},
186-
)),
187-
}
215+
mod_provider
216+
.add(&identifier)
217+
.await
218+
.map(|name| success_names.push(name))
219+
.map_err(|err| {
220+
let ret_err =
221+
if matches!(err, Error::ModrinthError(ferinth::Error::InvalidIDorSlug)) {
222+
Error::InvalidIdentifier
223+
} else {
224+
err
225+
};
226+
failures.push((identifier, ret_err))
227+
})
228+
.ok();
188229
}
189230
(success_names, failures)
190231
}
@@ -197,22 +238,9 @@ pub async fn add_single(
197238
identifier: &str,
198239
checks: &Checks,
199240
) -> Result<String> {
200-
if let Ok(project_id) = identifier.parse() {
201-
curseforge::curseforge(curseforge, project_id, profile, checks).await
202-
} else if identifier.matches('/').count() == 1 {
203-
let split = identifier.split('/').collect::<Vec<_>>();
204-
github::github(
205-
&github.repos(split[0], split[1]),
206-
profile,
207-
checks.perform_checks(),
208-
checks,
209-
)
241+
ModProvider::new(modrinth, curseforge, github, checks, profile)
242+
.add(identifier)
210243
.await
211-
} else {
212-
modrinth::modrinth(modrinth, identifier, profile, checks)
213-
.await
214-
.map(|o| o.0)
215-
}
216244
}
217245

218246
#[cfg(test)]
@@ -247,5 +275,11 @@ mod test {
247275
check.set_game_version();
248276

249277
assert!(check.game_version());
278+
279+
let check = Checks::from(true, false, true);
280+
281+
assert!(check.perform_checks());
282+
assert!(!check.game_version());
283+
assert!(check.mod_loader());
250284
}
251285
}

0 commit comments

Comments
 (0)