From ddde9204c72dd867f920f07f6483be03dda7cf68 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 28 Nov 2022 22:55:14 +0100 Subject: basically update impl; added "good" download; auto dl on new mod; db to 0.4; etc --- src/commands/modification.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 519a0cb..8e39d11 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,12 +1,12 @@ use std::io::{Error, ErrorKind}; -use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list}; +use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list, download_versions}; pub async fn modification(config: Cfg, input: Input) -> Result<(), Box> { - match input.subcommand.ok_or("")? { + match input.subcommand.as_ref().ok_or("")? { Subcmd::Add => { - add(config, input.args.ok_or("")?).await + add(config, input).await }, Subcmd::Remove => { remove(config, input.args.ok_or("")?) @@ -15,23 +15,23 @@ pub async fn modification(config: Cfg, input: Input) -> Result<(), Box) -> Result<(), Box> { - //TODO! DO NOT PANIC IF MOD IS ALREADY IN MODS DB +async fn add(config: Cfg, input: Input) -> Result<(), Box> { + + let args = input.args.ok_or("")?; + if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; let current_list = get_current_list(config.clone())?; let project = project(String::from(&config.apis.modrinth), &args[0]).await; - if project.versions.is_empty() { panic!("This should never happen"); }; - let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; let current_id = extract_current_version(available_versions.clone())?; let current_version = available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap(); - let file = current_version.files.into_iter().find(|f| f.primary).unwrap().url; + let file = current_version.clone().files.into_iter().find(|f| f.primary).unwrap().url; let mut available_versions_vec: Vec = Vec::new(); for ver in available_versions { @@ -43,16 +43,16 @@ async fn add(config: Cfg, args: Vec) -> Result<(), Box userlist_insert(config.clone(), current_list.id, String::from(&project.id), current_version.id, available_versions_vec, file)?, + Err(..) => userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version.id), available_versions_vec, file)?, }; match mods_get_all_ids(config.clone()) { Ok(mods) => { if mods.contains(&project.id) { - return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) + //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) } else { mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; } @@ -61,6 +61,9 @@ async fn add(config: Cfg, args: Vec) -> Result<(), Box