Use url::Url for urls, use chrono::DateTime<Utc> for dates

This commit is contained in:
Waffle 2021-07-03 23:35:13 +03:00
parent bd104a0a08
commit 421cf42835
18 changed files with 63 additions and 17 deletions

View file

@ -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

View file

@ -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 }

View file

@ -5,6 +5,7 @@ use std::{
};
use futures::future::FusedFuture;
use url::Url;
use crate::{
requests::{HasPayload, Output, Request, Requester},

View file

@ -7,6 +7,7 @@ use futures::{
Future,
};
use once_cell::sync::OnceCell;
use url::Url;
use crate::{
payloads::GetMe,

View file

@ -1,3 +1,5 @@
use url::Url;
use crate::{
prelude::Requester,
requests::HasPayload,

View file

@ -15,6 +15,7 @@ use tokio::sync::{
mpsc,
oneshot::{self, Receiver, Sender},
};
use url::Url;
use vecrem::VecExt;
use crate::{

View file

@ -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))
}

View file

@ -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) => {

View file

@ -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,
}

View file

@ -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,
}

View file

@ -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,
}

View file

@ -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,
}

View file

@ -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],
}
}
}

View file

@ -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

View file

@ -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.

View file

@ -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.

View file

@ -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>;

View file

@ -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)))
// }
}