summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: e4ebf76acb5948d6df68cdb6a139beb49dac561a (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
53
54
55
56
pub mod apis;
pub mod config;
pub mod commands;
pub mod input;
pub mod db;
pub mod error;

use std::{io::{Error, ErrorKind, Write}, fs::File};

pub use apis::*;
pub use commands::*;
use futures_util::StreamExt;
use reqwest::Client;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Modloader {
    Fabric,
    Forge
}

impl Modloader {
    fn stringify(&self) -> String {
        match self {
            Modloader::Fabric => String::from("fabric"),
            Modloader::Forge => String::from("forge"),
        }
    }

    fn from(string: &str) -> Result<Modloader, Box<Error>> {
        match string {
            "forge" => Ok(Modloader::Forge),
            "fabric" => Ok(Modloader::Fabric),
            _ => Err(Box::new(Error::new(ErrorKind::InvalidData, "UNKNOWN_MODLOADER")))
        }
    }
}

pub async fn download_file(url: String, path: String, name: String) -> Result<(), Box<dyn std::error::Error>> {
    println!("Downloading {}", url);
    let dl_path_file = format!("{}/{}", path, name);
    let res = Client::new()
        .get(String::from(&url))
        .send()
        .await?;
    
    // download chunks
    let mut file = File::create(String::from(&dl_path_file))?;
    let mut stream = res.bytes_stream();

    while let Some(item) = stream.next().await {
        let chunk = item?;
        file.write_all(&chunk)?;
    }

    Ok(())
}