Sucessfully imports data on napkin example

This commit is contained in:
Dandaedre 2025-02-21 05:42:43 +01:00
parent 22b46f6c07
commit 06b8fa7979
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
11 changed files with 58 additions and 45 deletions

1
.ignore Normal file
View File

@ -0,0 +1 @@
*.drawio

Binary file not shown.

View File

@ -26,6 +26,7 @@ fn main() {
// })
// .sum::<i32>();
let f = intermediate_deserialisation.iter().fold((0, 0), |val, res| (val.0 + res.is_ok() as usize, val.1 + 1_usize));
println!("Final data {:#?}", intermediate_deserialisation);
// println!("Final data {:#?}", intermediate_deserialisation);
println!("Failed to parse: {:#?}", intermediate_deserialisation.into_iter().filter_map(|v| v.err()).collect::<Vec<_>>());
println!("Succeded in parsing {}/{}", f.0, f.1);
}

View File

@ -20,10 +20,14 @@ pub enum FatalEquipementError {
#[error("Deserialisation from json failed : {0}")]
SerdeJSON(#[from] serde_json::Error),
#[error("Deserialisation from json failed at line {line} : {error}]")]
#[error(
"Deserialisation from json failed at line {line} on file {file} while trying to parse the value: {parsed_value}: {error}]"
)]
SerdeJSONAt {
line: String,
file: String,
error: serde_json::Error,
parsed_value: String,
},
#[error("Couldn't convert into type {origin_type} with data {data}")]

View File

@ -7,8 +7,6 @@ use std::fmt::Display;
use std::ops::Add;
use strum::Display;
use crate::error::FatalEquipementError;
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
#[cfg_attr(test, derive(PartialEq))]
/// The special category, a vec of string, for display purposes only
@ -36,21 +34,21 @@ pub enum PhysicalDamageType {
Pierce,
}
impl TryFrom<&str> for PhysicalDamageType {
type Error = FatalEquipementError;
// impl TryFrom<&str> for PhysicalDamageType {
// type Error = FatalEquipementError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value.to_ascii_lowercase().as_str() {
"blunt" => Ok(Self::Blunt),
"slash" => Ok(Self::Slash),
"pierce" => Ok(Self::Pierce),
_ => Err(FatalEquipementError::Conversion {
origin_type: std::any::type_name::<Self>().to_owned(),
data: value.to_string(),
}),
}
}
}
// fn try_from(value: &str) -> Result<Self, Self::Error> {
// match value.to_ascii_lowercase().as_str() {
// "blunt" => Ok(Self::Blunt),
// "slash" => Ok(Self::Slash),
// "pierce" => Ok(Self::Pierce),
// _ => Err(FatalEquipementError::Conversion {
// origin_type: std::any::type_name::<Self>().to_owned(),
// data: value.to_string(),
// }),
// }
// }
// }
/// Weapon damage, combining the base weapon damage type and it's level
///
@ -149,7 +147,7 @@ pub enum WeaponCategory {
/// The list of initial weapon meta categories
#[allow(clippy::missing_docs_in_private_items, missing_docs)]
#[derive(Clone, Debug, PartialEq, Display, Deserialize, Serialize, strum::EnumString)]
pub enum WeaponMetaCategory {
pub enum WeaponMacroCategory {
Staves,
Blunt,
Slash,
@ -157,6 +155,9 @@ pub enum WeaponMetaCategory {
Armor,
Shields,
Exotic,
/// A custom none category, used to forbid a part to be cast upon of from
// TODO: Check the soundness of this implementation
None,
}
#[cfg(test)]

View File

@ -42,7 +42,10 @@ impl From<EqNature> for Equipement {
// TODO : Checks that this is exactly desired behavious
// :Also check for Cross Weapon/Armor contamination
// Checks that the nature is compatible with the previous iteration
filter_function: FilterFunction::new(&|sel, lhs| match (&sel.target, &lhs.category) {
filter_function: FilterFunction::new(&|sel, lhs| match (
&sel.target,
&lhs.macro_category,
) {
(Some(a), Some(b)) => a == b,
(None, None) => true,
_ => false,
@ -83,7 +86,7 @@ impl Display for InnerEquipementStats {
self.cost,
self.difficulty,
self.tier,
self.category
self.macro_category
.clone()
.map_or_else(|| "ø".to_string(), |v| v.to_string())
)]);

View File

@ -77,7 +77,9 @@ impl FixedRecipe for EquipementFixedRecipe {
macro_rules! from_serde {
($value: expr) => {
serde_json::from_str(&$value).map_err(|error| FatalEquipementError::SerdeJSONAt {
line: format!("{} line {}", file!(), line!()),
line: line!().to_string(),
file: file!().to_string(),
parsed_value: $value.to_string(),
error,
})
};
@ -112,7 +114,7 @@ impl TryFrom<DatabaseEquipementStats> for InnerEquipementStats {
parry: value.parry,
speed: value.speed,
stealth: value.stealth,
category: from_serde!(&value.category)?,
macro_category: from_serde!(&value.category)?,
target: from_serde!(&value.target)?,
special: from_serde!(&value.special)?,
action_cost: value.action_cost,

View File

@ -53,7 +53,7 @@ mod test {
interfaces::RecipeCollection,
weaponry_structs::{
DamageMod, DamageType, Equipement, InitialEquipementList, SpecialCategory,
equipement_fields::{PhysicalDamageType, WeaponMetaCategory},
equipement_fields::{PhysicalDamageType, WeaponMacroCategory},
equipement_structs::InnerEquipementStats,
},
};
@ -93,8 +93,8 @@ mod test {
rarity: 1,
integrity: 2,
range: 3,
agility_bonus: 4,
category: Some(WeaponMetaCategory::Staves),
agility_bonus: 4.,
macro_category: Some(WeaponMacroCategory::Staves),
special: SpecialCategory(vec!["zero".into()]),
damage_type: Some(DamageType(vec![DamageMod::Secondary(
PhysicalDamageType::Slash,
@ -103,7 +103,7 @@ mod test {
};
let i2 = InnerEquipementStats {
integrity: 3,
agility_bonus: 5,
agility_bonus: 5.,
damage: 10,
damage_type: Some(DamageType(vec![DamageMod::Primary(
PhysicalDamageType::Blunt,
@ -116,10 +116,10 @@ mod test {
InnerEquipementStats {
rarity: 1,
integrity: 5,
agility_bonus: 9,
agility_bonus: 9.,
damage: 10,
range: 3,
category: Some(WeaponMetaCategory::Staves),
macro_category: Some(WeaponMacroCategory::Staves),
special: SpecialCategory(vec!["zero".into(), "one".into(), "two".into()]),
damage_type: Some(DamageType(vec![
DamageMod::Secondary(PhysicalDamageType::Slash,),

View File

@ -55,9 +55,9 @@ impl std::ops::Add for &InnerEquipementStats {
parry: self.parry + rhs.parry,
speed: self.speed + rhs.speed,
stealth: self.stealth + rhs.stealth,
category: self.category.clone().or(rhs.category.clone()), // Can be further optimized
target: self.target.clone().or(rhs.target.clone()), // Can be further optimized
special: self.special.clone() + rhs.special.clone(), // Can be further optimized
macro_category: self.macro_category.clone().or(rhs.macro_category.clone()), // Can be further optimized
target: self.target.clone().or(rhs.target.clone()), // Can be further optimized
special: self.special.clone() + rhs.special.clone(), // Can be further optimized
action_cost: self.action_cost + rhs.action_cost,
reload_cost: self.reload_cost.or(rhs.reload_cost),
range: self.range + rhs.range,

View File

@ -8,7 +8,7 @@ use crate::interfaces::{FilterFunction, FilterFunctionType};
use more_fields::FieldsAsSlice;
use serde::{Deserialize, Serialize};
use super::{DamageType, SpecialCategory, WeaponCategory, equipement_fields::WeaponMetaCategory};
use super::{DamageType, SpecialCategory, WeaponCategory, equipement_fields::WeaponMacroCategory};
/// The final equipement list
///
@ -75,17 +75,18 @@ pub struct InnerEquipementStats {
pub(super) integrity: i8,
pub(super) torque_two_handed: Option<i8>, // Might be option (single claw)
pub(super) torque_one_handed: Option<i8>, // Should be option (bows)
pub(super) dodge: i8,
pub(super) dodge: f32, // I could also use a custom HalfValue
pub(super) parry: i8,
pub(super) speed: i8,
pub(super) stealth: i8,
pub(super) category: Option<WeaponMetaCategory>,
pub(super) target: Option<WeaponMetaCategory>, // TODO: This might be incorrect, to check
/// The initial macro category, in turn combined with other stats it will describe a final category
pub(super) macro_category: Option<WeaponMacroCategory>, // TODO : This initial macro category fill as a final stat feed the final category
pub(super) target: Option<WeaponMacroCategory>, // TODO: This might be incorrect, to check
pub(super) special: SpecialCategory,
pub(super) action_cost: i8,
pub(super) reload_cost: Option<i8>, // Shouldn't it be Option ?
pub(super) range: i8, // Shoudln't it be Option ?
pub(super) agility_bonus: i8,
pub(super) agility_bonus: f32, // Also convert this one to HalfValue
}
// TODO: This implementation of default is painfull
@ -129,8 +130,8 @@ pub struct DatabaseEquipementStats {
pub integrity: i8,
pub torque_two_handed: String,
pub torque_one_handed: String,
#[INTEGER]
pub dodge: i8,
#[REAL]
pub dodge: f32,
#[INTEGER]
pub parry: i8,
#[INTEGER]
@ -145,8 +146,8 @@ pub struct DatabaseEquipementStats {
pub reload_cost: String,
#[INTEGER]
pub range: i8,
#[INTEGER]
pub agility_bonus: i8,
#[REAL]
pub agility_bonus: f32,
}
impl Default for DatabaseEquipementStats {
@ -156,7 +157,7 @@ impl Default for DatabaseEquipementStats {
meta_category: "EqShape".to_string(),
name: String::new(),
damage_type: serde_json::to_string(&None::<DamageType>).unwrap(),
category: serde_json::to_string(&None::<WeaponMetaCategory>).unwrap(),
category: serde_json::to_string(&None::<WeaponMacroCategory>).unwrap(),
target: serde_json::to_string(&None::<WeaponCategory>).unwrap(),
special: serde_json::to_string(&SpecialCategory::default()).unwrap(),
reload_cost: serde_json::to_string(&None::<u8>).unwrap(),
@ -175,12 +176,12 @@ impl Default for DatabaseEquipementStats {
integrity: 0,
torque_two_handed: serde_json::to_string(&0).unwrap(),
torque_one_handed: serde_json::to_string(&None::<u8>).unwrap(),
dodge: 0,
dodge: 0.,
parry: 0,
speed: 0,
stealth: 0,
range: 0,
agility_bonus: 0,
agility_bonus: 0.,
action_cost: 0,
}
}

View File

@ -35,5 +35,5 @@ pub use equipement_structs::{Equipement, InitialEquipementList};
// #[allow(unused_imports)]
pub use equipement_fields::PhysicalDamageType;
pub use equipement_fields::{
DamageMod, DamageType, NatureTarget, SpecialCategory, WeaponCategory, WeaponMetaCategory,
DamageMod, DamageType, NatureTarget, SpecialCategory, WeaponCategory, WeaponMacroCategory,
};