Skip to content

Commit c910fa1

Browse files
committed
Fix/remove more as casts
1 parent fa14d98 commit c910fa1

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

serde_with/src/chrono_0_4.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,20 @@ pub mod datetime_utc_ts_seconds_from_any {
9292
where
9393
E: DeError,
9494
{
95-
DateTime::from_timestamp(value as i64, 0).ok_or_else(|| {
95+
let value = i64::try_from(value).map_err(|_| {
96+
DeError::custom(format_args!(
97+
"a timestamp which can be represented in a DateTime but received '{value}'"
98+
))
99+
})?;
100+
DateTime::from_timestamp(value, 0).ok_or_else(|| {
96101
DeError::custom(format_args!(
97102
"a timestamp which can be represented in a DateTime but received '{value}'"
98103
))
99104
})
100105
}
101106

107+
// as conversions are necessary for floats
108+
#[allow(clippy::as_conversions)]
102109
fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
103110
where
104111
E: DeError,

serde_with/src/utils/duration.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ where
161161
where
162162
S: Serializer,
163163
{
164-
let mut secs = source.sign.apply(source.duration.as_secs() as i64);
164+
let mut secs = source
165+
.sign
166+
.apply(i64::try_from(source.duration.as_secs()).map_err(|_| {
167+
SerError::custom("The Duration of Timestamp is outside the supported range.")
168+
})?);
165169

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

188194
// Properly round the value
@@ -206,7 +212,11 @@ where
206212
where
207213
S: Serializer,
208214
{
209-
let mut secs = source.sign.apply(source.duration.as_secs() as i64);
215+
let mut secs = source
216+
.sign
217+
.apply(i64::try_from(source.duration.as_secs()).map_err(|_| {
218+
SerError::custom("The Duration of Timestamp is outside the supported range.")
219+
})?);
210220

211221
// Properly round the value
212222
if source.duration.subsec_millis() >= 500 {
@@ -509,7 +519,16 @@ fn parse_float_into_time_parts(mut value: &str) -> Result<(Sign, u64, u32), Pars
509519
let seconds = parts.next().expect("Float contains exactly one part");
510520
if let Ok(seconds) = seconds.parse() {
511521
let subseconds = parts.next().expect("Float contains exactly one part");
512-
let subseclen = subseconds.chars().count() as u32;
522+
let subseclen = u32::try_from(subseconds.chars().count()).map_err(|_| {
523+
#[cfg(feature = "alloc")]
524+
return ParseFloatError::Custom(alloc::format!(
525+
"Duration and Timestamps with no more than 9 digits precision, but '{value}' has more"
526+
));
527+
#[cfg(not(feature = "alloc"))]
528+
return ParseFloatError::Custom(
529+
"Duration and Timestamps with no more than 9 digits precision",
530+
);
531+
})?;
513532
if subseclen > 9 {
514533
#[cfg(feature = "alloc")]
515534
return Err(ParseFloatError::Custom(alloc::format!(

0 commit comments

Comments
 (0)