mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-14 11:44:04 +01:00
Add tools for Bot/Request type erasure
This commit is contained in:
parent
309daeb358
commit
1fe4518ee9
9 changed files with 1446 additions and 1 deletions
|
@ -9,11 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
### Added
|
||||
|
||||
- `ErasedRequester` bot adaptor, `ErasedRequest` struct, `{Request, RequesterExt}::erase` functions ([#105][pr105])
|
||||
- `Trace` bot adaptor ([#104][pr104])
|
||||
- `HasPayload`, `Request` and `Requester` implementations for `either::Either` ([#103][pr103])
|
||||
|
||||
[pr103]: https://github.com/teloxide/teloxide-core/pull/103
|
||||
[pr104]: https://github.com/teloxide/teloxide-core/pull/104
|
||||
[pr105]: https://github.com/teloxide/teloxide-core/pull/105
|
||||
|
||||
## 0.3.1 - 2021-07-07
|
||||
|
||||
|
|
12
Cargo.toml
12
Cargo.toml
|
@ -56,6 +56,9 @@ either = "1.6.1"
|
|||
vecrem = { version = "0.1", optional = true }
|
||||
bitflags = { version = "1.2", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_env_logger = "0.4"
|
||||
|
||||
[features]
|
||||
default = ["native-tls"]
|
||||
|
||||
|
@ -74,13 +77,16 @@ throttle = ["vecrem"]
|
|||
# Trace bot adaptor
|
||||
trace_adaptor = ["bitflags"]
|
||||
|
||||
# Erased bot adaptor
|
||||
erased = []
|
||||
|
||||
# CacheMe bot adaptor
|
||||
cache_me = []
|
||||
|
||||
# AutoSend bot adaptor
|
||||
auto_send = []
|
||||
|
||||
full = ["throttle", "trace_adaptor", "cache_me", "auto_send"]
|
||||
full = ["throttle", "trace_adaptor", "erased", "cache_me", "auto_send"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
|
@ -89,3 +95,7 @@ rustdoc-args = ["--cfg", "docsrs", "-Znormalize-docs"]
|
|||
[[example]]
|
||||
name = "self_info"
|
||||
required-features = ["tokio/macros", "tokio/rt-multi-thread", "auto_send"]
|
||||
|
||||
[[example]]
|
||||
name = "erased"
|
||||
required-features = ["tokio/macros", "tokio/rt-multi-thread", "auto_send", "erased"]
|
||||
|
|
43
examples/erased.rs
Normal file
43
examples/erased.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
use std::{env::VarError, time::Duration};
|
||||
|
||||
use teloxide_core::{adaptors::trace, prelude::*, types::ChatAction};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
pretty_env_logger::init();
|
||||
|
||||
let chat_id = std::env::var("CHAT_ID")
|
||||
.expect("Expected CHAT_ID env var")
|
||||
.parse::<i64>()?;
|
||||
|
||||
let trace_settings = match std::env::var("TRACE").as_deref() {
|
||||
Ok("EVERYTHING_VERBOSE") => trace::Settings::TRACE_EVERYTHING_VERBOSE,
|
||||
Ok("EVERYTHING") => trace::Settings::TRACE_EVERYTHING,
|
||||
Ok("REQUESTS_VERBOSE") => trace::Settings::TRACE_REQUESTS_VERBOSE,
|
||||
Ok("REQUESTS") => trace::Settings::TRACE_REQUESTS,
|
||||
Ok("RESPONSES_VERBOSE") => trace::Settings::TRACE_RESPONSES_VERBOSE,
|
||||
Ok("RESPONSES") => trace::Settings::TRACE_RESPONSES,
|
||||
Ok("EMPTY") | Ok("") | Err(VarError::NotPresent) => trace::Settings::empty(),
|
||||
Ok(_) | Err(VarError::NotUnicode(_)) => {
|
||||
panic!(
|
||||
"Expected `TRACE` environment variable to be equal to any of the following: \
|
||||
`EVERYTHING_VERBOSE`, `EVERYTHING`, `REQUESTS_VERBOSE`, `REQUESTS`, \
|
||||
`RESPONSES_VERBOSE`, `RESPONSES`, `EMPTY`, `` (empty string)"
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
log::info!("Trace settings: {:?}", trace_settings);
|
||||
|
||||
let bot = if trace_settings.is_empty() {
|
||||
Bot::from_env().erase().auto_send()
|
||||
} else {
|
||||
Bot::from_env().trace(trace_settings).erase().auto_send()
|
||||
};
|
||||
|
||||
bot.send_chat_action(chat_id, ChatAction::Typing).await?;
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
bot.send_message(chat_id, "Hey hey hey").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -5,6 +5,8 @@ use teloxide_core::{
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
pretty_env_logger::init();
|
||||
|
||||
let chat_id = std::env::var("CHAT_ID")
|
||||
.expect("Expected CHAT_ID env var")
|
||||
.parse::<i64>()?;
|
||||
|
|
|
@ -41,6 +41,18 @@ pub mod cache_me;
|
|||
)]
|
||||
pub mod trace;
|
||||
|
||||
/// [`ErasedErasedRequester`] bot adaptor which allows to erase type of
|
||||
/// [`Requester`].
|
||||
///
|
||||
/// [`ErasedRequester`]: erased::ErasedRequester
|
||||
/// [`Requester`]: crate::requests::Requester
|
||||
#[cfg(feature = "erased")]
|
||||
#[cfg_attr(
|
||||
all(any(docsrs, dep_docsrs), feature = "nightly"),
|
||||
doc(cfg(feature = "erased"))
|
||||
)]
|
||||
pub mod erased;
|
||||
|
||||
/// [`Throttle`] bot adaptor which allows automatically throttle when hitting
|
||||
/// API limits.
|
||||
///
|
||||
|
@ -66,6 +78,12 @@ pub use auto_send::AutoSend;
|
|||
doc(cfg(feature = "cache_me"))
|
||||
)]
|
||||
pub use cache_me::CacheMe;
|
||||
#[cfg(feature = "erased")]
|
||||
#[cfg_attr(
|
||||
all(any(docsrs, dep_docsrs), feature = "nightly"),
|
||||
doc(cfg(feature = "erased"))
|
||||
)]
|
||||
pub use erased::ErasedRequester;
|
||||
#[cfg(feature = "throttle")]
|
||||
#[cfg_attr(
|
||||
all(any(docsrs, dep_docsrs), feature = "nightly"),
|
||||
|
|
1339
src/adaptors/erased.rs
Normal file
1339
src/adaptors/erased.rs
Normal file
File diff suppressed because it is too large
Load diff
|
@ -48,6 +48,7 @@
|
|||
//! - `rustls` — use [`rustls`] tls implementation
|
||||
//! - `auto_send` — enables [`AutoSend`] bot adaptor
|
||||
//! - `trace_adaptor` — enables [`Trace`] bot adaptor
|
||||
//! - `erased` — enables [`ErasedRequester`] bot adaptor
|
||||
//! - `throttle` — enables [`Throttle`] bot adaptor
|
||||
//! - `cache_me` — enables [`CacheMe`] bot adaptor
|
||||
//! - `full` — enables all features except `nigthly`
|
||||
|
@ -57,6 +58,7 @@
|
|||
//!
|
||||
//! [`AutoSend`]: adaptors::AutoSend
|
||||
//! [`Trace`]: adaptors::Trace
|
||||
//! [`ErasedRequester`]: adaptors::ErasedRequester
|
||||
//! [`Throttle`]: adaptors::Throttle
|
||||
//! [`CacheMe`]: adaptors::CacheMe
|
||||
//! [`native-tls`]: https://docs.rs/native-tls
|
||||
|
|
|
@ -87,6 +87,18 @@ pub trait Request: HasPayload {
|
|||
/// # };
|
||||
/// ```
|
||||
fn send_ref(&self) -> Self::SendRef;
|
||||
|
||||
#[cfg(feature = "erased")]
|
||||
#[cfg_attr(
|
||||
all(any(docsrs, dep_docsrs), feature = "nightly"),
|
||||
doc(cfg(feature = "erased"))
|
||||
)]
|
||||
fn erase<'a>(self) -> crate::adaptors::erased::ErasedRequest<'a, Self::Payload, Self::Err>
|
||||
where
|
||||
Self: Sized + 'a,
|
||||
{
|
||||
crate::adaptors::erased::ErasedRequest::erase(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<L, R> Request for Either<L, R>
|
||||
|
|
|
@ -6,6 +6,9 @@ use crate::adaptors::CacheMe;
|
|||
#[cfg(feature = "auto_send")]
|
||||
use crate::adaptors::AutoSend;
|
||||
|
||||
#[cfg(feature = "erased")]
|
||||
use crate::adaptors::ErasedRequester;
|
||||
|
||||
#[cfg(feature = "trace_adaptor")]
|
||||
use crate::adaptors::trace::{Settings, Trace};
|
||||
|
||||
|
@ -42,6 +45,20 @@ pub trait RequesterExt: Requester {
|
|||
AutoSend::new(self)
|
||||
}
|
||||
|
||||
/// Erase requester type.
|
||||
#[cfg(feature = "erased")]
|
||||
#[cfg_attr(
|
||||
all(any(docsrs, dep_docsrs), feature = "nightly"),
|
||||
doc(cfg(feature = "erased"))
|
||||
)]
|
||||
fn erase<'a>(self) -> ErasedRequester<'a, Self::Err>
|
||||
where
|
||||
Self: 'a,
|
||||
Self: Sized,
|
||||
{
|
||||
ErasedRequester::new(self)
|
||||
}
|
||||
|
||||
/// Trace requests, see [`Trace`] for more.
|
||||
#[cfg(feature = "trace_adaptor")]
|
||||
#[cfg_attr(
|
||||
|
|
Loading…
Add table
Reference in a new issue