From b125dfd03084fff47ab8e90d002c6699b762d998 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Tue, 1 Nov 2022 23:00:45 +0100 Subject: added list stuff + beginnings of mods --- src/commands/add.rs | 28 ++++++++++++++---- src/commands/list.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 2 ++ 3 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 src/commands/list.rs (limited to 'src/commands') diff --git a/src/commands/add.rs b/src/commands/add.rs index 67f63de..ed4a6d8 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -1,11 +1,28 @@ use std::io::{Error, ErrorKind}; -use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader}; +use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader, input::Input}; -pub async fn add(config: Cfg, mc_mod: String) -> Result<(), Box> { - println!("Adding"); +pub async fn modification(config: Cfg, args: Option>) -> Result<(), Box> { - let project = project(String::from(&config.apis.modrinth), &mc_mod).await; + if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) } + + let arguments = Input::from(args.unwrap().join(" "))?; + + if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; + + match arguments.command.as_str() { + "add" => { + add(config, arguments.args.unwrap()).await + }, + _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND"))) + } +} + +pub async fn add(config: Cfg, args: Vec) -> Result<(), Box> { + + if args.len() < 1 { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; + + let project = project(String::from(&config.apis.modrinth), &args[0]).await; dbg!(&project); @@ -14,7 +31,8 @@ pub async fn add(config: Cfg, mc_mod: String) -> Result<(), Box { Err(Box::new(err)) }, Ok(()) => Ok(()), diff --git a/src/commands/list.rs b/src/commands/list.rs new file mode 100644 index 0000000..6c260ce --- /dev/null +++ b/src/commands/list.rs @@ -0,0 +1,80 @@ +use std::io::{Error, ErrorKind}; + +use crate::{db::{insert_list, remove_list, change_list, get_lists, get_current_list}, Modloader, config::Cfg, input::Input}; + +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(config)?; + println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list); + return Ok(()); + } + + let arguments = Input::from(args.unwrap().join(" "))?; + + if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; + + match arguments.command.as_str() { + "add" => { + add(config, arguments.args.unwrap()) + }, + "change" => { + change(config, arguments.args.unwrap()) + }, + "remove" => { + remove(config, arguments.args.unwrap()) + }, + _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND"))) + } +} + +fn add(config: Cfg, args: Vec) -> Result<(), Box> { + match args.len() { + 1 | 2 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), + 3 => { + let id = String::from(&args[0]); + let mc_version = String::from(&args[1]); + let mod_loader = match args[2].as_str() { + "forge" => Modloader::Forge, + "fabric" => Modloader::Fabric, + _ => return Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_MODLOADER"))) + }; + match insert_list(config, id, mc_version, mod_loader) { + Err(err) => { Err(Box::new(err)) }, + Ok(()) => Ok(()), + } + }, + 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), + _ => panic!("list arguments should never be zero or lower"), + } +} + +fn change(config: Cfg, args: Vec) -> Result<(), Box> { + let lists = get_lists(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(err)) }, + Ok(()) => Ok(()), + } + }, + 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), + _ => panic!("list arguments should never be zero or lower"), + } +} + +fn remove(config: Cfg, args: Vec) -> Result<(), Box> { + match args.len() { + 1 => { + match remove_list(config, String::from(&args[0])) { + Err(err) => { Err(Box::new(err)) }, + Ok(()) => Ok(()), + } + }, + 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), + _ => panic!("list arguments should never be zero or lower"), + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0cc183a..6432746 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,3 +1,5 @@ pub mod add; +pub mod list; pub use add::*; +pub use list::*; -- cgit v1.2.3