summaryrefslogtreecommitdiff
path: root/src/commands/add.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/add.rs')
-rw-r--r--src/commands/add.rs52
1 files changed, 0 insertions, 52 deletions
diff --git a/src/commands/add.rs b/src/commands/add.rs
deleted file mode 100644
index ed4a6d8..0000000
--- a/src/commands/add.rs
+++ /dev/null
@@ -1,52 +0,0 @@
1use std::io::{Error, ErrorKind};
2
3use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader, input::Input};
4
5pub async fn modification(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
6
7 if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) }
8
9 let arguments = Input::from(args.unwrap().join(" "))?;
10
11 if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
12
13 match arguments.command.as_str() {
14 "add" => {
15 add(config, arguments.args.unwrap()).await
16 },
17 _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND")))
18 }
19}
20
21pub async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
22
23 if args.len() < 1 { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
24
25 let project = project(String::from(&config.apis.modrinth), &args[0]).await;
26
27 dbg!(&project);
28
29 let loader = Modloader::Fabric;
30
31 if project.versions.is_empty() { panic!("This should never happen"); };
32
33 let current_version = get_current(config, String::from(&project.id)).await?;
34
35 //add to current list and mod table
36 match insert_mod(project.id, project.title, current_version, project.versions, loader, String::from("1.19.2")) {
37 Err(err) => { Err(Box::new(err)) },
38 Ok(()) => Ok(()),
39 }
40
41}
42
43async fn get_current(config: Cfg, id: String) -> Result<String, Box<dyn std::error::Error>> {
44 let available_versions = versions(config.apis.modrinth, id, Modloader::Fabric, String::from("1.19.2")).await;
45
46 match available_versions.len() {
47 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))),
48 //TODO compare publish dates
49 1.. => Ok(available_versions[0].id.to_string()),
50 _ => panic!("available_versions should never be negative"),
51 }
52}