summaryrefslogtreecommitdiff
path: root/src/commands/add.rs
blob: ed4a6d80d537ed256b4013c8d64600a9dc123fdb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::io::{Error, ErrorKind};

use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader, input::Input};

pub async fn modification(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
    
    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<String>) -> Result<(), Box<dyn std::error::Error>> {
    
    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);

    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?;
    
    //add to current list and mod table
    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<String, Box<dyn std::error::Error>> {
    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"),
    }
}