Skip to content

Commit 8cd46d4

Browse files
committed
WIP
1 parent c1d040b commit 8cd46d4

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ explicit_auto_deref = "allow"
4848
manual-unwrap-or-default = "allow"
4949

5050
# alloc_instead_of_core = "warn"
51-
# as_conversions = "warn"
51+
as_conversions = "warn"
5252
# arithmetic_side_effects = "warn"
5353
# Checks for usage of `cloned()` on an `Iterator` or `Option` where `copied()` could be used instead.
5454
cloned_instead_of_copied = "warn"

serde_with/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ hashbrown_0_14 = {package = "hashbrown", version = "0.14.0", optional = true, de
129129
hex = {version = "0.4.3", optional = true, default-features = false}
130130
indexmap_1 = {package = "indexmap", version = "1.8", optional = true, default-features = false, features = ["serde-1"]}
131131
indexmap_2 = {package = "indexmap", version = "2.0", optional = true, default-features = false, features = ["serde"]}
132+
num-traits = { version = "0.2.19", features = ["std"] }
132133
schemars_0_8 = {package = "schemars", version = "0.8.16", optional = true, default-features = false}
133134
serde = {version = "1.0.152", default-features = false}
134135
serde_derive = "1.0.152"

serde_with/src/chrono_0_4.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ fn unix_epoch_naive() -> NaiveDateTime {
5757
#[cfg(feature = "std")]
5858
pub mod datetime_utc_ts_seconds_from_any {
5959
use super::*;
60+
use num_traits::ToPrimitive as _;
6061

6162
/// Deserialize a Unix timestamp with optional subsecond precision into a `DateTime<Utc>`.
6263
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
@@ -87,20 +88,26 @@ pub mod datetime_utc_ts_seconds_from_any {
8788
where
8889
E: DeError,
8990
{
90-
DateTime::from_timestamp(value as i64, 0).ok_or_else(|| {
91-
DeError::custom(format_args!(
91+
i64::try_from(value)
92+
.ok()
93+
.and_then(|value| DateTime::from_timestamp(value, 0))
94+
.ok_or_else(|| {
95+
DeError::custom(format_args!(
9296
"a timestamp which can be represented in a DateTime but received '{value}'"
9397
))
94-
})
98+
})
9599
}
96100

97101
fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
98102
where
99103
E: DeError,
100104
{
101-
let seconds = value.trunc() as i64;
102-
let nsecs = (value.fract() * 1_000_000_000_f64).abs() as u32;
103-
DateTime::from_timestamp(seconds, nsecs).ok_or_else(|| {
105+
fn f64_to_value(value: f64) -> Option<DateTime<Utc>> {
106+
let seconds = value.trunc().to_i64()?;
107+
let nsecs = (value.fract() * 1_000_000_000_f64).abs().to_u32()?;
108+
DateTime::from_timestamp(seconds, nsecs)
109+
}
110+
f64_to_value(value).ok_or_else(|| {
104111
DeError::custom(format_args!(
105112
"a timestamp which can be represented in a DateTime but received '{value}'"
106113
))

0 commit comments

Comments
 (0)