Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from jonasbb:master #71

Merged
merged 12 commits into from
Dec 26, 2024
Prev Previous commit
Next Next commit
Fix/remove more as casts
  • Loading branch information
jonasbb committed Dec 25, 2024
commit c910fa12ec638b8198ed26b4f79150cf92798907
9 changes: 8 additions & 1 deletion serde_with/src/chrono_0_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,20 @@ pub mod datetime_utc_ts_seconds_from_any {
where
E: DeError,
{
DateTime::from_timestamp(value as i64, 0).ok_or_else(|| {
let value = i64::try_from(value).map_err(|_| {
DeError::custom(format_args!(
"a timestamp which can be represented in a DateTime but received '{value}'"
))
})?;
DateTime::from_timestamp(value, 0).ok_or_else(|| {
DeError::custom(format_args!(
"a timestamp which can be represented in a DateTime but received '{value}'"
))
})
}

// as conversions are necessary for floats
#[allow(clippy::as_conversions)]
fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
where
E: DeError,
Expand Down
25 changes: 22 additions & 3 deletions serde_with/src/utils/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ where
where
S: Serializer,
{
let mut secs = source.sign.apply(source.duration.as_secs() as i64);
let mut secs = source
.sign
.apply(i64::try_from(source.duration.as_secs()).map_err(|_| {
SerError::custom("The Duration of Timestamp is outside the supported range.")
})?);

// Properly round the value
if source.duration.subsec_millis() >= 500 {
Expand All @@ -183,6 +187,8 @@ where
where
S: Serializer,
{
// as conversions are necessary for floats
#[allow(clippy::as_conversions)]
let mut secs = source.sign.apply(source.duration.as_secs() as f64);

// Properly round the value
Expand All @@ -206,7 +212,11 @@ where
where
S: Serializer,
{
let mut secs = source.sign.apply(source.duration.as_secs() as i64);
let mut secs = source
.sign
.apply(i64::try_from(source.duration.as_secs()).map_err(|_| {
SerError::custom("The Duration of Timestamp is outside the supported range.")
})?);

// Properly round the value
if source.duration.subsec_millis() >= 500 {
Expand Down Expand Up @@ -509,7 +519,16 @@ fn parse_float_into_time_parts(mut value: &str) -> Result<(Sign, u64, u32), Pars
let seconds = parts.next().expect("Float contains exactly one part");
if let Ok(seconds) = seconds.parse() {
let subseconds = parts.next().expect("Float contains exactly one part");
let subseclen = subseconds.chars().count() as u32;
let subseclen = u32::try_from(subseconds.chars().count()).map_err(|_| {
#[cfg(feature = "alloc")]
return ParseFloatError::Custom(alloc::format!(
"Duration and Timestamps with no more than 9 digits precision, but '{value}' has more"
));
#[cfg(not(feature = "alloc"))]
return ParseFloatError::Custom(
"Duration and Timestamps with no more than 9 digits precision",
);
})?;
if subseclen > 9 {
#[cfg(feature = "alloc")]
return Err(ParseFloatError::Custom(alloc::format!(
Expand Down