Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbb committed Sep 12, 2024
1 parent c1d040b commit 8cd46d4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ explicit_auto_deref = "allow"
manual-unwrap-or-default = "allow"

# alloc_instead_of_core = "warn"
# as_conversions = "warn"
as_conversions = "warn"
# arithmetic_side_effects = "warn"
# Checks for usage of `cloned()` on an `Iterator` or `Option` where `copied()` could be used instead.
cloned_instead_of_copied = "warn"
Expand Down
1 change: 1 addition & 0 deletions serde_with/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ hashbrown_0_14 = {package = "hashbrown", version = "0.14.0", optional = true, de
hex = {version = "0.4.3", optional = true, default-features = false}
indexmap_1 = {package = "indexmap", version = "1.8", optional = true, default-features = false, features = ["serde-1"]}
indexmap_2 = {package = "indexmap", version = "2.0", optional = true, default-features = false, features = ["serde"]}
num-traits = { version = "0.2.19", features = ["std"] }
schemars_0_8 = {package = "schemars", version = "0.8.16", optional = true, default-features = false}
serde = {version = "1.0.152", default-features = false}
serde_derive = "1.0.152"
Expand Down
19 changes: 13 additions & 6 deletions serde_with/src/chrono_0_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn unix_epoch_naive() -> NaiveDateTime {
#[cfg(feature = "std")]
pub mod datetime_utc_ts_seconds_from_any {
use super::*;
use num_traits::ToPrimitive as _;

/// Deserialize a Unix timestamp with optional subsecond precision into a `DateTime<Utc>`.
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
Expand Down Expand Up @@ -87,20 +88,26 @@ pub mod datetime_utc_ts_seconds_from_any {
where
E: DeError,
{
DateTime::from_timestamp(value as i64, 0).ok_or_else(|| {
DeError::custom(format_args!(
i64::try_from(value)
.ok()
.and_then(|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}'"
))
})
})
}

fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
where
E: DeError,
{
let seconds = value.trunc() as i64;
let nsecs = (value.fract() * 1_000_000_000_f64).abs() as u32;
DateTime::from_timestamp(seconds, nsecs).ok_or_else(|| {
fn f64_to_value(value: f64) -> Option<DateTime<Utc>> {
let seconds = value.trunc().to_i64()?;
let nsecs = (value.fract() * 1_000_000_000_f64).abs().to_u32()?;
DateTime::from_timestamp(seconds, nsecs)
}
f64_to_value(value).ok_or_else(|| {
DeError::custom(format_args!(
"a timestamp which can be represented in a DateTime but received '{value}'"
))
Expand Down

0 comments on commit 8cd46d4

Please sign in to comment.