Merge pull request #1 from olesien/master

Added torn -> items
This commit is contained in:
TotallyNot 2024-01-22 20:10:55 +01:00 committed by GitHub
commit 42ffbaa38e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 105 additions and 2 deletions

View file

@ -182,6 +182,22 @@ where
deserializer.deserialize_any(ArrayVisitor(std::marker::PhantomData))
}
pub(crate) fn zero_is_none<'de, D, I>(deserializer: D) -> Result<Option<I>, D::Error>
where
D: Deserializer<'de>,
I: TryFrom<i64>,
{
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::<I>())
})?))
}
}
pub(crate) fn null_is_empty_dict<'de, D, K, V>(deserializer: D) -> Result<HashMap<K, V>, D::Error>
where
D: Deserializer<'de>,

View file

@ -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<i32, Item>", field = "items")]
Items,
}
pub type Selection = TornSelection;
@ -226,6 +229,76 @@ pub struct TerritoryWarReport {
pub factions: HashMap<i32, TerritoryWarReportFaction>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[non_exhaustive]
pub enum ItemType {
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 WeaponType {
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: ItemType,
pub weapon_type: Option<WeaponType>,
#[serde(deserialize_with = "de_util::zero_is_none")]
pub buy_price: Option<u64>,
#[serde(deserialize_with = "de_util::zero_is_none")]
pub sell_price: Option<u64>,
#[serde(deserialize_with = "de_util::zero_is_none")]
pub market_value: Option<u64>,
#[serde(deserialize_with = "de_util::zero_is_none")]
pub circulation: Option<u32>,
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));
}
}