1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
use std::{
    fmt::Debug,
    future::{Future, IntoFuture},
    pin::Pin,
    task::{self, Poll},
};

use futures::ready;
use url::Url;

use crate::{
    requests::{HasPayload, Output, Payload, Request, Requester},
    types::*,
};

/// Trace requests and responses.
///
/// This is a tool for debugging.
///
/// Depending on [`Settings`] and `log` facade this adaptor may output messages
/// like these:
/// ```text
/// TRACE teloxide_core::adaptors::trace > Sending `SendDice` request
/// TRACE teloxide_core::adaptors::trace > Got response from `SendDice` request
/// TRACE teloxide_core::adaptors::trace > Sending `SendDice` request: SendDice { chat_id: Id(0), emoji: Some(Dice), disable_notification: None, reply_to_message_id: None, allow_sending_without_reply: None, reply_markup: None }
/// TRACE teloxide_core::adaptors::trace > Got response from `SendDice` request: Ok(Message { id: 13812, date: 1625926524, chat: Chat { .. }, via_bot: None, kind: Dice(MessageDice { dice: Dice { emoji: Dice, value: 3 } }) })
/// ```
#[derive(Clone, Debug)]
pub struct Trace<B> {
    inner: B,
    settings: Settings,
}

impl<B> Trace<B> {
    pub fn new(inner: B, settings: Settings) -> Self {
        Self { inner, settings }
    }

    pub fn inner(&self) -> &B {
        &self.inner
    }

    pub fn into_inner(self) -> B {
        self.inner
    }

    pub fn settings(&self) -> Settings {
        self.settings
    }
}

bitflags::bitflags! {
    /// [`Trace`] settings that determine what will be logged.
    ///
    /// ## Examples
    ///
    /// ```
    /// use teloxide_core::adaptors::trace::Settings;
    ///
    /// // Trace nothing
    /// let _ = Settings::empty();
    /// // Trace only requests
    /// let _ = Settings::TRACE_REQUESTS;
    /// // Trace requests verbosely and responses (non verbosely)
    /// let _ = Settings::TRACE_REQUESTS_VERBOSE | Settings::TRACE_RESPONSES;
    /// ```
    pub struct Settings: u8 {
        /// Trace requests (only request kind, e.g. `send_message`)
        const TRACE_REQUESTS = 1;

        /// Trace requests verbosely (with all parameters).
        ///
        /// Implies [`TRACE_REQUESTS`]
        const TRACE_REQUESTS_VERBOSE = (1 << 1) | Self::TRACE_REQUESTS.bits;

        /// Trace responses (only request kind, e.g. `send_message`)
        const TRACE_RESPONSES = 1 << 2;

        /// Trace responses verbosely (with full response).
        ///
        /// Implies [`TRACE_RESPONSES`]
        const TRACE_RESPONSES_VERBOSE = (1 << 3) | Self::TRACE_RESPONSES.bits;

        /// Trace everything.
        ///
        /// Implies [`TRACE_REQUESTS`] and [`TRACE_RESPONSES`].
        const TRACE_EVERYTHING = Self::TRACE_REQUESTS.bits | Self::TRACE_RESPONSES.bits;

        /// Trace everything verbosely.
        ///
        /// Implies [`TRACE_REQUESTS_VERBOSE`] and [`TRACE_RESPONSES_VERBOSE`].
        const TRACE_EVERYTHING_VERBOSE = Self::TRACE_REQUESTS_VERBOSE.bits | Self::TRACE_RESPONSES_VERBOSE.bits;
    }
}

macro_rules! fty {
    ($T:ident) => {
        TraceRequest<B::$T>
    };
}

macro_rules! fwd_inner {
    ($m:ident $this:ident ($($arg:ident : $T:ty),*)) => {
        TraceRequest {
            inner: $this.inner().$m($($arg),*),
            settings: $this.settings
        }
    };
}

impl<B> Requester for Trace<B>
where
    B: Requester,
{
    type Err = B::Err;

    requester_forward! {
        get_me,
        log_out,
        close,
        get_updates,
        set_webhook,
        delete_webhook,
        get_webhook_info,
        forward_message,
        forward_messages,
        copy_message,
        copy_messages,
        send_message,
        send_photo,
        send_audio,
        send_document,
        send_video,
        send_animation,
        send_voice,
        send_video_note,
        send_media_group,
        send_location,
        edit_message_live_location,
        edit_message_live_location_inline,
        stop_message_live_location,
        stop_message_live_location_inline,
        send_venue,
        send_contact,
        send_poll,
        send_dice,
        send_chat_action,
        set_message_reaction,
        get_user_profile_photos,
        get_file,
        kick_chat_member,
        ban_chat_member,
        unban_chat_member,
        restrict_chat_member,
        promote_chat_member,
        set_chat_administrator_custom_title,
        ban_chat_sender_chat,
        unban_chat_sender_chat,
        set_chat_permissions,
        export_chat_invite_link,
        create_chat_invite_link,
        edit_chat_invite_link,
        revoke_chat_invite_link,
        set_chat_photo,
        delete_chat_photo,
        set_chat_title,
        set_chat_description,
        pin_chat_message,
        unpin_chat_message,
        unpin_all_chat_messages,
        leave_chat,
        get_chat,
        get_chat_administrators,
        get_chat_members_count,
        get_chat_member_count,
        get_chat_member,
        set_chat_sticker_set,
        delete_chat_sticker_set,
        get_forum_topic_icon_stickers,
        create_forum_topic,
        edit_forum_topic,
        close_forum_topic,
        reopen_forum_topic,
        delete_forum_topic,
        unpin_all_forum_topic_messages,
        edit_general_forum_topic,
        close_general_forum_topic,
        reopen_general_forum_topic,
        hide_general_forum_topic,
        unhide_general_forum_topic,
        unpin_all_general_forum_topic_messages,
        answer_callback_query,
        get_user_chat_boosts,
        set_my_commands,
        get_my_commands,
        set_my_name,
        get_my_name,
        set_my_description,
        get_my_description,
        set_my_short_description,
        get_my_short_description,
        set_chat_menu_button,
        get_chat_menu_button,
        set_my_default_administrator_rights,
        get_my_default_administrator_rights,
        delete_my_commands,
        answer_inline_query,
        answer_web_app_query,
        edit_message_text,
        edit_message_text_inline,
        edit_message_caption,
        edit_message_caption_inline,
        edit_message_media,
        edit_message_media_inline,
        edit_message_reply_markup,
        edit_message_reply_markup_inline,
        stop_poll,
        delete_message,
        delete_messages,
        send_sticker,
        get_sticker_set,
        get_custom_emoji_stickers,
        upload_sticker_file,
        create_new_sticker_set,
        add_sticker_to_set,
        set_sticker_position_in_set,
        delete_sticker_from_set,
        set_sticker_set_thumbnail,
        set_custom_emoji_sticker_set_thumbnail,
        set_sticker_set_title,
        delete_sticker_set,
        set_sticker_emoji_list,
        set_sticker_keywords,
        set_sticker_mask_position,
        send_invoice,
        create_invoice_link,
        answer_shipping_query,
        answer_pre_checkout_query,
        set_passport_data_errors,
        send_game,
        set_game_score,
        set_game_score_inline,
        get_game_high_scores,
        approve_chat_join_request,
        decline_chat_join_request
        => fwd_inner, fty
    }
}

#[must_use = "Requests are lazy and do nothing unless sent"]
#[derive(Clone)]
pub struct TraceRequest<R> {
    inner: R,
    settings: Settings,
}

impl<R> TraceRequest<R>
where
    R: Request,
{
    fn trace_request(&self)
    where
        R::Payload: Debug,
    {
        if self.settings.contains(Settings::TRACE_REQUESTS_VERBOSE) {
            log::trace!(
                "Sending `{}` request: {:?}",
                <R::Payload as Payload>::NAME,
                self.inner.payload_ref()
            );
        } else if self.settings.contains(Settings::TRACE_REQUESTS) {
            log::trace!("Sending `{}` request", R::Payload::NAME);
        }
    }

    fn trace_response_fn(&self) -> fn(&Result<Output<R>, R::Err>)
    where
        Output<R>: Debug,
        R::Err: Debug,
    {
        if self.settings.contains(Settings::TRACE_RESPONSES_VERBOSE) {
            |response| {
                log::trace!("Got response from `{}` request: {:?}", R::Payload::NAME, response)
            }
        } else if self.settings.contains(Settings::TRACE_RESPONSES) {
            |_| log::trace!("Got response from `{}` request", R::Payload::NAME)
        } else {
            |_| {}
        }
    }
}

impl<R> HasPayload for TraceRequest<R>
where
    R: HasPayload,
{
    type Payload = R::Payload;

    fn payload_mut(&mut self) -> &mut Self::Payload {
        self.inner.payload_mut()
    }

    fn payload_ref(&self) -> &Self::Payload {
        self.inner.payload_ref()
    }
}

impl<R> Request for TraceRequest<R>
where
    R: Request,
    Output<R>: Debug,
    R::Err: Debug,
    R::Payload: Debug,
{
    type Err = R::Err;

    type Send = Send<R::Send>;

    type SendRef = Send<R::SendRef>;

    fn send(self) -> Self::Send {
        self.trace_request();

        Send { trace_fn: self.trace_response_fn(), inner: self.inner.send() }
    }

    fn send_ref(&self) -> Self::SendRef {
        self.trace_request();

        Send { trace_fn: self.trace_response_fn(), inner: self.inner.send_ref() }
    }
}

impl<R> IntoFuture for TraceRequest<R>
where
    R: Request,
    Output<R>: Debug,
    R::Err: Debug,
    R::Payload: Debug,
{
    type Output = Result<Output<Self>, <Self as Request>::Err>;
    type IntoFuture = <Self as Request>::Send;

    fn into_future(self) -> Self::IntoFuture {
        self.send()
    }
}

#[pin_project::pin_project]
pub struct Send<F>
where
    F: Future,
{
    trace_fn: fn(&F::Output),
    #[pin]
    inner: F,
}

impl<F> Future for Send<F>
where
    F: Future,
{
    type Output = F::Output;

    fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
        let this = self.project();

        let ret = ready!(this.inner.poll(cx));
        (this.trace_fn)(&ret);
        Poll::Ready(ret)
    }
}