From c6eb43138dffe63dcd266c464840ec4462a55f9c Mon Sep 17 00:00:00 2001 From: LasterAlex Date: Fri, 30 Aug 2024 06:17:44 +0300 Subject: [PATCH] Added simple json request sugar --- crates/teloxide/src/sugar.rs | 1 + crates/teloxide/src/sugar/request.rs | 51 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 crates/teloxide/src/sugar/request.rs diff --git a/crates/teloxide/src/sugar.rs b/crates/teloxide/src/sugar.rs index 6bb10ad8..b6f23cc0 100644 --- a/crates/teloxide/src/sugar.rs +++ b/crates/teloxide/src/sugar.rs @@ -1,3 +1,4 @@ //! Some non-detrimental, but nice additions pub mod bot; +pub mod request; diff --git a/crates/teloxide/src/sugar/request.rs b/crates/teloxide/src/sugar/request.rs new file mode 100644 index 00000000..a3b82091 --- /dev/null +++ b/crates/teloxide/src/sugar/request.rs @@ -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(self, message_id: M) -> Self + where + M: Into, + Self: teloxide_core::requests::HasPayload + Sized, + { + self.reply_parameters(ReplyParameters::new(message_id.into())) + } + } + )* + }; +} + +pub trait RequestReplyExt { + fn reply_to(self, message_id: M) -> Self + where + M: Into, + Self: Sized; +} + +impl_request_reply_ext! { + ::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()) + } +}