mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-14 11:44:04 +01:00
Use DateTime<Utc>
for until_date
in ChatMember
This commit is contained in:
parent
c3a00c687a
commit
ef84162314
2 changed files with 32 additions and 5 deletions
24
src/types.rs
24
src/types.rs
|
@ -241,3 +241,27 @@ pub(crate) mod serde_opt_date_from_unix_timestamp {
|
|||
// DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc)))
|
||||
// }
|
||||
}
|
||||
|
||||
pub(crate) mod serde_date_from_unix_timestamp {
|
||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
pub(crate) fn serialize<S>(this: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
this.timestamp().serialize(serializer)
|
||||
}
|
||||
|
||||
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let timestamp = i64::deserialize(deserializer)?;
|
||||
|
||||
Ok(DateTime::from_utc(
|
||||
NaiveDateTime::from_timestamp(timestamp, 0),
|
||||
Utc,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::types::User;
|
||||
|
@ -98,8 +99,9 @@ pub struct Administrator {
|
|||
/// enum.
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Restricted {
|
||||
/// Date when restrictions will be lifted for this user, unix time.
|
||||
pub until_date: i64,
|
||||
/// Date when restrictions will be lifted for this user.
|
||||
#[serde(with = "crate::types::serde_date_from_unix_timestamp")]
|
||||
pub until_date: DateTime<Utc>,
|
||||
|
||||
/// `true` if the user can send text messages, contacts, locations and
|
||||
/// venues.
|
||||
|
@ -122,8 +124,9 @@ pub struct Restricted {
|
|||
/// messages. This struct is part of the [`ChatMemberKind`] enum.
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Banned {
|
||||
/// Date when restrictions will be lifted for this user, unix time.
|
||||
pub until_date: i64,
|
||||
/// Date when restrictions will be lifted for this user.
|
||||
#[serde(with = "crate::types::serde_date_from_unix_timestamp")]
|
||||
pub until_date: DateTime<Utc>,
|
||||
}
|
||||
|
||||
/// This allows calling [`ChatMemberKind`]'s methods directly on [`ChatMember`].
|
||||
|
@ -270,7 +273,7 @@ impl ChatMemberKind {
|
|||
}
|
||||
|
||||
/// Getter for [`Restricted::until_date`] and [`Banned::until_date`] fields.
|
||||
pub fn until_date(&self) -> Option<i64> {
|
||||
pub fn until_date(&self) -> Option<DateTime<Utc>> {
|
||||
match &self {
|
||||
Self::Owner(_) | Self::Administrator(_) | Self::Member | Self::Left => None,
|
||||
Self::Restricted(Restricted { until_date, .. })
|
||||
|
|
Loading…
Add table
Reference in a new issue