From f87ac1a38af96087e8a6927a6cad7ca19b48d76d Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 19 Dec 2022 16:48:21 +0100 Subject: basic io implementation finished --- src/commands/io.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 5 deletions(-) (limited to 'src/commands/io.rs') diff --git a/src/commands/io.rs b/src/commands/io.rs index dc1f408..47991c5 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -1,12 +1,81 @@ -use crate::{input::{Input, Subcmd}, config::Cfg}; +use std::fs::File; +use std::io::prelude::*; +use serde::{Serialize, Deserialize}; -pub fn io(_config: Cfg, input: Input) -> Result<(), Box> { +use crate::{input::{Input, Subcmd}, db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, mod_add, List}; + +#[derive(Debug, Serialize, Deserialize)] +struct Export { + lists: Vec +} + +#[derive(Debug, Serialize, Deserialize)] +struct ExportList { + id: String, + mods: String, + launcher: String, + mc_version: String, + download_folder: Option, +} + +impl ExportList { + pub fn from(config: Cfg, list_id: String, download: bool) -> Result> { + + let list = lists_get(config.clone(), String::from(&list_id))?; + + let mut dl_folder = None; + if download == true { dl_folder = Some(list.download_folder) }; + + let mods = userlist_get_all_ids(config, list_id)?.join("|"); + + Ok(Self { id: list.id, mods, launcher: list.modloader.stringify(), mc_version: list.mc_version, download_folder: dl_folder }) + } +} + +pub async fn io(config: Cfg, input: Input) -> Result<(), Box> { match input.subcommand.ok_or("INVALID_INPUT")? { - Subcmd::Export => {}, - Subcmd::Import => {}, - _ => {}, + Subcmd::Export => { export(config, input.args)? }, + Subcmd::Import => { import(config).await? }, + _ => { }, + } + + Ok(()) +} + +fn export(config: Cfg, _args: Option>) -> Result<(), Box> { + let list_ids = lists_get_all_ids(config.clone())?; + let mut lists: Vec = vec![]; + for list_id in list_ids { + lists.push(ExportList::from(config.clone(), String::from(list_id), true)?); } + + let toml = toml::to_string( &Export { lists } )?; + + let mut file = File::create("export.toml")?; + file.write_all(&toml.as_bytes())?; Ok(()) } + +async fn import(config: Cfg) -> Result<(), Box> { + + let mut file = File::open("export.toml")?; + let mut content = String::new(); + file.read_to_string(&mut content)?; + let export: Export = toml::from_str(&content)?; + + println!("{:#?}", export); + + 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")? }; + lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?; + //TODO currently workaround, too many requests + let mods: Vec<&str> = exportlist.mods.split("|").collect(); + for mod_id in mods { + println!("Adding {}", mod_id); + mod_add(config.clone(), mod_id, list.clone(), false).await?; + } + } + Ok(()) +} -- cgit v1.2.3