From c1cca55db7db7d8c1379712ccac290329560168c Mon Sep 17 00:00:00 2001 From: endernon Date: Tue, 5 Nov 2024 19:17:45 +0000 Subject: [PATCH] finally, proper error handling support --- src/errorfr/mod.rs | 25 ++++++++++++------------- src/main.rs | 28 ++++++++++++++++------------ 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/errorfr/mod.rs b/src/errorfr/mod.rs index 13644b4..1f326e3 100644 --- a/src/errorfr/mod.rs +++ b/src/errorfr/mod.rs @@ -3,29 +3,28 @@ 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, + /// item config json is missing + #[error("Error 1: item config json is missing")] + ItemJsonMissing, - /// 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. " + /// item config json is corrupt + #[error("Error 2: item config json is corrupt, Reread config.md" )] - JsonCorrupt, + ItemJsonCorrupt, /// 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, + #[error("Error 3: Identifications hashmap is missing")] + IDMapJsonMissing, /// 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, + #[error("Error 4: Identifications hashmap is corrupt")] + IDMapJsonCorrupt, /// 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. ")] + #[error("Error 5: shiny_stats.json is missing.")] 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. ")] + #[error("Error 6: shiny_stats.json is corrupt.")] ShinyJsonCorrupt } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index fdf2906..ad38c11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,11 +31,12 @@ struct Args { // const fallbackconfigpath: String = "config.json".to_owned(); -#[derive(Debug)] -struct E; - - -fn main() -> Result<(), E> { +fn main() { + if let Err(e) = cook() { + println!("{}",e); + } +} +fn cook() -> Result<(), Errorfr> { // enable fancypanic when building for release // fancypanic(); let args = Args::parse(); @@ -52,13 +53,14 @@ fn main() -> Result<(), E> { // newest json reading code 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 = - serde_json::from_reader(fs::File::open("shiny_stats.json").expect(ERROR[5])) - .expect(ERROR[6]); + .map_err(|_| Errorfr::ItemJsonMissing)?) + .map_err(|_| Errorfr::ItemJsonCorrupt)?; + let idsmap: HashMap = serde_json::from_reader(fs::File::open("id_keys.json") + .map_err(|_| Errorfr::IDMapJsonMissing)?) + .map_err(|_| Errorfr::IDMapJsonCorrupt)?; + let json_shiny: Vec = serde_json::from_reader(fs::File::open("shiny_stats.json") + .map_err(|_| Errorfr::ShinyJsonMissing)?) + .map_err(|_| Errorfr::ShinyJsonCorrupt)?; // println!("{:?}",idsmap.get("airDamage")); let mut out = Vec::new(); @@ -243,6 +245,8 @@ fn main() -> Result<(), E> { //let out = decode(&mut bytes_iter).unwrap(); // println!("{:#?}", out); + Ok(()) + } fn fancypanic() {