From 0c7f30a73dd0854e09fe980e878aba7ff7f49543 Mon Sep 17 00:00:00 2001 From: endernon Date: Tue, 5 Nov 2024 18:45:09 +0000 Subject: [PATCH] proper error handling v2 (does not compile for now) --- src/errorfr/mod.rs | 31 +++++++++++++++++++++++++++++++ src/errors/mod.rs | 43 ------------------------------------------- src/main.rs | 11 ++++++----- 3 files changed, 37 insertions(+), 48 deletions(-) create mode 100644 src/errorfr/mod.rs delete mode 100644 src/errors/mod.rs diff --git a/src/errorfr/mod.rs b/src/errorfr/mod.rs new file mode 100644 index 0000000..13644b4 --- /dev/null +++ b/src/errorfr/mod.rs @@ -0,0 +1,31 @@ +use thiserror::Error; + +/// Errors yep +#[derive(Error, Debug)] +pub enum Errorfr { + /// config json is missing + #[error("Error 1: json config json is missing, obtain it from https://git.frfrnocap.men/endernon/idmangler-cli/raw/branch/main/config.json and move it to this directory. " + )] + JsonMissing, + + /// config json is corrupt + #[error("Error 2: json config json is corrupt. Reread config.md or reobtain it from https://git.frfrnocap.men/endernon/idmangler-cli/raw/branch/main/config.json and move it to this diirectory. " + )] + JsonCorrupt, + + /// idmap is missing + #[error("Error 3: Identifications hashmap is missing. Get it from https://raw.githubusercontent.com/Wynntils/Static-Storage/main/Reference/id_keys.json and move it to this directory. ")] + IDMapMissing, + + /// idmap is corrupt + #[error("Error 4: Identifications hashmap is corrupt. Reobtain it from https://raw.githubusercontent.com/Wynntils/Static-Storage/main/Reference/id_keys.json and move it to this directory. ")] + IDMapCorrupt, + + /// shiny data json is missing + #[error("Error 5: Shiny data json is missing. Get it from https://raw.githubusercontent.com/Wynntils/Static-Storage/main/Data-Storage/shiny_stats.json and move it to this directory. ")] + ShinyJsonMissing, + + /// shiny data json is corrupt + #[error("Error 6: Shiny data json is corrupt. Get it from https://raw.githubusercontent.com/Wynntils/Static-Storage/main/Data-Storage/shiny_stats.json and move it to this directory. ")] + ShinyJsonCorrupt +} \ No newline at end of file diff --git a/src/errors/mod.rs b/src/errors/mod.rs deleted file mode 100644 index 9bbab94..0000000 --- a/src/errors/mod.rs +++ /dev/null @@ -1,43 +0,0 @@ -/// Potential errors thrown during encoding of id strings -#[derive(Error, Debug)] -pub enum EncodeError { - /// Encoder was given a string with non ascii characters. - #[error("Cannot encode non ascii string")] - NonAsciiString, - - /// More than 255 identifications were passed for encoding - #[error("Cannot encode more than 255 identifications per item")] - TooManyIdentifications, - /// Identification is missing a basevalue while using the extended encoding scheme. - /// - /// An id is required to have an basevalue if the extended encoding is used for idents - #[error("Identification id: {0} was not given a base value while using extended encoding")] - NoBasevalueGiven(u8), - - /// More than 255 powders were passed for encoding - #[error("Cannot encode more than 255 powders per item")] - TooManyPowders, - /// Invalid tier for a powder was passed - #[error("Invalid powder tier of {0} was passed")] - InvalidPowderTier(u8), - - /// Effect strength should be a percentage between 0 and 100 - #[error("Effect strength of {0} is too high, it should be a percentage between 0 and 100")] - EffectStrengthTooHigh(u8), - - /// More than 255 skills were passed for encoding - #[error("Cannot encode more than 255 skills per item")] - TooManySkills, - - /// More than 255 damage values were passed for encoding - #[error("Cannot encode more than 255 damage values per item")] - TooManyDamageValues, - - /// More than 255 effects were passed for encoding - #[error("Cannot encode more than 255 effects per item")] - TooManyEffects, - - /// More than 255 defense values were passed for encoding - #[error("Cannot encode more than 255 defense values per item")] - TooManyDefences, -} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index febff26..96c54e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,8 @@ use std::fs; use std::panic; mod structures; use crate::structures::*; -mod errors; - +mod errorfr; +use crate::errorfr::Errorfr; use clap::Parser; @@ -31,7 +31,7 @@ struct Args { // const fallbackconfigpath: String = "config.json".to_owned(); -fn main() { +fn main() -> Result<(), Errors> { // enable fancypanic when building for release // fancypanic(); let args = Args::parse(); @@ -47,8 +47,9 @@ fn main() { } // newest json reading code - let json_config: Jsonconfig = - serde_json::from_reader(fs::File::open(configpath).expect(ERROR[1])).expect(ERROR[2]); + let json_config: Jsonconfig = serde_json::from_reader(fs::File::open(configpath) + .map_err(|_| Errorfr::JsonMissing)?) + .map_err(|_| Errorfr::JsonCorrupt)?; let idsmap: HashMap = serde_json::from_reader(fs::File::open("id_keys.json").expect(ERROR[3])).expect(ERROR[4]); let json_shiny: Vec =