From e79f83d4a1a0597f23f67bab239d667d9a0b700c Mon Sep 17 00:00:00 2001 From: Waffle Date: Tue, 20 Oct 2020 15:33:22 +0300 Subject: [PATCH 1/4] Move bot adaptors to own module and place then under features This commit moves `bot::{auto_send,cache_me,limits}` modules to `adaptors::{auto_send,cache_me,throttle}. ALso it adds 4 crate features: - `throttle` - `cache_me` - `auto_send` - `full` (enables all features, except `nightly`) --- Cargo.toml | 18 +++++++++++++++++- src/adaptors.rs | 19 +++++++++++++++++++ src/{bot => adaptors}/auto_send.rs | 0 src/{bot => adaptors}/cache_me.rs | 0 src/{bot/limits.rs => adaptors/throttle.rs} | 16 +++++----------- src/bot/mod.rs | 7 ------- src/lib.rs | 8 +++----- src/requests/requester_ext.rs | 3 +-- 8 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 src/adaptors.rs rename src/{bot => adaptors}/auto_send.rs (100%) rename src/{bot => adaptors}/cache_me.rs (100%) rename src/{bot/limits.rs => adaptors/throttle.rs} (98%) diff --git a/Cargo.toml b/Cargo.toml index e25be772..d550d1f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,9 +39,25 @@ never = "0.1.0" vecrem = { git = "https://github.com/WaffleLapkin/vecrem", rev = "6b9b6f42342df8b75548c6ed387072ff235429b1" } [features] -# features those require nightly compiler +default = [] + +# Features which require nightly compiler. +# +# Currently the only used compiler feature is feature(type_alias_impl_trait) +# which allow implementing `Future`s without boxing. nightly = [] +# Throttling bot adaptor +throttle = [] + +# CacheMe bot adaptor +cache_me = [] + +# AutoSend bot adaptor +auto_send = [] + +full = ["throttle", "cache_me", "auto_send"] + [package.metadata."docs.rs"] all-features = true rustdoc-args = ["--cfg", "docsrs"] diff --git a/src/adaptors.rs b/src/adaptors.rs new file mode 100644 index 00000000..3d7c4611 --- /dev/null +++ b/src/adaptors.rs @@ -0,0 +1,19 @@ +//! Bot adaptors + +#[cfg(feature = "auto_send")] +#[cfg_attr(docsrs, doc(cfg(feature = "auto_send")))] +pub mod auto_send; +#[cfg(feature = "cache_me")] +#[cfg_attr(docsrs, doc(cfg(feature = "cache_me")))] +pub mod cache_me; +#[cfg(feature = "throttle")] +#[cfg_attr(docsrs, doc(cfg(feature = "throttle")))] +pub mod throttle; + +pub use { + auto_send::AutoSend, + cache_me::CacheMe, + throttle::Throttle, +}; + +// FIXME: move default `parse_mode` to adaptor diff --git a/src/bot/auto_send.rs b/src/adaptors/auto_send.rs similarity index 100% rename from src/bot/auto_send.rs rename to src/adaptors/auto_send.rs diff --git a/src/bot/cache_me.rs b/src/adaptors/cache_me.rs similarity index 100% rename from src/bot/cache_me.rs rename to src/adaptors/cache_me.rs diff --git a/src/bot/limits.rs b/src/adaptors/throttle.rs similarity index 98% rename from src/bot/limits.rs rename to src/adaptors/throttle.rs index 412057a9..f1c40180 100644 --- a/src/bot/limits.rs +++ b/src/adaptors/throttle.rs @@ -18,7 +18,7 @@ use tokio::{ use vecrem::VecExt; use crate::{ - bot::limits::chan_send::{ChanSend, SendTy}, + adaptors::throttle::chan_send::{ChanSend, SendTy}, payloads::SendMessage, requests::{HasPayload, Output, Request, Requester}, types::ChatId, @@ -346,12 +346,7 @@ impl Throttle { } } -impl Requester for Throttle -where - B::SendMessage: Send, -{ - type Err = B::Err; - +impl Requester for Throttle { type GetMe = B::GetMe; fn get_me(&self) -> Self::GetMe { @@ -417,9 +412,8 @@ impl HasPayload for ThrottlingRequest { } } -impl Request for ThrottlingRequest +impl Request for ThrottlingRequest where - R: Request + Send, ::Payload: GetChatId, { type Err = R::Err; @@ -613,7 +607,7 @@ mod chan_send { use never::Never; use tokio::sync::{mpsc, mpsc::error::SendError, oneshot::Sender}; - use crate::bot::limits::Id; + use crate::adaptors::throttle::Id; pub(super) trait SendTy { fn send_t(self, val: (Id, Sender)) -> ChanSend; @@ -623,7 +617,7 @@ mod chan_send { pub(super) struct ChanSend(#[pin] Inner); #[cfg(not(feature = "nightly"))] - type Inner = Pin)>>> + Send>>; + type Inner = Pin)>>>>>; #[cfg(feature = "nightly")] type Inner = impl Future)>>>; diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 01698ed5..5c46e992 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -14,14 +14,7 @@ use crate::{ }; mod api; -mod auto_send; -mod cache_me; mod download; -mod limits; - -pub use auto_send::AutoSend; -pub use cache_me::CacheMe; -pub use limits::{Limits, Throttle}; pub(crate) const TELOXIDE_TOKEN: &str = "TELOXIDE_TOKEN"; pub(crate) const TELOXIDE_PROXY: &str = "TELOXIDE_PROXY"; diff --git a/src/lib.rs b/src/lib.rs index e12421e4..1b4d5dac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ mod local_macros; // FIXME(waffle): rethink modules, find a place for wrappers. pub use self::{ - bot::{AutoSend, Bot, BotBuilder, CacheMe}, + bot::{Bot, BotBuilder}, errors::{ApiError, DownloadError, RequestError}, }; @@ -26,13 +26,11 @@ pub mod payloads; pub mod prelude; pub mod requests; pub mod types; - -// FIXME(waffle): made `pub` to reexport bot wrappers, in future we may want to -// reexport them from elsewhere -pub mod bot; +pub mod adaptors; // reexported mod errors; +mod bot; // implementation details mod net; diff --git a/src/requests/requester_ext.rs b/src/requests/requester_ext.rs index e6370846..00b41b32 100644 --- a/src/requests/requester_ext.rs +++ b/src/requests/requester_ext.rs @@ -1,7 +1,6 @@ use crate::{ - bot::{CacheMe, Limits, Throttle}, + adaptors::{CacheMe, throttle::Limits, Throttle, AutoSend}, requests::Requester, - AutoSend, }; pub trait RequesterExt: Requester { From 9bf8c02c9df2e87315048d8a23db8e6da59166f3 Mon Sep 17 00:00:00 2001 From: Waffle Date: Tue, 20 Oct 2020 16:52:29 +0300 Subject: [PATCH 2/4] doc & feature fixes - Remove `unstable-stream` feature - Doc fixes --- src/adaptors.rs | 31 ++++++++++++++++++++++--------- src/adaptors/throttle.rs | 4 +++- src/bot/download.rs | 15 +++++++-------- src/lib.rs | 5 +++-- src/net/download.rs | 3 +-- src/net/mod.rs | 5 +---- src/payloads/mod.rs | 1 + src/prelude.rs | 2 ++ src/requests/mod.rs | 2 +- src/requests/request.rs | 2 ++ src/requests/requester.rs | 2 +- src/requests/requester_ext.rs | 22 ++++++++++++++++++---- src/types/chat_member.rs | 8 ++++---- src/types/mod.rs | 2 +- 14 files changed, 67 insertions(+), 37 deletions(-) diff --git a/src/adaptors.rs b/src/adaptors.rs index 3d7c4611..09c4fec8 100644 --- a/src/adaptors.rs +++ b/src/adaptors.rs @@ -1,19 +1,32 @@ -//! Bot adaptors +//! ## Bot adaptors +//! +//! Bot adaptors are very similar to [`Iterator`] adaptors — +//! they are bots (implement [`Requester`]) which wrap other bots +//! adding new functionality. +//! +//! E.g. [`AutoSend`] allows `await`ing requests directly, +//! without need to use `.send()`. +//! +//! [`Requester`]: crate::requests::Requester #[cfg(feature = "auto_send")] -#[cfg_attr(docsrs, doc(cfg(feature = "auto_send")))] +#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "auto_send")))] pub mod auto_send; #[cfg(feature = "cache_me")] -#[cfg_attr(docsrs, doc(cfg(feature = "cache_me")))] +#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "cache_me")))] pub mod cache_me; #[cfg(feature = "throttle")] -#[cfg_attr(docsrs, doc(cfg(feature = "throttle")))] +#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "throttle")))] pub mod throttle; -pub use { - auto_send::AutoSend, - cache_me::CacheMe, - throttle::Throttle, -}; +#[cfg(feature = "auto_send")] +#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "auto_send")))] +pub use auto_send::AutoSend; +#[cfg(feature = "cache_me")] +#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "cache_me")))] +pub use cache_me::CacheMe; +#[cfg(feature = "throttle")] +#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "throttle")))] +pub use throttle::Throttle; // FIXME: move default `parse_mode` to adaptor diff --git a/src/adaptors/throttle.rs b/src/adaptors/throttle.rs index f1c40180..6d9fdd84 100644 --- a/src/adaptors/throttle.rs +++ b/src/adaptors/throttle.rs @@ -123,7 +123,7 @@ impl Default for Limits { /// ## Examples /// /// ```no_run (throttle fails to spawn task without tokio runtime) -/// use teloxide_core::{bot::Limits, requests::RequesterExt, Bot}; +/// use teloxide_core::{adaptors::throttle::Limits, requests::RequesterExt, Bot}; /// /// # #[allow(deprecated)] /// let bot = Bot::new("TOKEN").throttle(Limits::default()); @@ -316,6 +316,8 @@ impl Throttle { /// Creates new [`Throttle`] spawning the worker with `tokio::spawn` /// /// Note: it's recommended to use [`RequesterExt::throttle`] instead. + /// + /// [`RequesterExt::throttle`]: crate::requests::RequesterExt::throttle pub fn new_spawn(bot: B, limits: Limits) -> Self where // Basically, I hate this bound. diff --git a/src/bot/download.rs b/src/bot/download.rs index 241ef325..eed32b1c 100644 --- a/src/bot/download.rs +++ b/src/bot/download.rs @@ -1,11 +1,11 @@ -use tokio::io::AsyncWrite; +use bytes::Bytes; +use tokio::{io::AsyncWrite, stream::Stream}; -#[cfg(feature = "unstable-stream")] -use ::{bytes::Bytes, tokio::stream::Stream}; - -#[cfg(feature = "unstable-stream")] -use crate::net::download_file_stream; -use crate::{bot::Bot, net::download_file, DownloadError}; +use crate::{ + bot::Bot, + net::{download_file, download_file_stream}, + DownloadError, +}; impl Bot { /// Download a file from Telegram into `destination`. @@ -55,7 +55,6 @@ impl Bot { /// [`AsyncWrite`]: tokio::io::AsyncWrite /// [`tokio::fs::File`]: tokio::fs::File /// [`Bot::download_file`]: crate::Bot::download_file - #[cfg(feature = "unstable-stream")] pub async fn download_file_stream( &self, path: &str, diff --git a/src/lib.rs b/src/lib.rs index 1b4d5dac..ef19a9c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ #![cfg_attr(all(docsrs, feature = "nightly"), feature(doc_cfg, doc_spotlight))] #![cfg_attr(feature = "nightly", feature(type_alias_impl_trait))] #![forbid(unsafe_code)] +#![cfg_attr(feature = "full", deny(broken_intra_doc_links))] //#![deny(missing_docs)] #[macro_use] @@ -22,15 +23,15 @@ pub use self::{ errors::{ApiError, DownloadError, RequestError}, }; +pub mod adaptors; pub mod payloads; pub mod prelude; pub mod requests; pub mod types; -pub mod adaptors; // reexported -mod errors; mod bot; +mod errors; // implementation details mod net; diff --git a/src/net/download.rs b/src/net/download.rs index 05f08f5d..aa54f4f0 100644 --- a/src/net/download.rs +++ b/src/net/download.rs @@ -27,12 +27,11 @@ where Ok(()) } -#[cfg(feature = "unstable-stream")] pub async fn download_file_stream( client: &Client, token: &str, path: &str, -) -> Result>, reqwest::Error> { +) -> Result>, reqwest::Error> { let res = client .get(&super::file_url(TELEGRAM_API_URL, token, path)) .send() diff --git a/src/net/mod.rs b/src/net/mod.rs index 97db838a..ede6cc0b 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -1,8 +1,5 @@ -#[cfg(feature = "unstable-stream")] -pub(crate) use download::download_file_stream; - pub(crate) use self::{ - download::download_file, + download::{download_file, download_file_stream}, request::{request_json, request_json2, request_multipart, request_multipart2}, telegram_response::TelegramResponse, }; diff --git a/src/payloads/mod.rs b/src/payloads/mod.rs index c468e562..22276002 100644 --- a/src/payloads/mod.rs +++ b/src/payloads/mod.rs @@ -1,3 +1,4 @@ +/// Payloads - data types sended to relegram pub mod setters; mod get_me; diff --git a/src/prelude.rs b/src/prelude.rs index f805598b..886b201a 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1 +1,3 @@ +//! Crate prelude + pub use crate::requests::Requester; diff --git a/src/requests/mod.rs b/src/requests/mod.rs index 2ba00350..cc134a48 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -1,4 +1,4 @@ -//! API requests. +//! Telegram API requests. mod has_payload; mod payload; diff --git a/src/requests/request.rs b/src/requests/request.rs index 6abb457e..0d6692d9 100644 --- a/src/requests/request.rs +++ b/src/requests/request.rs @@ -14,6 +14,8 @@ use crate::requests::{HasPayload, Output}; /// This is crucial for request wrappers which may want to cancel and/or never /// send the underlying request. E.g.: [`Throttle`]'s `send_ref` calls /// `B::send_ref` while _not_ meaning to really send the request right now. +/// +/// [`Throttle`]: crate::adaptors::Throttle #[cfg_attr(all(docsrs, feature = "nightly"), doc(spotlight))] pub trait Request: HasPayload { /* diff --git a/src/requests/requester.rs b/src/requests/requester.rs index 91f297af..4b31950b 100644 --- a/src/requests/requester.rs +++ b/src/requests/requester.rs @@ -4,7 +4,7 @@ use crate::{ types::ChatId, }; -/// The trait implemented by all bots & bot wrappers. +/// The trait implemented by all bots & bot adaptors. /// Essentially a request builder factory (?). /// /// _This trait is included in the crate's [`prelude`](crate::prelude)_. diff --git a/src/requests/requester_ext.rs b/src/requests/requester_ext.rs index 00b41b32..1b5530b2 100644 --- a/src/requests/requester_ext.rs +++ b/src/requests/requester_ext.rs @@ -1,10 +1,20 @@ -use crate::{ - adaptors::{CacheMe, throttle::Limits, Throttle, AutoSend}, - requests::Requester, -}; +use crate::requests::Requester; + +#[cfg(feature = "cache_me")] +use crate::adaptors::CacheMe; + +#[cfg(feature = "auto_send")] +use crate::adaptors::AutoSend; + +#[cfg(feature = "throttle")] +use crate::adaptors::throttle::{Limits, Throttle}; pub trait RequesterExt: Requester { /// Add `get_me` caching ability, see [`CacheMe`] for more. + /// + /// [`CacheMe`]: + #[cfg(feature = "cache_me")] + #[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "cache_me")))] fn cache_me(self) -> CacheMe where Self: Sized, @@ -13,6 +23,8 @@ pub trait RequesterExt: Requester { } /// Send requests automatically, see [`AutoSend`] for more. + #[cfg(feature = "auto_send")] + #[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "auto_send")))] fn auto_send(self) -> AutoSend where Self: Sized, @@ -23,6 +35,8 @@ pub trait RequesterExt: Requester { /// Add throttling ability, see [`Throttle`] for more. /// /// Note: this spawns the worker, just as [`Throttle::new_spawn`]. + #[cfg(feature = "throttle")] + #[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "throttle")))] fn throttle(self, limits: Limits) -> Throttle where Self: Sized, diff --git a/src/types/chat_member.rs b/src/types/chat_member.rs index 4d7c9530..46c5c7cf 100644 --- a/src/types/chat_member.rs +++ b/src/types/chat_member.rs @@ -27,14 +27,14 @@ pub enum ChatMemberKind { Kicked(Kicked), } -/// Creator of the group. This struct is part of the [`ChatmemberKind`] enum. +/// Creator of the group. This struct is part of the [`ChatMemberKind`] enum. #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct Creator { /// Custom title for this user. pub custom_title: Option, } -/// Administrator of the group. This struct is part of the [`ChatmemberKind`] +/// Administrator of the group. This struct is part of the [`ChatMemberKind`] /// enum. #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct Administrator { @@ -81,7 +81,7 @@ pub struct Administrator { pub can_promote_members: bool, } -/// User, restricted in the group. This struct is part of the [`ChatmemberKind`] +/// User, restricted in the group. This struct is part of the [`ChatMemberKind`] /// enum. #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct Restricted { @@ -106,7 +106,7 @@ pub struct Restricted { pub can_add_web_page_previews: bool, } -/// User kicked from the group. This struct is part of the [`ChatmemberKind`] +/// User kicked from the group. This struct is part of the [`ChatMemberKind`] /// enum. #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct Kicked { diff --git a/src/types/mod.rs b/src/types/mod.rs index e6f7e5fa..52bf56a2 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1,4 +1,4 @@ -//! API types. +//! Telergam API types. pub use allowed_update::*; pub use animation::*; From d47036012b65acc4e752f3598d79152ebce2dbb9 Mon Sep 17 00:00:00 2001 From: Waffle Date: Wed, 21 Oct 2020 19:51:00 +0300 Subject: [PATCH 3/4] fix rebase mistakes --- src/adaptors/throttle.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/adaptors/throttle.rs b/src/adaptors/throttle.rs index 6d9fdd84..ad923fd4 100644 --- a/src/adaptors/throttle.rs +++ b/src/adaptors/throttle.rs @@ -348,7 +348,12 @@ impl Throttle { } } -impl Requester for Throttle { +impl Requester for Throttle +where + B::SendMessage: Send, +{ + type Err = B::Err; + type GetMe = B::GetMe; fn get_me(&self) -> Self::GetMe { @@ -414,8 +419,9 @@ impl HasPayload for ThrottlingRequest { } } -impl Request for ThrottlingRequest +impl Request for ThrottlingRequest where + R: Request + Send, ::Payload: GetChatId, { type Err = R::Err; @@ -619,7 +625,7 @@ mod chan_send { pub(super) struct ChanSend(#[pin] Inner); #[cfg(not(feature = "nightly"))] - type Inner = Pin)>>>>>; + type Inner = Pin)>>> + Send>>; #[cfg(feature = "nightly")] type Inner = impl Future)>>>; From 2dfe8a15c5d150c0a21977097b122554252d2b59 Mon Sep 17 00:00:00 2001 From: Waffle Date: Wed, 21 Oct 2020 19:53:39 +0300 Subject: [PATCH 4/4] fmt --- src/adaptors/throttle.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/adaptors/throttle.rs b/src/adaptors/throttle.rs index ad923fd4..8a9481d4 100644 --- a/src/adaptors/throttle.rs +++ b/src/adaptors/throttle.rs @@ -349,11 +349,11 @@ impl Throttle { } impl Requester for Throttle -where - B::SendMessage: Send, -{ +where + B::SendMessage: Send, +{ type Err = B::Err; - + type GetMe = B::GetMe; fn get_me(&self) -> Self::GetMe {