Skip to content

Commit

Permalink
Fix/remove more as casts
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbb committed Dec 25, 2024
1 parent fa14d98 commit c910fa1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
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

0 comments on commit c910fa1

Please sign in to comment.