From d6232fd38004226fa6f2f69b96929bed2523bf69 Mon Sep 17 00:00:00 2001 From: olesien Date: Mon, 22 Jan 2024 20:04:22 +0100 Subject: [PATCH 1/2] Added torn -> items Also added unit test to check. --- torn-api/src/de_util.rs | 16 ++++++++ torn-api/src/torn.rs | 91 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 2 deletions(-) diff --git a/torn-api/src/de_util.rs b/torn-api/src/de_util.rs index 0bf658c..073ffc5 100644 --- a/torn-api/src/de_util.rs +++ b/torn-api/src/de_util.rs @@ -182,6 +182,22 @@ where deserializer.deserialize_any(ArrayVisitor(std::marker::PhantomData)) } +pub(crate) fn zero_is_none<'de, D, I>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, + I: TryFrom, +{ + let num = i64::deserialize(deserializer)?; + + if num == 0 { + Ok(None) + } else { + Ok(Some(num.try_into().map_err(|_| { + D::Error::invalid_value(Unexpected::Signed(num), &std::any::type_name::()) + })?)) + } +} + pub(crate) fn null_is_empty_dict<'de, D, K, V>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, diff --git a/torn-api/src/torn.rs b/torn-api/src/torn.rs index aa9137a..05923ef 100644 --- a/torn-api/src/torn.rs +++ b/torn-api/src/torn.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use chrono::{DateTime, Utc}; use serde::{ @@ -8,7 +8,7 @@ use serde::{ use torn_api_macros::ApiCategory; -use crate::user; +use crate::{de_util, user}; #[derive(Debug, Clone, Copy, ApiCategory)] #[api(category = "torn")] @@ -40,6 +40,9 @@ pub enum TornSelection { #[api(type = "TerritoryWarReport", field = "territorywarreport")] TerritoryWarReport, + + #[api(type = "BTreeMap", field = "items")] + Items, } pub type Selection = TornSelection; @@ -226,6 +229,76 @@ pub struct TerritoryWarReport { pub factions: HashMap, } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] +#[non_exhaustive] +pub enum ItemTypes { + Primary, + Secondary, + Melee, + Temporary, + Defensive, + Collectible, + Medical, + Drug, + Booster, + #[serde(rename = "Energy Drink")] + EnergyDrink, + Alcohol, + Book, + Candy, + Car, + Clothing, + Electronic, + Enhancer, + Flower, + Jewelry, + Other, + Special, + #[serde(rename = "Supply Pack")] + SupplyPack, + Virus, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] +#[non_exhaustive] +//Missing hand to hand because it is not possible as a weapon +pub enum WeaponTypes { + Slashing, + Rifle, + SMG, + Piercing, + Clubbing, + Pistol, + #[serde(rename = "Machine gun")] + MachineGun, + Mechanical, + Temporary, + Heavy, + Shotgun, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct Item<'a> { + pub name: String, + pub description: String, + #[serde(deserialize_with = "de_util::empty_string_is_none")] + pub effect: Option<&'a str>, + #[serde(deserialize_with = "de_util::empty_string_is_none")] + pub requirement: Option<&'a str>, + #[serde(rename = "type")] + pub item_type: ItemTypes, + pub weapon_type: Option, + #[serde(deserialize_with = "de_util::zero_is_none")] + pub buy_price: Option, + #[serde(deserialize_with = "de_util::zero_is_none")] + pub sell_price: Option, + #[serde(deserialize_with = "de_util::zero_is_none")] + pub market_value: Option, + #[serde(deserialize_with = "de_util::zero_is_none")] + pub circulation: Option, + pub image: String, +} + #[cfg(test)] mod tests { use super::*; @@ -327,4 +400,18 @@ mod tests { TerritoryWarOutcome::EndWithDestroyDefense ); } + + #[async_test] + async fn item() { + let key = setup(); + + let response = Client::default() + .torn_api(key) + .torn(|b| b.selections([Selection::Items]).id(837)) + .await + .unwrap(); + + let item_list = response.items().unwrap(); + assert!(item_list.contains_key(&837)); + } } From c9ead5e7d8f67d910a8a7a6589379552d1dfe8f4 Mon Sep 17 00:00:00 2001 From: olesien Date: Mon, 22 Jan 2024 20:09:37 +0100 Subject: [PATCH 2/2] Fixed naming conventions --- torn-api/src/torn.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/torn-api/src/torn.rs b/torn-api/src/torn.rs index 05923ef..4398a7e 100644 --- a/torn-api/src/torn.rs +++ b/torn-api/src/torn.rs @@ -231,7 +231,7 @@ pub struct TerritoryWarReport { #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] #[non_exhaustive] -pub enum ItemTypes { +pub enum ItemType { Primary, Secondary, Melee, @@ -262,7 +262,7 @@ pub enum ItemTypes { #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] #[non_exhaustive] //Missing hand to hand because it is not possible as a weapon -pub enum WeaponTypes { +pub enum WeaponType { Slashing, Rifle, SMG, @@ -286,8 +286,8 @@ pub struct Item<'a> { #[serde(deserialize_with = "de_util::empty_string_is_none")] pub requirement: Option<&'a str>, #[serde(rename = "type")] - pub item_type: ItemTypes, - pub weapon_type: Option, + pub item_type: ItemType, + pub weapon_type: Option, #[serde(deserialize_with = "de_util::zero_is_none")] pub buy_price: Option, #[serde(deserialize_with = "de_util::zero_is_none")]