From fc1cb1acc0dce412e948475002666bcd1d4b0348 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 31 Oct 2022 22:41:18 +0100 Subject: add first impl --- src/commands/add.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/commands/add.rs (limited to 'src/commands/add.rs') 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 @@ +use std::io::{Error, ErrorKind}; + +use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader}; + +pub async fn add(config: Cfg, mc_mod: String) -> Result<(), Box> { + println!("Adding"); + + let project = project(String::from(&config.apis.modrinth), &mc_mod).await; + + dbg!(&project); + + let loader = Modloader::Fabric; + + if project.versions.is_empty() { panic!("This should never happen"); }; + + let current_version = get_current(config, String::from(&project.id)).await?; + + match insert_mod(project.id, project.title, current_version, project.versions, loader, String::from("1.19.2")) { + Err(err) => { Err(Box::new(err)) }, + Ok(()) => Ok(()), + } + +} + +async fn get_current(config: Cfg, id: String) -> Result> { + let available_versions = versions(config.apis.modrinth, id, Modloader::Fabric, String::from("1.19.2")).await; + + match available_versions.len() { + 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))), + //TODO compare publish dates + 1.. => Ok(available_versions[0].id.to_string()), + _ => panic!("available_versions should never be negative"), + } +} -- cgit v1.2.3