From 3f91f620474ff3dae5331f30487e4c3ed527fb99 Mon Sep 17 00:00:00 2001 From: endernon Date: Tue, 14 Jan 2025 22:30:32 +0000 Subject: [PATCH] crazy optimization --- src/errorfr.rs | 16 ++++++++++++---- src/jsondl.rs | 43 +++++++++++++++++++++++-------------------- src/main.rs | 3 ++- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/errorfr.rs b/src/errorfr.rs index cdcf8e0..03905d8 100644 --- a/src/errorfr.rs +++ b/src/errorfr.rs @@ -16,8 +16,8 @@ pub enum Errorfr { IDMapJsonMissing, /// idmap is corrupt - #[error("Error 2.2: id_keys.json is corrupt. \nYou should run \"--download id_keys\" or \"--download All\".")] - IDMapJsonCorrupt, + #[error("Error 2.2: id_keys.json is corrupt. \nYou should run \"--download id_keys\" or \"--download All\".\n{0}")] + IDMapJsonCorrupt(serde_json::Error), /// shiny data json is missing #[error("Error 2.3: shiny_stats.json is missing. \nYou should run \"--download ShinyStats\" or \"--download All\".")] @@ -33,10 +33,18 @@ pub enum Errorfr { /// gear data json is corrupt #[error("Error 2.6: gear.json is corrupt. It's only required for the \"perfect mode\" item gen. \nYou should run \"--download Gear\" or \"--download All\".\n{0:?}")] - GearJsonCorrupt(serde_json5::Error), + GearJsonCorrupt(serde_json::Error), + + /// gear json is missing + #[error("Error 2.7: gear_cache.json is missing. It's only required for the \"perfect mode\" item gen. \nYou should run \"--download Gear\" or \"--download All\".")] + GearJsonCacheMissing, + + /// gear data json is corrupt + #[error("Error 2.8: gear_cache.json is corrupt. It's only required for the \"perfect mode\" item gen. \nYou should run \"--download Gear\" or \"--download All\".\n{0:?}")] + GearJsonCacheCorrupt(serde_json::Error), /// gear data json cache could not be created - #[error("Error 2.7: unable to create file gear_cache.json.")] + #[error("Error 2.8: unable to create file gear_cache.json.")] GearJsonCacheCreateFail, diff --git a/src/jsondl.rs b/src/jsondl.rs index 84aaf08..eae86f6 100644 --- a/src/jsondl.rs +++ b/src/jsondl.rs @@ -1,38 +1,41 @@ -use crate::dl_json; -use crate::errorfr::Errorfr; -use crate::gearjson; -use crate::jsonstruct::Shinystruct; +use crate::{dl_json, + errorfr::Errorfr, + gearjson, + jsonstruct::Shinystruct +}; use serde::Deserialize; -use std::collections::HashMap; -use std::{fs, io}; -use std::io::Write; +use std::{ + collections::HashMap, + fs, + io::{BufReader, Write} +}; pub fn load_idkeys(executable_path: &str) -> Result, Errorfr> { // id_keys.json - serde_json5::from_reader( + serde_json::from_reader(&mut BufReader::new( &mut fs::File::open(executable_path.to_owned() + "/data/id_keys.json") - .map_err(|_| Errorfr::IDMapJsonMissing)?) - .map_err(|_| Errorfr::IDMapJsonCorrupt) + .map_err(|_| Errorfr::IDMapJsonMissing)?)) + .map_err(Errorfr::IDMapJsonCorrupt) } pub fn load_shinystats(executable_path: &str) -> Result, Errorfr> { // shiny_stats.json - serde_json5::from_reader( - &mut fs::File::open(executable_path.to_owned() + "/data/shiny_stats.json") - .map_err(|_| Errorfr::ShinyJsonMissing)?) + serde_json::from_reader(&mut BufReader::new( + fs::File::open(executable_path.to_owned() + "/data/shiny_stats.json") + .map_err(|_| Errorfr::ShinyJsonMissing)?)) .map_err(|_| Errorfr::ShinyJsonCorrupt) } pub fn load_gear(executable_path: &str) -> Result, Errorfr> { // gear.json (ONLY FOR DL gear.json) - serde_json5::from_reader( - &mut fs::File::open(executable_path.to_owned() + "/data/gear.json") - .map_err(|_| Errorfr::GearJsonMissing)?) - .map_err(Errorfr::GearJsonCorrupt) + serde_json::from_reader(&mut BufReader::new( + fs::File::open(executable_path.to_owned() + "/data/gear.json") + .map_err(|_| Errorfr::GearJsonMissing)?)) + .map_err(Errorfr::GearJsonCacheCorrupt) } pub fn load_gear_cache(executable_path: &str) -> Result, Errorfr> { // gear_cache.json (ONLY FOR PERFECT ITEM FUNCTION GEN) - serde_json5::from_reader( - &mut fs::File::open(executable_path.to_owned() + "/data/gear_cache.json") - .map_err(|_| Errorfr::GearJsonMissing)?) + serde_json::from_reader(&mut BufReader::new( + fs::File::open(executable_path.to_owned() + "/data/gear_cache.json") + .map_err(|_| Errorfr::GearJsonCacheMissing)?)) .map_err(Errorfr::GearJsonCorrupt) } diff --git a/src/main.rs b/src/main.rs index 374fff7..ba2acb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use clap::Parser; use idmangler_lib::{encoding::string::encode_string, types::EncodingVersion}; use reqwest::Url; use std::{collections::HashMap, env, fs, io, path::PathBuf}; +use std::io::Write; #[derive(Parser, Debug, Clone)] #[command(version, about, long_about = None, arg_required_else_help(true))] @@ -41,7 +42,7 @@ fn dl_json(url: Url, savename: String) -> Result<(), Errorfr> { let savepath = savename.to_string(); println!("Downloading file to {savepath}"); let mut out = fs::File::create(savepath).map_err(|_| Errorfr::JsonDlReqFileCreateFail)?; - io::copy(&mut body.as_bytes(), &mut out).map_err(|_| Errorfr::JsonDlReqFileWriteFail)?; + out.write_all(body.as_bytes()).map_err(|_| Errorfr::JsonDlReqFileWriteFail)?; Ok(()) }