diff options
Diffstat (limited to 'src/commands/add.rs')
-rw-r--r-- | src/commands/add.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/commands/add.rs b/src/commands/add.rs new file mode 100644 index 0000000..67f63de --- /dev/null +++ b/src/commands/add.rs | |||
@@ -0,0 +1,34 @@ | |||
1 | use std::io::{Error, ErrorKind}; | ||
2 | |||
3 | use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader}; | ||
4 | |||
5 | pub async fn add(config: Cfg, mc_mod: String) -> Result<(), Box<dyn std::error::Error>> { | ||
6 | println!("Adding"); | ||
7 | |||
8 | let project = project(String::from(&config.apis.modrinth), &mc_mod).await; | ||
9 | |||
10 | dbg!(&project); | ||
11 | |||
12 | let loader = Modloader::Fabric; | ||
13 | |||
14 | if project.versions.is_empty() { panic!("This should never happen"); }; | ||
15 | |||
16 | let current_version = get_current(config, String::from(&project.id)).await?; | ||
17 | |||
18 | match insert_mod(project.id, project.title, current_version, project.versions, loader, String::from("1.19.2")) { | ||
19 | Err(err) => { Err(Box::new(err)) }, | ||
20 | Ok(()) => Ok(()), | ||
21 | } | ||
22 | |||
23 | } | ||
24 | |||
25 | async fn get_current(config: Cfg, id: String) -> Result<String, Box<dyn std::error::Error>> { | ||
26 | let available_versions = versions(config.apis.modrinth, id, Modloader::Fabric, String::from("1.19.2")).await; | ||
27 | |||
28 | match available_versions.len() { | ||
29 | 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))), | ||
30 | //TODO compare publish dates | ||
31 | 1.. => Ok(available_versions[0].id.to_string()), | ||
32 | _ => panic!("available_versions should never be negative"), | ||
33 | } | ||
34 | } | ||