1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

serialize SendPropIndentifier as string to not run into js number rounding

This commit is contained in:
Robin Appelman 2022-09-03 17:40:54 +02:00
commit fefda77884
3 changed files with 37 additions and 4 deletions

View file

@ -12,7 +12,8 @@ use bitbuffer::{
use enumflags2::{bitflags, BitFlags}; use enumflags2::{bitflags, BitFlags};
use num_traits::Signed; use num_traits::Signed;
use parse_display::Display; use parse_display::Display;
use serde::{Deserialize, Serialize}; use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::borrow::Cow; use std::borrow::Cow;
use std::cmp::min; use std::cmp::min;
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
@ -1116,7 +1117,7 @@ impl<'a> TryFrom<&'a SendPropValue> for &'a [SendPropValue] {
} }
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct SendPropIdentifier(u64); pub struct SendPropIdentifier(u64);
impl SendPropIdentifier { impl SendPropIdentifier {
@ -1168,6 +1169,38 @@ impl Display for SendPropIdentifier {
} }
} }
impl<'de> Deserialize<'de> for SendPropIdentifier {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum Options<'a> {
Num(u64),
Str(&'a str),
}
let raw = Options::deserialize(deserializer)?;
Ok(match raw {
Options::Num(num) => SendPropIdentifier(num),
Options::Str(s) => {
let num: u64 = s.parse().map_err(|e| D::Error::custom(e))?;
SendPropIdentifier(num)
}
})
}
}
impl Serialize for SendPropIdentifier {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.to_string().serialize(serializer)
}
}
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Display, PartialEq, Serialize, Deserialize)] #[derive(Clone, Display, PartialEq, Serialize, Deserialize)]
#[display("{index} = {value}")] #[display("{index} = {value}")]

View file

@ -277,7 +277,7 @@
"charge": 0 "charge": 0
} }
], ],
"buildings": [], "buildings": {},
"world": { "world": {
"boundary_min": { "boundary_min": {
"x": -4523.0, "x": -4523.0,

View file

@ -24,7 +24,7 @@
"charge": 0 "charge": 0
} }
], ],
"buildings": [], "buildings": {},
"world": { "world": {
"boundary_min": { "boundary_min": {
"x": -3882.0, "x": -3882.0,