From 5f0df0577f73785628778cd359a50538a37e9f9f Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 14 Nov 2022 17:55:11 +0400 Subject: [PATCH] Remove use of deprecated `chrono::NaiveDateTime::from_timestamp` --- crates/teloxide-core/src/types.rs | 34 ++++++++++++------- .../types/non_telegram_types/until_date.rs | 9 +++-- crates/teloxide-core/src/types/update.rs | 3 +- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/crates/teloxide-core/src/types.rs b/crates/teloxide-core/src/types.rs index 117830b3..0b09f7c0 100644 --- a/crates/teloxide-core/src/types.rs +++ b/crates/teloxide-core/src/types.rs @@ -239,10 +239,24 @@ pub use chat_id::*; pub use recipient::*; pub use user_id::*; -pub(crate) mod serde_opt_date_from_unix_timestamp { +/// Converts an `i64` timestump to a `choro::DateTime`, producing serde error +/// for invalid timestumps +pub(crate) fn serde_timestamp( + timestamp: i64, +) -> Result, E> { use chrono::{DateTime, NaiveDateTime, Utc}; + + NaiveDateTime::from_timestamp_opt(timestamp, 0) + .ok_or_else(|| E::custom("invalid timestump")) + .map(|naive| DateTime::from_utc(naive, Utc)) +} + +pub(crate) mod serde_opt_date_from_unix_timestamp { + use chrono::{DateTime, Utc}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; + use crate::types::serde_timestamp; + pub(crate) fn serialize( this: &Option>, serializer: S, @@ -257,8 +271,7 @@ pub(crate) mod serde_opt_date_from_unix_timestamp { where D: Deserializer<'de>, { - Ok(Option::::deserialize(deserializer)? - .map(|timestamp| DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc))) + Option::::deserialize(deserializer)?.map(serde_timestamp).transpose() } #[test] @@ -271,7 +284,8 @@ pub(crate) mod serde_opt_date_from_unix_timestamp { { let json = r#"{"date":1}"#; - let expected = DateTime::from_utc(NaiveDateTime::from_timestamp(1, 0), Utc); + let expected = + DateTime::from_utc(chrono::NaiveDateTime::from_timestamp_opt(1, 0).unwrap(), Utc); let Struct { date } = serde_json::from_str(json).unwrap(); assert_eq!(date, Some(expected)); @@ -287,9 +301,11 @@ pub(crate) mod serde_opt_date_from_unix_timestamp { } pub(crate) mod serde_date_from_unix_timestamp { - use chrono::{DateTime, NaiveDateTime, Utc}; + use chrono::{DateTime, Utc}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; + use crate::types::serde_timestamp; + pub(crate) fn serialize(this: &DateTime, serializer: S) -> Result where S: Serializer, @@ -301,9 +317,7 @@ pub(crate) mod serde_date_from_unix_timestamp { where D: Deserializer<'de>, { - let timestamp = i64::deserialize(deserializer)?; - - Ok(DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc)) + serde_timestamp(i64::deserialize(deserializer)?) } } @@ -360,10 +374,6 @@ pub(crate) mod duration_secs { where S: Serializer, { - // match this { - // Some(url) => url.serialize(serializer), - // None => "".serialize(serializer), - // } this.as_secs().serialize(serializer) } diff --git a/crates/teloxide-core/src/types/non_telegram_types/until_date.rs b/crates/teloxide-core/src/types/non_telegram_types/until_date.rs index d499ef9c..d0c09e4b 100644 --- a/crates/teloxide-core/src/types/non_telegram_types/until_date.rs +++ b/crates/teloxide-core/src/types/non_telegram_types/until_date.rs @@ -1,6 +1,8 @@ -use chrono::{DateTime, NaiveDateTime, Utc}; +use chrono::{DateTime, Utc}; use serde::{de::Visitor, Deserialize, Serialize}; +use crate::types::serde_timestamp; + /// A range of time, before some date (for example a time before a restrictions /// will be lifted from a member of a chat). #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)] @@ -31,10 +33,7 @@ impl<'de> Deserialize<'de> for UntilDate { { match v { 0 => Ok(UntilDate::Forever), - timestamp => Ok(UntilDate::Date(DateTime::from_utc( - NaiveDateTime::from_timestamp(timestamp, 0), - Utc, - ))), + timestamp => serde_timestamp(timestamp).map(UntilDate::Date), } } diff --git a/crates/teloxide-core/src/types/update.rs b/crates/teloxide-core/src/types/update.rs index 1cd90467..d34c3798 100644 --- a/crates/teloxide-core/src/types/update.rs +++ b/crates/teloxide-core/src/types/update.rs @@ -302,7 +302,8 @@ mod test { #[test] fn message() { let timestamp = 1_569_518_342; - let date = DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc); + let date = + DateTime::from_utc(NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap(), Utc); let json = r#"{ "update_id":892252934,