fix: add hex parsing
This commit is contained in:
parent
6090b2b47f
commit
9edfcb3af7
3 changed files with 763 additions and 647 deletions
|
@ -1,7 +1,7 @@
|
|||
use std::fmt;
|
||||
use std::fmt::{Formatter, Write};
|
||||
use hifitime::Epoch;
|
||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||
use std::fmt;
|
||||
use std::fmt::{Formatter, Write};
|
||||
|
||||
#[derive(PartialOrd, PartialEq, Ord, Eq, Clone, Copy, Debug, Default)]
|
||||
#[repr(transparent)]
|
||||
|
@ -16,8 +16,8 @@ impl From<Epoch> for EpochUTC {
|
|||
|
||||
impl Serialize for EpochUTC {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_f64(self.0.to_unix_seconds())
|
||||
}
|
||||
|
@ -25,8 +25,8 @@ impl Serialize for EpochUTC {
|
|||
|
||||
impl<'de> Deserialize<'de> for EpochUTC {
|
||||
fn deserialize<D>(deserializer: D) -> Result<EpochUTC, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct EpochVisitor;
|
||||
|
||||
|
@ -34,31 +34,43 @@ impl<'de> Deserialize<'de> for EpochUTC {
|
|||
type Value = EpochUTC;
|
||||
|
||||
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
||||
formatter.write_str("a string or a float")
|
||||
formatter.write_str(
|
||||
"a (string, representing a timestamp in decimal or hexadecimal form) or a decimal",
|
||||
)
|
||||
}
|
||||
|
||||
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> where E: de::Error {
|
||||
return Ok(Epoch::from_unix_seconds(v as f64).into())
|
||||
}
|
||||
|
||||
fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E> where E: de::Error {
|
||||
return Ok(Epoch::from_unix_seconds(v as f64).into())
|
||||
}
|
||||
|
||||
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E> where E: de::Error {
|
||||
return Ok(Epoch::from_unix_seconds(v).into())
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<EpochUTC, E>
|
||||
where
|
||||
E: de::Error,
|
||||
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
return Ok(Epoch::from_unix_seconds(
|
||||
value.parse().map_err(de::Error::custom)?
|
||||
).into())
|
||||
return Ok(Epoch::from_unix_seconds(v as f64).into());
|
||||
}
|
||||
|
||||
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
return Ok(Epoch::from_unix_seconds(v).into());
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
if value.len() == 8 {
|
||||
let bytes = hex::decode(value).map_err(de::Error::custom)?;
|
||||
|
||||
return Ok(Epoch::from_unix_seconds(u32::from_be_bytes(
|
||||
bytes.as_slice().try_into().unwrap(),
|
||||
) as f64)
|
||||
.into());
|
||||
}
|
||||
return Ok(
|
||||
Epoch::from_unix_seconds(value.parse().map_err(de::Error::custom)?).into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_any(EpochVisitor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue