handle negative review times in sync

This commit is contained in:
Damien Elmes 2020-08-07 15:02:03 +10:00
parent 0180868b60
commit 8363719994

View file

@ -17,49 +17,49 @@ where
Ok(T::deserialize(v).unwrap_or_default()) Ok(T::deserialize(v).unwrap_or_default())
} }
pub fn deserialize_int_from_number<'de, T, D>(deserializer: D) -> Result<T, D::Error> pub(crate) fn deserialize_int_from_number<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
T: serde::Deserialize<'de> + FromF64, T: serde::Deserialize<'de> + FromI64,
{ {
#[derive(DeTrait)] #[derive(DeTrait)]
#[serde(untagged)] #[serde(untagged)]
enum IntOrFloat<T> { enum IntOrFloat {
Int(T), Int(i64),
Float(f64), Float(f64),
} }
match IntOrFloat::<T>::deserialize(deserializer)? { match IntOrFloat::deserialize(deserializer)? {
IntOrFloat::Float(s) => Ok(T::from_f64(s)), IntOrFloat::Float(f) => Ok(T::from_i64(f as i64)),
IntOrFloat::Int(i) => Ok(i), IntOrFloat::Int(i) => Ok(T::from_i64(i)),
} }
} }
// It may be possible to use the num_traits crate instead in the future. // It may be possible to use the num_traits crate instead in the future.
pub trait FromF64 { pub(crate) trait FromI64 {
fn from_f64(val: f64) -> Self; fn from_i64(val: i64) -> Self;
} }
impl FromF64 for i32 { impl FromI64 for i32 {
fn from_f64(val: f64) -> Self { fn from_i64(val: i64) -> Self {
val as Self val as Self
} }
} }
impl FromF64 for u32 { impl FromI64 for u32 {
fn from_f64(val: f64) -> Self { fn from_i64(val: i64) -> Self {
val as Self val.max(0) as Self
} }
} }
impl FromF64 for i64 { impl FromI64 for i64 {
fn from_f64(val: f64) -> Self { fn from_i64(val: i64) -> Self {
val as Self val
} }
} }
impl FromF64 for TimestampSecs { impl FromI64 for TimestampSecs {
fn from_f64(val: f64) -> Self { fn from_i64(val: i64) -> Self {
TimestampSecs(val as i64) TimestampSecs(val as i64)
} }
} }