crazy optimization

This commit is contained in:
endernon 2025-01-14 22:30:32 +00:00
parent 192765a3ec
commit 3f91f62047
3 changed files with 37 additions and 25 deletions

View file

@ -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,

View file

@ -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<HashMap<String, u8>, 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<Vec<Shinystruct>, 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<HashMap<String, gearjson::GearJsonItem>, 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<HashMap<String, gearjson::GearJsonItem>, 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)
}

View file

@ -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(())
}