Added simple json request sugar

This commit is contained in:
LasterAlex 2024-08-30 06:17:44 +03:00
parent 431d67d535
commit 19ae8dd7a9
No known key found for this signature in database
2 changed files with 52 additions and 0 deletions

View file

@ -1,3 +1,4 @@
//! Some non-detrimental, but nice additions //! Some non-detrimental, but nice additions
pub mod bot; pub mod bot;
pub mod request;

View file

@ -0,0 +1,51 @@
//! Additions to [`JsonRequest`].
//!
//! [`JsonRequest`]: teloxide_core::requests::JsonRequest
use teloxide_core::{payloads::*, prelude::Requester, types::*, Bot};
macro_rules! impl_request_reply_ext {
($($t:ty),*) => {
$(
impl RequestReplyExt for $t {
fn reply_to<M>(self, message_id: M) -> Self
where
M: Into<MessageId>,
Self: teloxide_core::requests::HasPayload + Sized,
{
self.reply_parameters(ReplyParameters::new(message_id.into()))
}
}
)*
};
}
pub trait RequestReplyExt {
fn reply_to<M>(self, message_id: M) -> Self
where
M: Into<MessageId>,
Self: Sized;
}
impl_request_reply_ext! {
<Bot as Requester>::SendMessage
}
#[cfg(test)]
mod tests {
use std::ops::Deref;
use super::*;
use teloxide_core::{prelude::Requester, Bot};
#[test]
fn test_reply_to() {
let bot = Bot::new("TOKEN");
let real_reply_req = bot
.send_message(ChatId(1234), "test")
.reply_parameters(ReplyParameters::new(MessageId(1)));
let sugar_reply_req = bot.send_message(ChatId(1234), "test").reply_to(MessageId(1));
assert_eq!(real_reply_req.deref(), sugar_reply_req.deref())
}
}