serde: Improve error handling in time modules
This commit is contained in:
parent
2f1b9f0979
commit
2805dd733b
@ -2,14 +2,14 @@
|
|||||||
//! since the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within bounds.
|
//! since the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within bounds.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
convert::TryFrom,
|
convert::TryInto,
|
||||||
time::{Duration, SystemTime, UNIX_EPOCH},
|
time::{Duration, SystemTime, UNIX_EPOCH},
|
||||||
};
|
};
|
||||||
|
|
||||||
use js_int::UInt;
|
use js_int::UInt;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Deserialize, Deserializer},
|
de::{self, Deserialize, Deserializer},
|
||||||
ser::{Error, Serialize, Serializer},
|
ser::{self, Serialize, Serializer},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Serialize a SystemTime.
|
/// Serialize a SystemTime.
|
||||||
@ -20,12 +20,10 @@ pub fn serialize<S>(time: &SystemTime, serializer: S) -> Result<S::Ok, S::Error>
|
|||||||
where
|
where
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
// If this unwrap fails, the system this is executed is completely broken.
|
let time_since_epoch = time.duration_since(UNIX_EPOCH).map_err(ser::Error::custom)?;
|
||||||
let time_since_epoch = time.duration_since(UNIX_EPOCH).unwrap();
|
let uint: UInt = time_since_epoch.as_millis().try_into().map_err(ser::Error::custom)?;
|
||||||
match UInt::try_from(time_since_epoch.as_millis()) {
|
|
||||||
Ok(uint) => uint.serialize(serializer),
|
uint.serialize(serializer)
|
||||||
Err(err) => Err(S::Error::custom(err)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserializes a SystemTime.
|
/// Deserializes a SystemTime.
|
||||||
@ -37,7 +35,9 @@ where
|
|||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let millis = UInt::deserialize(deserializer)?;
|
let millis = UInt::deserialize(deserializer)?;
|
||||||
Ok(UNIX_EPOCH + Duration::from_millis(millis.into()))
|
UNIX_EPOCH
|
||||||
|
.checked_add(Duration::from_millis(millis.into()))
|
||||||
|
.ok_or_else(|| de::Error::custom("input too large for SystemTime"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -6,7 +6,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
|||||||
|
|
||||||
use js_int::UInt;
|
use js_int::UInt;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Deserialize, Deserializer},
|
de::{self, Deserialize, Deserializer},
|
||||||
ser::{Serialize, Serializer},
|
ser::{Serialize, Serializer},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -32,8 +32,13 @@ pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<SystemTime>, D::Err
|
|||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
Ok(Option::<UInt>::deserialize(deserializer)?
|
Option::<UInt>::deserialize(deserializer)?
|
||||||
.map(|millis| UNIX_EPOCH + Duration::from_millis(millis.into())))
|
.map(|millis| {
|
||||||
|
UNIX_EPOCH
|
||||||
|
.checked_add(Duration::from_millis(millis.into()))
|
||||||
|
.ok_or_else(|| de::Error::custom("input too large for SystemTime"))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -6,7 +6,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
|||||||
|
|
||||||
use js_int::UInt;
|
use js_int::UInt;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Deserialize, Deserializer},
|
de::{self, Deserialize, Deserializer},
|
||||||
ser::{Serialize, Serializer},
|
ser::{Serialize, Serializer},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -32,8 +32,13 @@ pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<SystemTime>, D::Err
|
|||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
Ok(Option::<UInt>::deserialize(deserializer)?
|
Option::<UInt>::deserialize(deserializer)?
|
||||||
.map(|secs| UNIX_EPOCH + Duration::from_secs(secs.into())))
|
.map(|secs| {
|
||||||
|
UNIX_EPOCH
|
||||||
|
.checked_add(Duration::from_secs(secs.into()))
|
||||||
|
.ok_or_else(|| de::Error::custom("input too large for SystemTime"))
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -2,14 +2,14 @@
|
|||||||
//! the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within bounds.
|
//! the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within bounds.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
convert::TryFrom,
|
convert::TryInto,
|
||||||
time::{Duration, SystemTime, UNIX_EPOCH},
|
time::{Duration, SystemTime, UNIX_EPOCH},
|
||||||
};
|
};
|
||||||
|
|
||||||
use js_int::UInt;
|
use js_int::UInt;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Deserialize, Deserializer},
|
de::{self, Deserialize, Deserializer},
|
||||||
ser::{Error, Serialize, Serializer},
|
ser::{self, Serialize, Serializer},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Serialize a SystemTime.
|
/// Serialize a SystemTime.
|
||||||
@ -20,12 +20,10 @@ pub fn serialize<S>(time: &SystemTime, serializer: S) -> Result<S::Ok, S::Error>
|
|||||||
where
|
where
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
// If this unwrap fails, the system this is executed is completely broken.
|
let time_since_epoch = time.duration_since(UNIX_EPOCH).map_err(ser::Error::custom)?;
|
||||||
let time_since_epoch = time.duration_since(UNIX_EPOCH).unwrap();
|
let uint: UInt = time_since_epoch.as_secs().try_into().map_err(ser::Error::custom)?;
|
||||||
match UInt::try_from(time_since_epoch.as_secs()) {
|
|
||||||
Ok(uint) => uint.serialize(serializer),
|
uint.serialize(serializer)
|
||||||
Err(err) => Err(S::Error::custom(err)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserializes a SystemTime.
|
/// Deserializes a SystemTime.
|
||||||
@ -37,7 +35,9 @@ where
|
|||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let secs = UInt::deserialize(deserializer)?;
|
let secs = UInt::deserialize(deserializer)?;
|
||||||
Ok(UNIX_EPOCH + Duration::from_secs(secs.into()))
|
UNIX_EPOCH
|
||||||
|
.checked_add(Duration::from_secs(secs.into()))
|
||||||
|
.ok_or_else(|| de::Error::custom("input too large for SystemTime"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user