added territory coordinates

This commit is contained in:
TotallyNot 2023-02-21 16:00:55 +01:00
parent 3757d1778f
commit 71eef676d3
3 changed files with 50 additions and 2 deletions

View file

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

View file

@ -47,4 +47,12 @@ pub struct Territory {
pub density: i16,
pub daily_respect: i16,
pub faction: i32,
#[cfg(feature = "decimal")]
#[serde(deserialize_with = "de_util::string_or_decimal")]
pub coordinate_x: rust_decimal::Decimal,
#[cfg(feature = "decimal")]
#[serde(deserialize_with = "de_util::string_or_decimal")]
pub coordinate_y: rust_decimal::Decimal,
}

View file

@ -37,7 +37,8 @@ where
if i == 0 {
Ok(None)
} else {
let naive = NaiveDateTime::from_timestamp(i, 0);
let naive = NaiveDateTime::from_timestamp_opt(i, 0)
.ok_or_else(|| D::Error::invalid_value(Unexpected::Signed(i), &"Epoch timestamp"))?;
Ok(Some(DateTime::from_utc(naive, Utc)))
}
}
@ -90,3 +91,42 @@ where
deserializer.deserialize_any(DumbVisitor)
}
#[cfg(feature = "decimal")]
pub(crate) fn string_or_decimal<'de, D>(deserializer: D) -> Result<rust_decimal::Decimal, D::Error>
where
D: Deserializer<'de>,
{
struct DumbVisitor;
impl<'de> Visitor<'de> for DumbVisitor {
type Value = rust_decimal::Decimal;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "integer or float as string")
}
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: Error,
{
Ok(v.into())
}
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: Error,
{
Ok(v.into())
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
rust_decimal::Decimal::from_str_exact(v).map_err(E::custom)
}
}
deserializer.deserialize_any(DumbVisitor)
}