diff --git a/Cargo.lock b/Cargo.lock index 5d0fc35..5fa0da6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,6 +127,7 @@ dependencies = [ "idmangler-lib", "serde", "serde_json", + "thiserror", ] [[package]] @@ -211,9 +212,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.77" +version = "2.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" dependencies = [ "proc-macro2", "quote", @@ -222,18 +223,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.63" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +checksum = "02dd99dc800bbb97186339685293e1cc5d9df1f8fae2d0aecd9ff1c77efea892" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.63" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +checksum = "a7c61ec9a6f64d2793d8a45faba21efbe3ced62a886d44c36a009b2b519b4c7e" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 6ab5a27..2602f46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ clap = { version = "4.5.20", features = ["derive"] } idmangler-lib = "0.3.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +thiserror = "1.0.68" diff --git a/src/errors/mod.rs b/src/errors/mod.rs new file mode 100644 index 0000000..9bbab94 --- /dev/null +++ b/src/errors/mod.rs @@ -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, +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f47d4de..febff26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,9 +10,10 @@ use idmangler_lib::{ use std::collections::HashMap; use std::fs; use std::panic; -use std::string::ToString; mod structures; use crate::structures::*; +mod errors; + use clap::Parser; @@ -35,9 +36,9 @@ fn main() { // fancypanic(); let args = Args::parse(); - let mut debugMode = false; + let mut debug_mode = false; if args.debugmode == true { - debugMode = true; + debug_mode = true; println!("Debug mode enabled"); }; let mut configpath: String = String::from("config.json"); @@ -46,11 +47,11 @@ fn main() { } // 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]); let idsmap: HashMap = serde_json::from_reader(fs::File::open("id_keys.json").expect(ERROR[3])).expect(ERROR[4]); - let json_shiny: Vec = + let json_shiny: Vec = serde_json::from_reader(fs::File::open("shiny_stats.json").expect(ERROR[5])) .expect(ERROR[6]); // println!("{:?}",idsmap.get("airDamage")); @@ -109,7 +110,7 @@ fn main() { for _i in 0..powderamount { powdervec.push((Powders::EARTH, powdertier)) } - if debugMode { + if debug_mode { println!("Powder type: Earth"); } } @@ -117,7 +118,7 @@ fn main() { for _i in 0..powderamount { powdervec.push((Powders::THUNDER, powdertier)) } - if debugMode { + if debug_mode { println!("Powder type: Thunder"); } } @@ -125,7 +126,7 @@ fn main() { for _i in 0..powderamount { powdervec.push((Powders::WATER, powdertier)) } - if debugMode { + if debug_mode { println!("Powder type: Water"); } } @@ -133,7 +134,7 @@ fn main() { for _i in 0..powderamount { powdervec.push((Powders::FIRE, powdertier)) } - if debugMode { + if debug_mode { println!("Powder type: Fire"); } } @@ -141,7 +142,7 @@ fn main() { for _i in 0..powderamount { powdervec.push((Powders::AIR, powdertier)) } - if debugMode { + if debug_mode { println!("Powder type: Air"); } } @@ -149,18 +150,18 @@ fn main() { for _i in 0..powderamount { powdervec.push((Powders::THUNDER, powdertier)) } - if debugMode { + if debug_mode { println!("Powder type: Broken, fallback Thunder"); } } }; - if debugMode { + if debug_mode { println!("Powder tier: {}", powdertier); println!("Powder amount: {}", powderamount); } } - if debugMode { + if debug_mode { println!("Powders Vec: {:?}", powdervec); } @@ -176,7 +177,7 @@ fn main() { Some(i) => { if i != 0 { RerollData(i).encode(ver, &mut out).unwrap(); - if debugMode { + if debug_mode { println!("Rerolls: {}", i) } } @@ -186,21 +187,21 @@ fn main() { let mut realshinykey: u8; if let Some(shiny) = json_config.shiny { - if let ref shinykey = shiny.key { + if let ref _shinykey = shiny.key { if let shinyvalue = shiny.value { realshinykey = 1; for i in json_shiny { if i.key == shiny.key { realshinykey = i.id; - if debugMode { + if debug_mode { println!("shiny key {}", shiny.key); } } } - if debugMode { - println!("realshinykey: {}", realshinykey); - println!("shinyvalue: {}", shinyvalue); + if debug_mode { + dbg!(&realshinykey); + dbg!(&shinyvalue); } ShinyData { @@ -246,8 +247,7 @@ fn fancypanic() { "{}", panic_msg .lines() - .skip(1) - .next() + .nth(1) .unwrap_or("HOW DID YOU BREAK THE PANIC HANDLER???") ); })); diff --git a/src/structures/mod.rs b/src/structures/mod.rs index c48e9e4..5559449 100644 --- a/src/structures/mod.rs +++ b/src/structures/mod.rs @@ -13,10 +13,10 @@ pub struct Identificationer { pub roll: Option, } #[derive(Deserialize)] -pub struct jsonconfig { +pub struct Jsonconfig { pub debug: Option, pub name: String, - pub shiny: Option, + pub shiny: Option, pub ids: Vec, pub powder_limit: u8, pub powders: Vec, @@ -24,12 +24,12 @@ pub struct jsonconfig { } #[derive(Deserialize)] -pub struct shinystruct { +pub struct Shinystruct { pub id: u8, pub key: String, } #[derive(Deserialize)] -pub struct shinyjson { +pub struct Shinyjson { pub key: String, pub value: i64, }