summaryrefslogtreecommitdiff
path: root/src/commands/io.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/io.rs')
-rw-r--r--src/commands/io.rs51
1 files changed, 38 insertions, 13 deletions
diff --git a/src/commands/io.rs b/src/commands/io.rs
index 5de8dd1..7f03eec 100644
--- a/src/commands/io.rs
+++ b/src/commands/io.rs
@@ -1,12 +1,18 @@
1use serde::{Deserialize, Serialize};
1use std::fs::File; 2use std::fs::File;
2use std::io::prelude::*; 3use std::io::prelude::*;
3use serde::{Serialize, Deserialize};
4 4
5use crate::{db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, List, devdir, error::MLE, mod_add, IDSelector}; 5use crate::{
6 config::Cfg,
7 db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids},
8 devdir,
9 error::MLE,
10 mod_add, IDSelector, List, Modloader,
11};
6 12
7#[derive(Debug, Serialize, Deserialize)] 13#[derive(Debug, Serialize, Deserialize)]
8struct Export { 14struct Export {
9 lists: Vec<ExportList> 15 lists: Vec<ExportList>,
10} 16}
11 17
12#[derive(Debug, Serialize, Deserialize)] 18#[derive(Debug, Serialize, Deserialize)]
@@ -20,15 +26,22 @@ struct ExportList {
20 26
21impl ExportList { 27impl ExportList {
22 pub fn from(config: Cfg, list_id: String, download: bool) -> MLE<Self> { 28 pub fn from(config: Cfg, list_id: String, download: bool) -> MLE<Self> {
23
24 let list = lists_get(config.clone(), String::from(&list_id))?; 29 let list = lists_get(config.clone(), String::from(&list_id))?;
25 30
26 let mut dl_folder = None; 31 let mut dl_folder = None;
27 if download{ dl_folder = Some(list.download_folder) }; 32 if download {
33 dl_folder = Some(list.download_folder)
34 };
28 35
29 let mods = userlist_get_all_ids(config, list_id)?.join("|"); 36 let mods = userlist_get_all_ids(config, list_id)?.join("|");
30 37
31 Ok(Self { id: list.id, mods, launcher: list.modloader.to_string(), mc_version: list.mc_version, download_folder: dl_folder }) 38 Ok(Self {
39 id: list.id,
40 mods,
41 launcher: list.modloader.to_string(),
42 mc_version: list.mc_version,
43 download_folder: dl_folder,
44 })
32 } 45 }
33} 46}
34 47
@@ -43,32 +56,44 @@ pub fn export(config: Cfg, list: Option<String>) -> MLE<()> {
43 for list_id in list_ids { 56 for list_id in list_ids {
44 lists.push(ExportList::from(config.clone(), list_id, true)?); 57 lists.push(ExportList::from(config.clone(), list_id, true)?);
45 } 58 }
46 59
47 let toml = toml::to_string( &Export { lists } )?; 60 let toml = toml::to_string(&Export { lists })?;
48 61
49 let filestr = dirs::home_dir().unwrap().join("mlexport.toml"); 62 let filestr = dirs::home_dir().unwrap().join("mlexport.toml");
50 63
51 let mut file = File::create(devdir(filestr.into_os_string().into_string().unwrap().as_str()))?; 64 let mut file = File::create(devdir(
65 filestr.into_os_string().into_string().unwrap().as_str(),
66 ))?;
52 file.write_all(toml.as_bytes())?; 67 file.write_all(toml.as_bytes())?;
53 68
54 Ok(()) 69 Ok(())
55} 70}
56 71
57pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE<()> { 72pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE<()> {
58
59 let mut file = File::open(file_str)?; 73 let mut file = File::open(file_str)?;
60 let mut content = String::new(); 74 let mut content = String::new();
61 file.read_to_string(&mut content)?; 75 file.read_to_string(&mut content)?;
62 let export: Export = toml::from_str(&content)?; 76 let export: Export = toml::from_str(&content)?;
63 77
64 for exportlist in export.lists { 78 for exportlist in export.lists {
65 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() }; 79 let list = List {
66 lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?; 80 id: exportlist.id,
81 mc_version: exportlist.mc_version,
82 modloader: Modloader::from(&exportlist.launcher)?,
83 download_folder: exportlist.download_folder.ok_or("NO_DL").unwrap(),
84 };
85 lists_insert(
86 config.clone(),
87 list.id.clone(),
88 list.mc_version.clone(),
89 list.modloader.clone(),
90 String::from(&list.download_folder),
91 )?;
67 let mods: Vec<&str> = exportlist.mods.split('|').collect(); 92 let mods: Vec<&str> = exportlist.mods.split('|').collect();
68 let mut mod_ids = vec![]; 93 let mut mod_ids = vec![];
69 for mod_id in mods { 94 for mod_id in mods {
70 mod_ids.push(IDSelector::ModificationID(String::from(mod_id))); 95 mod_ids.push(IDSelector::ModificationID(String::from(mod_id)));
71 }; 96 }
72 //TODO impl set_version and good direct download 97 //TODO impl set_version and good direct download
73 //TODO impl all at once, dafuck 98 //TODO impl all at once, dafuck
74 mod_add(config.clone(), mod_ids, list, direct_download, false).await?; 99 mod_add(config.clone(), mod_ids, list, direct_download, false).await?;