diff --git a/src/encode.rs b/src/encode.rs index 31d49f8..aa1f38f 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -1,8 +1,15 @@ -use crate::jsonstruct::{CraftedTypesFr, Durability, FuncParams, Identificationer, ItemTypeDeser, Powder, RequirementsDeser, Shinyjson, Shinystruct}; -use idmangler_lib::types::{ClassType, Element, ItemType, RollType, SkillType, Stat}; -use idmangler_lib::{CustomGearTypeData, CustomConsumableTypeData, DataEncoder, EndData, IdentificationData, NameData, PowderData, RequirementsData, RerollData, ShinyData, StartData, TypeData, DurabilityData}; -use std::collections::HashMap; use crate::errorfr::Errorfr; +use crate::jsonstruct::{ + CraftedTypesFr, Durability, FuncParams, Identificationer, ItemTypeDeser, Powder, + RequirementsDeser, Shinyjson, Shinystruct, +}; +use idmangler_lib::types::{ClassType, Element, ItemType, RollType, SkillType, Stat}; +use idmangler_lib::{ + CustomConsumableTypeData, CustomGearTypeData, DataEncoder, DurabilityData, EndData, + IdentificationData, NameData, PowderData, RequirementsData, RerollData, ShinyData, StartData, + TypeData, +}; +use std::collections::HashMap; pub fn encode_startdata(general_params: &mut FuncParams) { if *general_params.fr_debug_mode { @@ -22,7 +29,10 @@ pub fn encode_typedata(general_params: &mut FuncParams, item_type_deser: ItemTyp .encode(general_params.fr_ver, general_params.fr_out) .unwrap(); } -pub fn encode_typedata_custom(general_params: &mut FuncParams, crafted_type: &str) -> Result<(), Errorfr> { +pub fn encode_typedata_custom( + general_params: &mut FuncParams, + crafted_type: &str, +) -> Result<(), Errorfr> { let frfr_type = CraftedTypesFr::try_from(crafted_type)?; match frfr_type { CraftedTypesFr::Gear(a) => { @@ -32,7 +42,7 @@ pub fn encode_typedata_custom(general_params: &mut FuncParams, crafted_type: &st CustomGearTypeData(a) .encode(general_params.fr_ver, general_params.fr_out) .unwrap() - }, + } CraftedTypesFr::Consu(a) => { if *general_params.fr_debug_mode { println!("Encoding CustomTypeData: Consumable"); @@ -44,23 +54,26 @@ pub fn encode_typedata_custom(general_params: &mut FuncParams, crafted_type: &st } Ok(()) } -pub fn encode_duradata(general_params: &mut FuncParams, real_dura: Durability) -> Result<(), Errorfr> { +pub fn encode_duradata( + general_params: &mut FuncParams, + real_dura: Durability, +) -> Result<(), Errorfr> { let effect_strength_fr: u8; // but actually it should be 0 to 100, not 0 to 255. But i dunno how to use u7 data type. if let Some(effstr) = real_dura.effect_strength { if *general_params.fr_debug_mode { println!("Encoding DurabilityData: Defined"); } effect_strength_fr = effstr - } - else { + } else { let current_percentage = real_dura.dura_cur / real_dura.dura_max; // percentage of max durability if current_percentage > 100 { - return Err(Errorfr::JsonDuraOutOfRange) + return Err(Errorfr::JsonDuraOutOfRange); } - if current_percentage >= 50 { // for 100% dura to 50% dura, the effectiveness is 100% + if current_percentage >= 50 { + // for 100% dura to 50% dura, the effectiveness is 100% effect_strength_fr = 100 - } - else if current_percentage >= 10 { // for 50% dura to 10% dura, the effectiveness is 100% to 50% + } else if current_percentage >= 10 { + // for 50% dura to 10% dura, the effectiveness is 100% to 50% // see this answer from Stackoverflow for transcribing range // https://stackoverflow.com/a/929107 @@ -70,36 +83,41 @@ pub fn encode_duradata(general_params: &mut FuncParams, real_dura: Durability) - let new_range = 50; // NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin effect_strength_fr = ((((current_percentage - 10) * new_range) / old_range) + 50) as u8 - } - else if current_percentage >= 0 { // for 10% dura to 0% dura, the effectiveness is 50% to 10% + } else if current_percentage >= 0 { + // for 10% dura to 0% dura, the effectiveness is 50% to 10% // old range is 10-0 = 10 let old_range = 10; // new range is 50-10 = 40 let new_range = 40; // NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin effect_strength_fr = ((((current_percentage) * new_range) / old_range) + 10) as u8 - } - else { - return Err(Errorfr::JsonDuraOutOfRange) + } else { + return Err(Errorfr::JsonDuraOutOfRange); } } if *general_params.fr_debug_mode { - println!("Encoding DurabilityData.effect_strenght: {}", effect_strength_fr); + println!( + "Encoding DurabilityData.effect_strenght: {}", + effect_strength_fr + ); println!("Encoding DurabilityData.current: {}", real_dura.dura_cur); println!("Encoding DurabilityData.current: {}", real_dura.dura_max); } DurabilityData { effect_strenght: effect_strength_fr, current: real_dura.dura_cur, - max: real_dura.dura_max + max: real_dura.dura_max, } - .encode(general_params.fr_ver, general_params.fr_out) - .unwrap(); + .encode(general_params.fr_ver, general_params.fr_out) + .unwrap(); Ok(()) } pub fn encode_reqdata(general_params: &mut FuncParams, real_reqdata: RequirementsDeser) { if *general_params.fr_debug_mode { - println!("Encoding RequirementData.Level: {:?}", real_reqdata.level.clone()) + println!( + "Encoding RequirementData.Level: {:?}", + real_reqdata.level.clone() + ) } let mut fr_class: Option = None; if let Some(actualclass) = real_reqdata.class { @@ -107,8 +125,7 @@ pub fn encode_reqdata(general_params: &mut FuncParams, real_reqdata: Requirement if *general_params.fr_debug_mode { println!("Encoding RequirementData.Class: {:?}", actualclass.clone()) } - } - else if *general_params.fr_debug_mode { + } else if *general_params.fr_debug_mode { println!("Encoding RequirementData.Class: Undefined"); } let spvec: Vec<(SkillType, i32)> = Vec::<(SkillType, i32)>::from(real_reqdata.sp); @@ -118,10 +135,10 @@ pub fn encode_reqdata(general_params: &mut FuncParams, real_reqdata: Requirement RequirementsData { level: real_reqdata.level, class: fr_class, - skills: spvec + skills: spvec, } - .encode(general_params.fr_ver, general_params.fr_out) - .unwrap() + .encode(general_params.fr_ver, general_params.fr_out) + .unwrap() } pub fn encode_namedata(general_params: &mut FuncParams, real_name: &str) { // ENCODE: NameData @@ -132,7 +149,11 @@ pub fn encode_namedata(general_params: &mut FuncParams, real_name: &str) { .encode(general_params.fr_ver, general_params.fr_out) .unwrap(); } -pub fn encode_iddata(general_params: &mut FuncParams, real_ids: Vec, idsmap: HashMap) { +pub fn encode_iddata( + general_params: &mut FuncParams, + real_ids: Vec, + idsmap: HashMap, +) { let mut idvec = Vec::new(); for eachid in real_ids { let id_id = idsmap.get(eachid.id.trim()); @@ -213,7 +234,11 @@ pub fn encode_rerolldata(general_params: &mut FuncParams, rerollcount: u8) { } } } -pub fn encode_shinydata(general_params: &mut FuncParams, shiny: Shinyjson, json_shiny: Vec) { +pub fn encode_shinydata( + general_params: &mut FuncParams, + shiny: Shinyjson, + json_shiny: Vec, +) { let mut realshinykey: u8; let _shinykey = &shiny.key; let shinyvalue = shiny.value; diff --git a/src/errorfr.rs b/src/errorfr.rs index 02b165f..91216a7 100644 --- a/src/errorfr.rs +++ b/src/errorfr.rs @@ -64,8 +64,10 @@ pub enum Errorfr { /// Durability was not found but is necessary #[error("Error 4.5: \"Durability\" was not found (necessary for Crafted Gear item type)")] JsonNotFoundDura, - + /// Durability Bad - #[error("Error 4.6: \"Requirements\" was not found (necessary for Crafted Gear / Consumable items)")] - JsonNotFoundReqs + #[error( + "Error 4.6: \"Requirements\" was not found (necessary for Crafted Gear / Consumable items)" + )] + JsonNotFoundReqs, } diff --git a/src/jsonstruct.rs b/src/jsonstruct.rs index ed1b44a..3b6bdfc 100644 --- a/src/jsonstruct.rs +++ b/src/jsonstruct.rs @@ -1,13 +1,12 @@ use crate::errorfr::Errorfr; -use idmangler_lib::types::{ItemType, TransformVersion, ConsumableType, GearType}; +use crate::jsonstruct::CraftedTypesFr::{Consu, Gear}; +use idmangler_lib::types::{ClassType, ConsumableType::*, GearType::*, SkillType}; +use idmangler_lib::types::{ConsumableType, GearType, ItemType, TransformVersion}; use serde::Deserialize; use std::fs; -use idmangler_lib::types::{ConsumableType::*,GearType::*, ClassType, SkillType}; -use crate::jsonstruct::CraftedTypesFr::{Consu, Gear}; // structs for the json parsing -#[derive(Deserialize)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct Jsonconfig { pub debug: Option, // not a thing to be encoded, this just toggles debug prints. Also settable using --debug // Item Types (Gear, Tome, Charm, Crafted Gear, Crafted Consum) @@ -27,26 +26,22 @@ pub struct Jsonconfig { pub powders: Option>, pub rerolls: Option, } -// reimplementing this because it doesnt have Deserialize. +// reimplementing this because it doesnt have Deserialize. // Also, changing the SkillPoint stuff into NOT a vec. // This avoids confusing end user. -#[derive(Deserialize)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -#[derive(Clone, Copy)] +#[derive(Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)] pub struct RequirementsDeser { pub level: u8, pub class: Option, - pub sp: SkillPointDeser + pub sp: SkillPointDeser, } -#[derive(Deserialize)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -#[derive(Clone, Copy)] +#[derive(Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)] pub enum ClassDeser { Archer, Warrior, Assassin, Mage, - Shaman + Shaman, } impl From for ClassType { fn from(value: ClassDeser) -> Self { @@ -55,13 +50,11 @@ impl From for ClassType { ClassDeser::Warrior => ClassType::Warrior, ClassDeser::Assassin => ClassType::Assasin, ClassDeser::Mage => ClassType::Mage, - ClassDeser::Shaman => ClassType::Shaman + ClassDeser::Shaman => ClassType::Shaman, } } } -#[derive(Deserialize, Copy)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -#[derive(Clone)] +#[derive(Deserialize, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone)] pub struct SkillPointDeser { #[serde(alias = "Str")] #[serde(alias = "str")] @@ -82,7 +75,7 @@ pub struct SkillPointDeser { #[serde(alias = "Agi")] #[serde(alias = "agi")] #[serde(alias = "agility")] - pub agility: Option + pub agility: Option, } impl From for Vec<(SkillType, i32)> { @@ -109,7 +102,7 @@ impl From for Vec<(SkillType, i32)> { #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub enum CraftedTypesFr { Gear(GearType), - Consu(ConsumableType) + Consu(ConsumableType), } impl TryFrom<&str> for CraftedTypesFr { type Error = Errorfr; @@ -139,39 +132,34 @@ impl TryFrom<&str> for CraftedTypesFr { "weapon" => Ok(Gear(Weapon)), "accessory" => Ok(Gear(Accessory)), // fallback error return - _ => Err(Errorfr::JsonInvalidCraftedType) + _ => Err(Errorfr::JsonInvalidCraftedType), } } } -#[derive(Deserialize)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct Durability { pub effect_strength: Option, pub dura_cur: i32, - pub dura_max: i32 + pub dura_max: i32, } -#[derive(Deserialize)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct Shinystruct { pub id: u8, pub key: String, } -#[derive(Deserialize)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct Identificationer { pub id: String, pub base: i32, pub roll: Option, } -#[derive(Deserialize)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct Powder { pub r#type: char, pub amount: Option, } -#[derive(Deserialize)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct Shinyjson { pub key: String, pub value: i64, diff --git a/src/main.rs b/src/main.rs index 692158c..a4d6dcc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,9 +13,9 @@ use crate::errorfr::Errorfr; use crate::jsondl::*; use crate::jsonstruct::*; +use crate::errorfr::Errorfr::JsonNotFoundCraftedType; use clap::Parser; use reqwest::Url; -use crate::errorfr::Errorfr::JsonNotFoundCraftedType; #[derive(Parser, Debug, Clone)] #[command(version, about, long_about = None, arg_required_else_help(true))] @@ -130,9 +130,8 @@ fn cook( ItemTypeDeser::CraftedGear | ItemTypeDeser::CraftedConsu => { if let Some(real_crafted_type) = json_config.crafted_type { encode_typedata_custom(&mut fr_params, &real_crafted_type)?; - } - else { - return Err(JsonNotFoundCraftedType) + } else { + return Err(JsonNotFoundCraftedType); } } _ => {} @@ -165,11 +164,10 @@ fn cook( ItemTypeDeser::CraftedGear => { if let Some(real_dura) = json_config.durability { encode_duradata(&mut fr_params, real_dura)?; + } else { + return Err(Errorfr::JsonNotFoundDura); } - else { - return Err(Errorfr::JsonNotFoundDura) - } - }, + } _ => {} } @@ -178,11 +176,10 @@ fn cook( ItemTypeDeser::CraftedGear | ItemTypeDeser::CraftedConsu => { if let Some(real_reqs) = json_config.requirements { encode_reqdata(&mut fr_params, real_reqs) + } else { + return Err(Errorfr::JsonNotFoundReqs); } - else { - return Err(Errorfr::JsonNotFoundReqs) - } - }, + } _ => {} }