From 889dc4f87b05d838b25428478a8c42dac454a5cf Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 7 Nov 2022 22:29:35 +0100 Subject: finished rusqlite; added all db tests --- src/commands/download.rs | 4 ++-- src/commands/list.rs | 27 ++++++++++++--------------- src/commands/modification.rs | 2 +- src/commands/setup.rs | 17 ++++++++--------- src/commands/update.rs | 13 ++++++++----- 5 files changed, 31 insertions(+), 32 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index 05c54cb..993294b 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -4,12 +4,12 @@ use reqwest::Client; use futures_util::StreamExt; -use crate::{get_current_list, config::Cfg, db::get_dl_links}; +use crate::{get_current_list, config::Cfg, db::userlist_get_all_downloads}; pub async fn download(config: Cfg) -> Result<(), Box> { let list = get_current_list(config.clone())?; - let links = get_dl_links(config.clone(), list)?; + let links = userlist_get_all_downloads(config.clone(), list.id)?; download_links(config, links).await?; diff --git a/src/commands/list.rs b/src/commands/list.rs index ffa5926..6c80e4e 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,8 +1,8 @@ use std::io::{Error, ErrorKind}; -use crate::{db::{lists_insert, remove_list, change_list, get_lists, get_current_list_id, get_list}, Modloader, config::Cfg, input::Input}; +use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get}, Modloader, config::Cfg, input::Input}; -#[derive(Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct List { pub id: String, pub mc_version: String, @@ -12,8 +12,8 @@ pub struct List { pub fn list(config: Cfg, args: Option>) -> Result<(), Box> { if args.is_none() { - let lists = get_lists(config.clone())?; - let current_list = get_current_list_id(config)?; + let lists = lists_get_all_ids(config.clone())?; + let current_list = config_get_current_list(config)?; println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list); return Ok(()); } @@ -37,8 +37,8 @@ pub fn list(config: Cfg, args: Option>) -> Result<(), Box Result> { - let id = get_current_list_id(config.clone())?; - get_list(config, id) + let id = config_get_current_list(config.clone())?; + lists_get(config, id) } fn add(config: Cfg, args: Vec) -> Result<(), Box> { @@ -52,10 +52,7 @@ fn add(config: Cfg, args: Vec) -> Result<(), Box> "fabric" => Modloader::Fabric, _ => return Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_MODLOADER"))) }; - match lists_insert(config, id, mc_version, mod_loader) { - Err(err) => { Err(Box::new(Error::new(ErrorKind::Other, "TBD"))) }, - Ok(()) => Ok(()), - } + lists_insert(config, id, mc_version, mod_loader) }, 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), _ => panic!("list arguments should never be zero or lower"), @@ -63,13 +60,13 @@ fn add(config: Cfg, args: Vec) -> Result<(), Box> } fn change(config: Cfg, args: Vec) -> Result<(), Box> { - let lists = get_lists(config.clone())?; + let lists = lists_get_all_ids(config.clone())?; match args.len() { 1 => { let list = String::from(&args[0]); if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); }; - match change_list(config, list) { - Err(err) => { Err(Box::new(Error::new(ErrorKind::Other, "TBD"))) }, + match config_change_current_list(config, list) { + Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "72"))) }, Ok(()) => Ok(()), } }, @@ -81,8 +78,8 @@ fn change(config: Cfg, args: Vec) -> Result<(), Box) -> Result<(), Box> { match args.len() { 1 => { - match remove_list(config, String::from(&args[0])) { - Err(err) => { Err(Box::new(Error::new(ErrorKind::Other, "TBD"))) }, + match lists_remove(config, String::from(&args[0])) { + Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "85"))) }, Ok(()) => Ok(()), } }, diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 7836735..595b677 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -22,7 +22,7 @@ pub async fn modification(config: Cfg, args: Option>) -> Result<(), } async fn add(config: Cfg, args: Vec) -> Result<(), Box> { - + //TODO! DO NOT PANIC IF MOD IS ALREADY IN MODS DB if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; let current_list = get_current_list(config.clone())?; diff --git a/src/commands/setup.rs b/src/commands/setup.rs index 0940959..8c0fcfd 100644 --- a/src/commands/setup.rs +++ b/src/commands/setup.rs @@ -1,6 +1,6 @@ use std::{fs::File, path::Path, io::{Error, ErrorKind}}; -use crate::{config::Cfg, db::{db_setup, user_dbversion, create_dbversion, insert_column, get_lists, get_list, get_current_versions, insert_dl_link}, modrinth::get_raw_versions}; +use crate::{config::Cfg, db::{db_setup, s_config_get_version, s_config_create_version, s_insert_column, lists_get_all_ids, lists_get, userlist_get_all_current_version_ids, s_userlist_update_download}, modrinth::get_raw_versions}; pub async fn setup(config: Cfg) -> Result<(), Box> { @@ -10,7 +10,7 @@ pub async fn setup(config: Cfg) -> Result<(), Box> { return create(config, db_file); } - match user_dbversion(config.clone()) { + match s_config_get_version(config.clone()) { Ok(ver) => { match ver.as_str() { _ => return Err(Box::new(Error::new(ErrorKind::Other, "UNKNOWN_VERSION"))) @@ -29,26 +29,25 @@ fn create(config: Cfg, db_file: String) -> Result<(), Box } async fn to_02(config: Cfg) -> Result<(), Box> { - let lists = get_lists(config.clone())?; + let lists = lists_get_all_ids(config.clone())?; for list in lists { println!("Updating {}", list); - insert_column(config.clone(), String::from(&list), String::from("current_download"), String::new())?; + s_insert_column(config.clone(), String::from(&list), String::from("current_download"), String::new())?; - let full_list = get_list(config.clone(), String::from(&list))?; + let full_list = lists_get(config.clone(), String::from(&list))?; - let versions = get_current_versions(config.clone(), full_list.clone())?; + let versions = userlist_get_all_current_version_ids(config.clone(), full_list.clone().id)?; let raw_versions = get_raw_versions(String::from(&config.apis.modrinth), versions).await; for ver in raw_versions { println!("Adding link for {}", ver.project_id); let file = ver.files.into_iter().find(|f| f.primary).unwrap(); - insert_dl_link(config.clone(), full_list.clone(), ver.project_id, file.url)?; + s_userlist_update_download(config.clone(), String::from(&full_list.id), ver.project_id, file.url)?; } }; - create_dbversion(config)?; - + s_config_create_version(config)?; Ok(()) } diff --git a/src/commands/update.rs b/src/commands/update.rs index d278a78..e383eae 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -4,7 +4,7 @@ use reqwest::Client; use futures_util::StreamExt; -use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, get_versions, get_list_version, change_list_versions}, List}; +use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions}, List}; pub async fn update(config: Cfg) -> Result<(), Box> { @@ -12,7 +12,7 @@ pub async fn update(config: Cfg) -> Result<(), Box> { let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; - let mut versions = get_versions(config.clone(), mods.clone())?; + let mut versions = mods_get_versions(config.clone(), mods.clone())?; versions.sort_by_key(|ver| ver.mod_id.clone()); let mut projects = projects(String::from(&config.apis.modrinth), mods).await; @@ -54,12 +54,15 @@ async fn specific_update(config: Cfg, list: List, project: Project) -> Result = vec![]; - if versions.join("|") != get_list_version(config.clone(), list.clone(), String::from(&project.id))? { + if versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))? { //get new versions print!(" | getting new version"); let current_str = extract_current_version(applicable_versions.clone())?; - current.push(applicable_versions.into_iter().find(|ver| ver.id == current_str).unwrap()); - change_list_versions(config, list, current_str, versions, project.id)?; + let current_ver = applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("")?; + current.push(current_ver.clone()); + + let link = current_ver.files.into_iter().find(|f| f.primary).ok_or("")?.url; + userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?; } if current.is_empty() { return Err(Box::new(Error::new(ErrorKind::NotFound, "NO_UPDATE_AVAILABLE"))) }; -- cgit v1.2.3