finally, proper error handling support

This commit is contained in:
endernon 2024-11-05 19:17:45 +00:00
parent e1cbcfc193
commit c1cca55db7
2 changed files with 28 additions and 25 deletions

View file

@ -3,29 +3,28 @@ use thiserror::Error;
/// Errors yep /// Errors yep
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Errorfr { pub enum Errorfr {
/// config json is missing /// item 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. " #[error("Error 1: item config json is missing")]
)] ItemJsonMissing,
JsonMissing,
/// config json is corrupt /// item 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. " #[error("Error 2: item config json is corrupt, Reread config.md"
)] )]
JsonCorrupt, ItemJsonCorrupt,
/// idmap is missing /// 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. ")] #[error("Error 3: Identifications hashmap is missing")]
IDMapMissing, IDMapJsonMissing,
/// idmap is corrupt /// 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. ")] #[error("Error 4: Identifications hashmap is corrupt")]
IDMapCorrupt, IDMapJsonCorrupt,
/// shiny data json is missing /// 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, ShinyJsonMissing,
/// shiny data json is corrupt /// 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 ShinyJsonCorrupt
} }

View file

@ -31,11 +31,12 @@ struct Args {
// const fallbackconfigpath: String = "config.json".to_owned(); // const fallbackconfigpath: String = "config.json".to_owned();
#[derive(Debug)] fn main() {
struct E; if let Err(e) = cook() {
println!("{}",e);
}
fn main() -> Result<(), E> { }
fn cook() -> Result<(), Errorfr> {
// enable fancypanic when building for release // enable fancypanic when building for release
// fancypanic(); // fancypanic();
let args = Args::parse(); let args = Args::parse();
@ -52,13 +53,14 @@ fn main() -> Result<(), E> {
// newest json reading code // newest json reading code
let json_config: Jsonconfig = serde_json::from_reader(fs::File::open(configpath) let json_config: Jsonconfig = serde_json::from_reader(fs::File::open(configpath)
.map_err(|_| Errorfr::JsonMissing)?) .map_err(|_| Errorfr::ItemJsonMissing)?)
.map_err(|_| Errorfr::JsonCorrupt)?; .map_err(|_| Errorfr::ItemJsonCorrupt)?;
let idsmap: HashMap<String, u8> = let idsmap: HashMap<String, u8> = serde_json::from_reader(fs::File::open("id_keys.json")
serde_json::from_reader(fs::File::open("id_keys.json").expect(ERROR[3])).expect(ERROR[4]); .map_err(|_| Errorfr::IDMapJsonMissing)?)
let json_shiny: Vec<Shinystruct> = .map_err(|_| Errorfr::IDMapJsonCorrupt)?;
serde_json::from_reader(fs::File::open("shiny_stats.json").expect(ERROR[5])) let json_shiny: Vec<Shinystruct> = serde_json::from_reader(fs::File::open("shiny_stats.json")
.expect(ERROR[6]); .map_err(|_| Errorfr::ShinyJsonMissing)?)
.map_err(|_| Errorfr::ShinyJsonCorrupt)?;
// println!("{:?}",idsmap.get("airDamage")); // println!("{:?}",idsmap.get("airDamage"));
let mut out = Vec::new(); let mut out = Vec::new();
@ -243,6 +245,8 @@ fn main() -> Result<(), E> {
//let out = decode(&mut bytes_iter).unwrap(); //let out = decode(&mut bytes_iter).unwrap();
// println!("{:#?}", out); // println!("{:#?}", out);
Ok(())
} }
fn fancypanic() { fn fancypanic() {