mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 06:25:10 +01:00
Added simple json request sugar
This commit is contained in:
parent
431d67d535
commit
19ae8dd7a9
2 changed files with 52 additions and 0 deletions
|
@ -1,3 +1,4 @@
|
|||
//! Some non-detrimental, but nice additions
|
||||
|
||||
pub mod bot;
|
||||
pub mod request;
|
||||
|
|
51
crates/teloxide/src/sugar/request.rs
Normal file
51
crates/teloxide/src/sugar/request.rs
Normal 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())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue