error handling better edition

This commit is contained in:
endernon 2024-11-05 17:51:55 +00:00
parent 8bc131c7fe
commit 7570f39402
5 changed files with 76 additions and 31 deletions

13
Cargo.lock generated
View file

@ -127,6 +127,7 @@ dependencies = [
"idmangler-lib", "idmangler-lib",
"serde", "serde",
"serde_json", "serde_json",
"thiserror",
] ]
[[package]] [[package]]
@ -211,9 +212,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.77" version = "2.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -222,18 +223,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.63" version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" checksum = "02dd99dc800bbb97186339685293e1cc5d9df1f8fae2d0aecd9ff1c77efea892"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.63" version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" checksum = "a7c61ec9a6f64d2793d8a45faba21efbe3ced62a886d44c36a009b2b519b4c7e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

@ -9,3 +9,4 @@ clap = { version = "4.5.20", features = ["derive"] }
idmangler-lib = "0.3.0" idmangler-lib = "0.3.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
thiserror = "1.0.68"

43
src/errors/mod.rs Normal file
View file

@ -0,0 +1,43 @@
/// 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,
}

View file

@ -10,9 +10,10 @@ use idmangler_lib::{
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use std::panic; use std::panic;
use std::string::ToString;
mod structures; mod structures;
use crate::structures::*; use crate::structures::*;
mod errors;
use clap::Parser; use clap::Parser;
@ -35,9 +36,9 @@ fn main() {
// fancypanic(); // fancypanic();
let args = Args::parse(); let args = Args::parse();
let mut debugMode = false; let mut debug_mode = false;
if args.debugmode == true { if args.debugmode == true {
debugMode = true; debug_mode = true;
println!("Debug mode enabled"); println!("Debug mode enabled");
}; };
let mut configpath: String = String::from("config.json"); let mut configpath: String = String::from("config.json");
@ -46,11 +47,11 @@ fn main() {
} }
// newest json reading code // newest json reading code
let json_config: jsonconfig = let json_config: Jsonconfig =
serde_json::from_reader(fs::File::open(configpath).expect(ERROR[1])).expect(ERROR[2]); serde_json::from_reader(fs::File::open(configpath).expect(ERROR[1])).expect(ERROR[2]);
let idsmap: HashMap<String, u8> = let idsmap: HashMap<String, u8> =
serde_json::from_reader(fs::File::open("id_keys.json").expect(ERROR[3])).expect(ERROR[4]); serde_json::from_reader(fs::File::open("id_keys.json").expect(ERROR[3])).expect(ERROR[4]);
let json_shiny: Vec<shinystruct> = let json_shiny: Vec<Shinystruct> =
serde_json::from_reader(fs::File::open("shiny_stats.json").expect(ERROR[5])) serde_json::from_reader(fs::File::open("shiny_stats.json").expect(ERROR[5]))
.expect(ERROR[6]); .expect(ERROR[6]);
// println!("{:?}",idsmap.get("airDamage")); // println!("{:?}",idsmap.get("airDamage"));
@ -109,7 +110,7 @@ fn main() {
for _i in 0..powderamount { for _i in 0..powderamount {
powdervec.push((Powders::EARTH, powdertier)) powdervec.push((Powders::EARTH, powdertier))
} }
if debugMode { if debug_mode {
println!("Powder type: Earth"); println!("Powder type: Earth");
} }
} }
@ -117,7 +118,7 @@ fn main() {
for _i in 0..powderamount { for _i in 0..powderamount {
powdervec.push((Powders::THUNDER, powdertier)) powdervec.push((Powders::THUNDER, powdertier))
} }
if debugMode { if debug_mode {
println!("Powder type: Thunder"); println!("Powder type: Thunder");
} }
} }
@ -125,7 +126,7 @@ fn main() {
for _i in 0..powderamount { for _i in 0..powderamount {
powdervec.push((Powders::WATER, powdertier)) powdervec.push((Powders::WATER, powdertier))
} }
if debugMode { if debug_mode {
println!("Powder type: Water"); println!("Powder type: Water");
} }
} }
@ -133,7 +134,7 @@ fn main() {
for _i in 0..powderamount { for _i in 0..powderamount {
powdervec.push((Powders::FIRE, powdertier)) powdervec.push((Powders::FIRE, powdertier))
} }
if debugMode { if debug_mode {
println!("Powder type: Fire"); println!("Powder type: Fire");
} }
} }
@ -141,7 +142,7 @@ fn main() {
for _i in 0..powderamount { for _i in 0..powderamount {
powdervec.push((Powders::AIR, powdertier)) powdervec.push((Powders::AIR, powdertier))
} }
if debugMode { if debug_mode {
println!("Powder type: Air"); println!("Powder type: Air");
} }
} }
@ -149,18 +150,18 @@ fn main() {
for _i in 0..powderamount { for _i in 0..powderamount {
powdervec.push((Powders::THUNDER, powdertier)) powdervec.push((Powders::THUNDER, powdertier))
} }
if debugMode { if debug_mode {
println!("Powder type: Broken, fallback Thunder"); println!("Powder type: Broken, fallback Thunder");
} }
} }
}; };
if debugMode { if debug_mode {
println!("Powder tier: {}", powdertier); println!("Powder tier: {}", powdertier);
println!("Powder amount: {}", powderamount); println!("Powder amount: {}", powderamount);
} }
} }
if debugMode { if debug_mode {
println!("Powders Vec: {:?}", powdervec); println!("Powders Vec: {:?}", powdervec);
} }
@ -176,7 +177,7 @@ fn main() {
Some(i) => { Some(i) => {
if i != 0 { if i != 0 {
RerollData(i).encode(ver, &mut out).unwrap(); RerollData(i).encode(ver, &mut out).unwrap();
if debugMode { if debug_mode {
println!("Rerolls: {}", i) println!("Rerolls: {}", i)
} }
} }
@ -186,21 +187,21 @@ fn main() {
let mut realshinykey: u8; let mut realshinykey: u8;
if let Some(shiny) = json_config.shiny { if let Some(shiny) = json_config.shiny {
if let ref shinykey = shiny.key { if let ref _shinykey = shiny.key {
if let shinyvalue = shiny.value { if let shinyvalue = shiny.value {
realshinykey = 1; realshinykey = 1;
for i in json_shiny { for i in json_shiny {
if i.key == shiny.key { if i.key == shiny.key {
realshinykey = i.id; realshinykey = i.id;
if debugMode { if debug_mode {
println!("shiny key {}", shiny.key); println!("shiny key {}", shiny.key);
} }
} }
} }
if debugMode { if debug_mode {
println!("realshinykey: {}", realshinykey); dbg!(&realshinykey);
println!("shinyvalue: {}", shinyvalue); dbg!(&shinyvalue);
} }
ShinyData { ShinyData {
@ -246,8 +247,7 @@ fn fancypanic() {
"{}", "{}",
panic_msg panic_msg
.lines() .lines()
.skip(1) .nth(1)
.next()
.unwrap_or("HOW DID YOU BREAK THE PANIC HANDLER???") .unwrap_or("HOW DID YOU BREAK THE PANIC HANDLER???")
); );
})); }));

View file

@ -13,10 +13,10 @@ pub struct Identificationer {
pub roll: Option<u8>, pub roll: Option<u8>,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct jsonconfig { pub struct Jsonconfig {
pub debug: Option<bool>, pub debug: Option<bool>,
pub name: String, pub name: String,
pub shiny: Option<shinyjson>, pub shiny: Option<Shinyjson>,
pub ids: Vec<Identificationer>, pub ids: Vec<Identificationer>,
pub powder_limit: u8, pub powder_limit: u8,
pub powders: Vec<Powder>, pub powders: Vec<Powder>,
@ -24,12 +24,12 @@ pub struct jsonconfig {
} }
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct shinystruct { pub struct Shinystruct {
pub id: u8, pub id: u8,
pub key: String, pub key: String,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct shinyjson { pub struct Shinyjson {
pub key: String, pub key: String,
pub value: i64, pub value: i64,
} }