summaryrefslogtreecommitdiff
path: root/src/commands/io.rs
blob: dd294bc6bb3bacb7fb90afae89315b26d53f47c2 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::prelude::*;

use crate::{
    config::Cfg,
    db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_set_version, userlist_get_all_ids, userlist_get_current_version},
    error::MLE,
    mod_add, IDSelector, List, Modloader, AddMod,
};

#[derive(Debug, Serialize, Deserialize)]
struct Export {
    lists: Vec<ExportList>,
}

#[derive(Debug, Serialize, Deserialize)]
struct ExportVersion {
    version: String,
    set: bool
}

impl ExportVersion {
    fn from(config: Cfg, list_id: &str, mod_id: &str) -> MLE<Self> {
        Ok(Self {
            version: userlist_get_current_version(config.clone(), list_id, mod_id)?,
            set: userlist_get_set_version(config.clone(), list_id, mod_id)?
        })
    }
}

#[derive(Debug, Serialize, Deserialize)]
struct ExportList {
    id: String,
    versions: Vec<ExportVersion>,
    launcher: String,
    mc_version: String,
    download_folder: Option<String>,
}

impl ExportList {
    pub fn from(config: Cfg, list_id: String, download: bool) -> MLE<Self> {
        let list = lists_get(config.clone(), String::from(&list_id))?;

        let mut dl_folder = None;
        if download {
            dl_folder = Some(list.download_folder)
        };

        let mods = userlist_get_all_ids(config.clone(), &list_id)?;
        let mut versions = vec![];
        for m in mods {
            versions.push(ExportVersion::from(config.clone(), &list_id, &m)?)
        }

        Ok(Self {
            id: list.id,
            versions,
            launcher: list.modloader.to_string(),
            mc_version: list.mc_version,
            download_folder: dl_folder,
        })
    }
}

pub fn export(config: Cfg, list: Option<String>) -> MLE<()> {
    let mut list_ids: Vec<String> = vec![];
    if list.is_none() {
        list_ids = lists_get_all_ids(config.clone())?;
    } else {
        list_ids.push(lists_get(config.clone(), list.unwrap())?.id);
    }
    let mut lists: Vec<ExportList> = vec![];
    for list_id in list_ids {
        lists.push(ExportList::from(config.clone(), list_id, true)?);
    }

    let toml = toml::to_string(&Export { lists })?;

    let filestr = dirs::home_dir().unwrap().join("mlexport.toml");

    let mut file = File::create(filestr.into_os_string().into_string().unwrap().as_str())?;
    file.write_all(toml.as_bytes())?;

    Ok(())
}

pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE<()> {
    let mut file = File::open(file_str)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    let export: Export = toml::from_str(&content)?;

    for exportlist in export.lists {
        let list = List {
            id: exportlist.id,
            mc_version: exportlist.mc_version,
            modloader: Modloader::from(&exportlist.launcher)?,
            download_folder: exportlist.download_folder.ok_or("NO_DL").unwrap(),
        };
        lists_insert(
            config.clone(),
            list.id.clone(),
            list.mc_version.clone(),
            list.modloader.clone(),
            String::from(&list.download_folder),
        )?;

        let mut ver_ids = vec![];
        for id in exportlist.versions {
            ver_ids.push(AddMod { id: IDSelector::VersionID(id.version), set_version: id.set} );
        }
        mod_add(config.clone(), ver_ids, list, direct_download).await?;
    }
    Ok(())
}