diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b9af44c..7ae6371b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,8 +33,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `ChatMemberKind::is_{creator,administrator,member,restricted,left,kicked}` which check `kind` along with `is_privileged` and `is_in_chat` which combine some of the above. - Refactor privilege getters - Rename `ChatAction::{RecordAudio => RecordVoice, UploadAudio => UploadVoice}` ([#86][pr86]) +- Use `url::Url` for urls, use `chrono::DateTime<Utc>` for dates ([#97][pr97]) [pr74]: https://github.com/teloxide/teloxide-core/pull/74 +[pr97]: https://github.com/teloxide/teloxide-core/pull/97 ### Fixed diff --git a/Cargo.toml b/Cargo.toml index db46ed81..1ce8e8fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ mime = "0.3.16" thiserror = "1.0.20" once_cell = "1.5.0" never = "0.1.0" +chrono = "0.4.19" vecrem = { version = "0.1", optional = true } diff --git a/src/adaptors/auto_send.rs b/src/adaptors/auto_send.rs index b3716ef9..736216e0 100644 --- a/src/adaptors/auto_send.rs +++ b/src/adaptors/auto_send.rs @@ -5,6 +5,7 @@ use std::{ }; use futures::future::FusedFuture; +use url::Url; use crate::{ requests::{HasPayload, Output, Request, Requester}, diff --git a/src/adaptors/cache_me.rs b/src/adaptors/cache_me.rs index fdbccb61..4f7e06f3 100644 --- a/src/adaptors/cache_me.rs +++ b/src/adaptors/cache_me.rs @@ -7,6 +7,7 @@ use futures::{ Future, }; use once_cell::sync::OnceCell; +use url::Url; use crate::{ payloads::GetMe, diff --git a/src/adaptors/parse_mode.rs b/src/adaptors/parse_mode.rs index 31e0f2d9..42d349eb 100644 --- a/src/adaptors/parse_mode.rs +++ b/src/adaptors/parse_mode.rs @@ -1,3 +1,5 @@ +use url::Url; + use crate::{ prelude::Requester, requests::HasPayload, diff --git a/src/adaptors/throttle.rs b/src/adaptors/throttle.rs index 36005e49..349098b1 100644 --- a/src/adaptors/throttle.rs +++ b/src/adaptors/throttle.rs @@ -15,6 +15,7 @@ use tokio::sync::{ mpsc, oneshot::{self, Receiver, Sender}, }; +use url::Url; use vecrem::VecExt; use crate::{ diff --git a/src/bot/api.rs b/src/bot/api.rs index f0e95786..cebf3733 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -1,3 +1,5 @@ +use url::Url; + use crate::{ payloads, prelude::Requester, @@ -20,10 +22,7 @@ impl Requester for Bot { type SetWebhook = JsonRequest<payloads::SetWebhook>; - fn set_webhook<U>(&self, url: U) -> Self::SetWebhook - where - U: Into<String>, - { + fn set_webhook(&self, url: Url) -> Self::SetWebhook { Self::SetWebhook::new(self.clone(), payloads::SetWebhook::new(url)) } diff --git a/src/local_macros.rs b/src/local_macros.rs index 750df39a..f5e5b30f 100644 --- a/src/local_macros.rs +++ b/src/local_macros.rs @@ -433,6 +433,7 @@ macro_rules! requester_forward { requester_forward!(@method $rest $body $ty); )* }; + (@method get_updates $body:ident $ty:ident) => { type GetUpdates = $ty![GetUpdates]; @@ -444,9 +445,9 @@ macro_rules! requester_forward { (@method set_webhook $body:ident $ty:ident) => { type SetWebhook = $ty![SetWebhook]; - fn set_webhook<U>(&self, url: U) -> Self::SetWebhook where U: Into<String> { + fn set_webhook(&self, url: Url) -> Self::SetWebhook { let this = self; - $body!(set_webhook this (url: U)) + $body!(set_webhook this (url: Url)) } }; (@method delete_webhook $body:ident $ty:ident) => { diff --git a/src/payloads/answer_callback_query.rs b/src/payloads/answer_callback_query.rs index 7d0d1bb3..17381fe9 100644 --- a/src/payloads/answer_callback_query.rs +++ b/src/payloads/answer_callback_query.rs @@ -7,6 +7,7 @@ // [cg]: https://github.com/teloxide/cg // [`schema`]: https://github.com/WaffleLapkin/tg-methods-schema use serde::Serialize; +use url::Url; use crate::types::True; @@ -35,7 +36,7 @@ impl_payload! { /// [callback_game]: https://core.telegram.org/bots/api#inlinekeyboardbutton /// [@Botfather]: https://t.me/botfather /// [`Game`]: crate::types::Game - pub url: String [into], + pub url: Url, /// The maximum amount of time in seconds that the result of the callback query may be cached client-side. Telegram apps will support caching starting in version 3.14. Defaults to 0. pub cache_time: u32, } diff --git a/src/payloads/create_chat_invite_link.rs b/src/payloads/create_chat_invite_link.rs index 9fcfcd93..ca68f41e 100644 --- a/src/payloads/create_chat_invite_link.rs +++ b/src/payloads/create_chat_invite_link.rs @@ -6,6 +6,7 @@ // // [cg]: https://github.com/teloxide/cg // [`schema`]: https://github.com/WaffleLapkin/tg-methods-schema +use chrono::{DateTime, Utc}; use serde::Serialize; use crate::types::{ChatId, ChatInviteLink}; @@ -23,7 +24,8 @@ impl_payload! { } optional { /// Point in time (Unix timestamp) when the link will expire - pub expire_date: i64, + #[serde(with = "crate::types::serde_opt_date_from_unix_timestamp")] + pub expire_date: DateTime<Utc> [into], /// Maximum number of users that can be members of the chat simultaneously after joining the chat via this invite link; 1-99999 pub member_limit: u32, } diff --git a/src/payloads/edit_chat_invite_link.rs b/src/payloads/edit_chat_invite_link.rs index 98732b98..6d683113 100644 --- a/src/payloads/edit_chat_invite_link.rs +++ b/src/payloads/edit_chat_invite_link.rs @@ -6,6 +6,7 @@ // // [cg]: https://github.com/teloxide/cg // [`schema`]: https://github.com/WaffleLapkin/tg-methods-schema +use chrono::{DateTime, Utc}; use serde::Serialize; use crate::types::ChatId; @@ -24,7 +25,8 @@ impl_payload! { } optional { /// Point in time (Unix timestamp) when the link will expire - pub expire_date: i64, + #[serde(with = "crate::types::serde_opt_date_from_unix_timestamp")] + pub expire_date: DateTime<Utc> [into], /// Maximum number of users that can be members of the chat simultaneously after joining the chat via this invite link; 1-99999 pub member_limit: u32, } diff --git a/src/payloads/kick_chat_member.rs b/src/payloads/kick_chat_member.rs index 26a51129..5679d804 100644 --- a/src/payloads/kick_chat_member.rs +++ b/src/payloads/kick_chat_member.rs @@ -6,6 +6,7 @@ // // [cg]: https://github.com/teloxide/cg // [`schema`]: https://github.com/WaffleLapkin/tg-methods-schema +use chrono::{DateTime, Utc}; use serde::Serialize; use crate::types::{ChatId, True}; @@ -24,7 +25,8 @@ impl_payload! { } optional { /// Date when the user will be unbanned, unix time. If user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever - pub until_date: u64, + #[serde(with = "crate::types::serde_opt_date_from_unix_timestamp")] + pub until_date: DateTime<Utc> [into], /// Pass True to delete all messages from the chat for the user that is being removed. If False, the user will be able to see messages in the group that were sent before the user was removed. Always True for supergroups and channels. pub revoke_messages: bool, } diff --git a/src/payloads/restrict_chat_member.rs b/src/payloads/restrict_chat_member.rs index c6cb3c9c..6e78192c 100644 --- a/src/payloads/restrict_chat_member.rs +++ b/src/payloads/restrict_chat_member.rs @@ -6,6 +6,7 @@ // // [cg]: https://github.com/teloxide/cg // [`schema`]: https://github.com/WaffleLapkin/tg-methods-schema +use chrono::{DateTime, Utc}; use serde::Serialize; use crate::types::{ChatId, ChatPermissions, True}; @@ -24,7 +25,8 @@ impl_payload! { } optional { /// Date when the user will be unbanned, unix time. If user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever - pub until_date: u64, + #[serde(with = "crate::types::serde_opt_date_from_unix_timestamp")] + pub until_date: DateTime<Utc> [into], } } } diff --git a/src/payloads/send_invoice.rs b/src/payloads/send_invoice.rs index 5a166f28..3ffe3b93 100644 --- a/src/payloads/send_invoice.rs +++ b/src/payloads/send_invoice.rs @@ -7,6 +7,7 @@ // [cg]: https://github.com/teloxide/cg // [`schema`]: https://github.com/WaffleLapkin/tg-methods-schema use serde::Serialize; +use url::Url; use crate::types::{ChatId, InlineKeyboardMarkup, LabeledPrice, Message}; @@ -46,7 +47,7 @@ impl_payload! { /// A JSON-serialized data about the invoice, which will be shared with the payment provider. A detailed description of required fields should be provided by the payment provider. pub provider_data: String [into], /// URL of the product photo for the invoice. Can be a photo of the goods or a marketing image for a service. People like it better when they see what they are paying for. - pub photo_url: String [into], + pub photo_url: Url, /// Photo size pub photo_size: String [into], /// Photo width diff --git a/src/payloads/send_poll.rs b/src/payloads/send_poll.rs index ec289ae3..213ead71 100644 --- a/src/payloads/send_poll.rs +++ b/src/payloads/send_poll.rs @@ -6,6 +6,7 @@ // // [cg]: https://github.com/teloxide/cg // [`schema`]: https://github.com/WaffleLapkin/tg-methods-schema +use chrono::{DateTime, Utc}; use serde::Serialize; use crate::types::{ChatId, Message, MessageEntity, ParseMode, PollType, ReplyMarkup}; @@ -44,7 +45,8 @@ impl_payload! { /// Amount of time in seconds the poll will be active after creation, 5-600. Can't be used together with close_date. pub open_period: u16, /// Point in time (Unix timestamp) when the poll will be automatically closed. Must be at least 5 and no more than 600 seconds in the future. Can't be used together with open_period. - pub close_date: u64, + #[serde(with = "crate::types::serde_opt_date_from_unix_timestamp")] + pub close_date: DateTime<Utc> [into], /// Pass True, if the poll needs to be immediately closed. This can be useful for poll preview. pub is_closed: bool, /// Sends the message [silently]. Users will receive a notification with no sound. diff --git a/src/payloads/set_webhook.rs b/src/payloads/set_webhook.rs index 2162642a..954d059a 100644 --- a/src/payloads/set_webhook.rs +++ b/src/payloads/set_webhook.rs @@ -7,6 +7,7 @@ // [cg]: https://github.com/teloxide/cg // [`schema`]: https://github.com/WaffleLapkin/tg-methods-schema use serde::Serialize; +use url::Url; use crate::types::{AllowedUpdate, InputFile, True}; @@ -21,7 +22,7 @@ impl_payload! { pub SetWebhook (SetWebhookSetters) => True { required { /// HTTPS url to send updates to. Use an empty string to remove webhook integration - pub url: String [into], + pub url: Url, } optional { /// Upload your public key certificate so that the root certificate in use can be checked. See our [self-signed guide] for details. diff --git a/src/requests/requester.rs b/src/requests/requester.rs index 3a4a91d0..b5152a01 100644 --- a/src/requests/requester.rs +++ b/src/requests/requester.rs @@ -1,6 +1,8 @@ // We can't change Telegram API #![allow(clippy::too_many_arguments)] +use url::Url; + use crate::{ payloads::{GetMe, SendMessage, *}, requests::Request, @@ -60,6 +62,7 @@ pub trait Requester { // // [cg]: https://github.com/teloxide/cg // [`schema`]: https://github.com/WaffleLapkin/tg-methods-schema + type GetUpdates: Request<Payload = GetUpdates, Err = Self::Err>; /// For Telegram documentation see [`GetUpdates`]. @@ -68,9 +71,7 @@ pub trait Requester { type SetWebhook: Request<Payload = SetWebhook, Err = Self::Err>; /// For Telegram documentation see [`SetWebhook`]. - fn set_webhook<U>(&self, url: U) -> Self::SetWebhook - where - U: Into<String>; + fn set_webhook(&self, url: Url) -> Self::SetWebhook; type DeleteWebhook: Request<Payload = DeleteWebhook, Err = Self::Err>; diff --git a/src/types.rs b/src/types.rs index 8cc6a35c..b8593849 100644 --- a/src/types.rs +++ b/src/types.rs @@ -217,3 +217,27 @@ mod non_telegram_types { pub(crate) mod mime; pub(super) mod semiparsed_vec; } + +pub(crate) mod serde_opt_date_from_unix_timestamp { + use chrono::{DateTime, Utc}; + use serde::{Serialize, Serializer}; + + pub(crate) fn serialize<S>( + this: &Option<DateTime<Utc>>, + serializer: S, + ) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + this.map(|dt| dt.timestamp()).serialize(serializer) + } + + // pub(crate) fn deserialize<'de, D>(deserializer: D) -> + // Result<Option<DateTime<Utc>>, D::Error> where + // D: Deserializer<'de>, + // { + // Ok(Option::<i64>::deserialize(deserializer)? + // .map(|timestamp| + // DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc))) + // } +}