fixed faction->territory for sectored factions

This commit is contained in:
TotallyNot 2023-03-11 19:21:08 +01:00
parent 4583b88366
commit 6b4189ab2d
4 changed files with 36 additions and 5 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/target /target
/Cargo.lock /Cargo.lock
.env .env
.DS_Store

View file

@ -1,6 +1,6 @@
[package] [package]
name = "torn-api" name = "torn-api"
version = "0.5.14" version = "0.5.15"
edition = "2021" edition = "2021"
authors = ["Pyrit [2111649]"] authors = ["Pyrit [2111649]"]
license = "MIT" license = "MIT"

View file

@ -1,8 +1,8 @@
#![allow(unused)] #![allow(unused)]
use std::collections::BTreeMap; use std::collections::{BTreeMap, HashMap};
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{serde::ts_nanoseconds::deserialize, DateTime, NaiveDateTime, Utc};
use serde::de::{Deserialize, Deserializer, Error, Unexpected, Visitor}; use serde::de::{Deserialize, Deserializer, Error, Unexpected, Visitor};
pub(crate) fn empty_string_is_none<'de, D>(deserializer: D) -> Result<Option<&'de str>, D::Error> pub(crate) fn empty_string_is_none<'de, D>(deserializer: D) -> Result<Option<&'de str>, D::Error>
@ -182,6 +182,15 @@ where
deserializer.deserialize_any(ArrayVisitor(std::marker::PhantomData)) deserializer.deserialize_any(ArrayVisitor(std::marker::PhantomData))
} }
pub(crate) fn null_is_empty_dict<'de, D, K, V>(deserializer: D) -> Result<HashMap<K, V>, D::Error>
where
D: Deserializer<'de>,
K: std::hash::Hash + std::cmp::Eq + Deserialize<'de>,
V: Deserialize<'de>,
{
Ok(Option::deserialize(deserializer)?.unwrap_or_default())
}
#[cfg(feature = "decimal")] #[cfg(feature = "decimal")]
pub(crate) fn string_or_decimal<'de, D>(deserializer: D) -> Result<rust_decimal::Decimal, D::Error> pub(crate) fn string_or_decimal<'de, D>(deserializer: D) -> Result<rust_decimal::Decimal, D::Error>
where where

View file

@ -5,7 +5,7 @@ use serde::Deserialize;
use torn_api_macros::ApiCategory; use torn_api_macros::ApiCategory;
use crate::de_util; use crate::de_util::{self, null_is_empty_dict};
pub use crate::common::{LastAction, Status, Territory}; pub use crate::common::{LastAction, Status, Territory};
@ -21,7 +21,11 @@ pub enum Selection {
#[api(type = "BTreeMap<i32, AttackFull>", field = "attacks")] #[api(type = "BTreeMap<i32, AttackFull>", field = "attacks")]
Attacks, Attacks,
#[api(type = "HashMap<String, Territory>", field = "territory")] #[api(
type = "HashMap<String, Territory>",
field = "territory",
with = "null_is_empty_dict"
)]
Territory, Territory,
} }
@ -199,4 +203,21 @@ mod tests {
response.attacks_full().unwrap(); response.attacks_full().unwrap();
response.territory().unwrap(); response.territory().unwrap();
} }
#[async_test]
async fn destroyed_faction() {
let key = setup();
let response = Client::default()
.torn_api(key)
.faction(|b| {
b.id(8981)
.selections(&[Selection::Basic, Selection::Territory])
})
.await
.unwrap();
response.basic().unwrap();
response.territory().unwrap();
}
} }