fixed territory wars in faction->basic

This commit is contained in:
TotallyNot 2023-03-11 18:16:49 +01:00
parent acb8cbbdff
commit 4583b88366
3 changed files with 49 additions and 2 deletions

View file

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

View file

@ -135,6 +135,53 @@ where
deserializer.deserialize_map(MapVisitor) deserializer.deserialize_map(MapVisitor)
} }
pub(crate) fn empty_dict_is_empty_array<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
struct ArrayVisitor<T>(std::marker::PhantomData<T>);
impl<'de, T> Visitor<'de> for ArrayVisitor<T>
where
T: Deserialize<'de>,
{
type Value = Vec<T>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "vec or empty object")
}
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
match map.size_hint() {
Some(0) | None => Ok(Vec::default()),
Some(len) => Err(A::Error::invalid_length(len, &"empty dict")),
}
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut result = match seq.size_hint() {
Some(len) => Vec::with_capacity(len),
None => Vec::default(),
};
while let Some(element) = seq.next_element()? {
result.push(element);
}
Ok(result)
}
}
deserializer.deserialize_any(ArrayVisitor(std::marker::PhantomData))
}
#[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

@ -68,7 +68,7 @@ pub struct Basic<'a> {
#[serde(deserialize_with = "de_util::datetime_map")] #[serde(deserialize_with = "de_util::datetime_map")]
pub peace: BTreeMap<i32, DateTime<Utc>>, pub peace: BTreeMap<i32, DateTime<Utc>>,
#[serde(borrow)] #[serde(borrow, deserialize_with = "de_util::empty_dict_is_empty_array")]
pub territory_wars: Vec<FactionTerritoryWar<'a>>, pub territory_wars: Vec<FactionTerritoryWar<'a>>,
} }