telers 1.0.0-beta.7

An asynchronous framework for Telegram Bot API written in Rust
Documentation
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Render a message's text/caption together with its [`MessageEntity`] list back into a
//! single HTML or `MarkdownV2` string — the inverse of parsing formatted text into entities.
//!
//! This lets a formatted message be stored as one human-readable string and re-sent later
//! with a `parse_mode`, instead of persisting the text and entities separately.

mod html;
mod markdown;
mod tag;

use tag::{Kind, Tag, TagWriter};

use crate::types::MessageEntity;

/// Renders text and its message entities into HTML or `MarkdownV2`.
///
/// # Example
/// ```
/// use telers::{
///     types::{MessageEntity, MessageEntityBold},
///     utils::text::Renderer,
/// };
///
/// let text = "Bold text";
/// let entities = [MessageEntity::Bold(MessageEntityBold::new(0, 4))];
///
/// assert_eq!(Renderer::new(text, &entities).as_html(), "<b>Bold</b> text");
/// ```
#[derive(Clone)]
pub struct Renderer<'a> {
    text: &'a str,
    tags: Vec<Tag<'a>>,
}

#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
impl<'a> Renderer<'a> {
    /// Creates a new [`Renderer`] for the given text and message entities.
    #[must_use]
    pub fn new(text: &'a str, entities: &'a [MessageEntity]) -> Self {
        let mut tags = Vec::with_capacity(entities.len() * 2);

        for (index, entity) in entities.iter().enumerate() {
            let kind = match entity {
                MessageEntity::Bold(_) => Kind::Bold,
                MessageEntity::Italic(_) => Kind::Italic,
                MessageEntity::Underline(_) => Kind::Underline,
                MessageEntity::Strikethrough(_) => Kind::Strikethrough,
                MessageEntity::Spoiler(_) => Kind::Spoiler,
                MessageEntity::Blockquote(_) => Kind::Blockquote,
                MessageEntity::ExpandableBlockquote(_) => Kind::ExpandableBlockquote,
                MessageEntity::Code(_) => Kind::Code,
                MessageEntity::Pre(pre) => Kind::Pre(pre.language.as_deref()),
                MessageEntity::TextLink(link) => Kind::TextLink(&link.url),
                MessageEntity::TextMention(mention) => Kind::TextMention(mention.user.id),
                MessageEntity::CustomEmoji(emoji) => Kind::CustomEmoji(&emoji.custom_emoji_id),
                MessageEntity::DateTime(date_time) => Kind::DateTime {
                    unix_time: date_time.unix_time,
                    format: date_time.date_time_format.as_deref(),
                },
                // Auto-detected entities (mention, hashtag, cashtag, bot command, url, email,
                // phone number) carry no markup — Telegram re-detects them — so they're skipped.
                _ => continue,
            };

            let offset = entity.offset() as usize;
            let length = entity.length() as usize;

            tags.push(Tag::start(kind.clone(), offset, index));

            // A blockquote can span multiple lines; MarkdownV2 needs the quote marker
            // repeated after every newline inside it.
            if matches!(kind, Kind::Blockquote | Kind::ExpandableBlockquote) {
                let new_line_indexes = text
                    .encode_utf16()
                    .skip(offset)
                    .take(length)
                    .enumerate()
                    .filter_map(|(idx, unit)| (unit == u16::from(b'\n')).then_some(idx));

                for new_line_index in new_line_indexes {
                    tags.push(Tag::mid_new_line(
                        kind.clone(),
                        offset + new_line_index + 1,
                        index,
                    ));
                }
            }

            tags.push(Tag::end(kind, offset + length, index));
        }

        tags.sort_unstable();

        Self {
            text,
            tags,
        }
    }

    /// Renders the text with the given [`TagWriter`], inserting tags at their UTF-16 offsets.
    ///
    /// Unlike teloxide, text with no renderable entities is still escaped (rather than
    /// returned verbatim) so the result is always valid HTML / `MarkdownV2`.
    fn format(&self, writer: &TagWriter) -> String {
        let mut buffer = String::with_capacity(self.text.len() + self.tags.len() * 8);
        let mut tags = self.tags.iter();
        let mut current_tag = tags.next();
        let mut prev_point: Option<u16> = None;
        // Characters inside a `code`/`pre` entity use reduced escaping. Nesting is tracked
        // as a depth so overlapping verbatim spans behave correctly.
        let mut verbatim_depth: usize = 0;

        for (idx, point) in self.text.encode_utf16().enumerate() {
            while let Some(tag) = current_tag {
                if tag.offset == idx {
                    if matches!(tag.kind, Kind::Code | Kind::Pre(_)) {
                        match tag.place {
                            tag::Place::Start => verbatim_depth += 1,
                            tag::Place::End => verbatim_depth = verbatim_depth.saturating_sub(1),
                            tag::Place::MidNewLine => {}
                        }
                    }
                    (writer.write_tag_fn)(tag, &mut buffer);
                    current_tag = tags.next();
                } else {
                    break;
                }
            }

            let ch = if let Some(previous) = prev_point.take() {
                char::decode_utf16([previous, point])
                    .next()
                    .unwrap()
                    .unwrap()
            } else {
                match char::decode_utf16([point]).next().unwrap() {
                    Ok(ch) => ch,
                    Err(unpaired) => {
                        prev_point = Some(unpaired.unpaired_surrogate());
                        continue;
                    }
                }
            };

            (writer.write_char_fn)(ch, &mut buffer, verbatim_depth > 0);
        }

        for tag in current_tag.into_iter().chain(tags) {
            (writer.write_tag_fn)(tag, &mut buffer);
        }

        buffer
    }

    /// Renders the text as an **HTML-formatted** string.
    #[must_use]
    #[inline]
    pub fn as_html(&self) -> String {
        self.format(&html::HTML)
    }

    /// Renders the text as a **MarkdownV2-formatted** string.
    #[must_use]
    #[inline]
    pub fn as_markdown(&self) -> String {
        self.format(&markdown::MARKDOWN)
    }
}

#[cfg(test)]
mod tests {
    use super::Renderer;
    use crate::types::{
        MessageEntity, MessageEntityBold, MessageEntityCode, MessageEntityCustomEmoji,
        MessageEntityDateTime, MessageEntityHashtag, MessageEntityItalic, MessageEntityMention,
        MessageEntityPre, MessageEntityStrikethrough, MessageEntityTextLink,
        MessageEntityTextMention, MessageEntityUnderline, User,
    };

    #[test]
    fn render_simple() {
        let text = "Bold italic <underline_";
        let entities = [
            MessageEntity::Bold(MessageEntityBold::new(0, 4)),
            MessageEntity::Italic(MessageEntityItalic::new(5, 6)),
            MessageEntity::Underline(MessageEntityUnderline::new(12, 10)),
        ];

        let render = Renderer::new(text, &entities);

        assert_eq!(
            render.as_html(),
            "<b>Bold</b> <i>italic</i> <u>&lt;underline</u>_"
        );
        assert_eq!(
            render.as_markdown(),
            "*Bold* _\ritalic_\r __\r<underline__\r\\_"
        );
    }

    #[test]
    fn render_pre_with_lang() {
        let text = "Some pre, normal and rusty code";
        let entities = [
            MessageEntity::Pre(MessageEntityPre::new(5, 3)),
            MessageEntity::Code(MessageEntityCode::new(10, 6)),
            MessageEntity::Pre(MessageEntityPre::new(21, 5).language("rust")),
        ];

        let render = Renderer::new(text, &entities);

        assert_eq!(
            render.as_html(),
            "Some <pre>pre</pre>, <code>normal</code> and <pre><code \
             class=\"language-rust\">rusty</code></pre> code",
        );
        assert_eq!(
            render.as_markdown(),
            "Some ```\npre```\n, `normal` and ```rust\nrusty```\n code",
        );
    }

    #[test]
    fn render_nested() {
        let text = "Some bold both italics";
        let entities = [
            MessageEntity::Bold(MessageEntityBold::new(5, 9)),
            MessageEntity::Italic(MessageEntityItalic::new(10, 12)),
        ];

        let render = Renderer::new(text, &entities);

        assert_eq!(render.as_html(), "Some <b>bold <i>both</b> italics</i>");
        assert_eq!(render.as_markdown(), "Some *bold _\rboth* italics_\r");
    }

    #[test]
    fn render_overlapping_at_same_offset() {
        // Two entities starting at the same offset: outer (lower index) opens first, and
        // the inner closes first.
        let text = "este";
        let entities = [
            MessageEntity::Underline(MessageEntityUnderline::new(0, 4)),
            MessageEntity::Strikethrough(MessageEntityStrikethrough::new(0, 4)),
        ];

        assert_eq!(
            Renderer::new(text, &entities).as_html(),
            "<u><s>este</s></u>"
        );
    }

    #[test]
    fn render_custom_emoji() {
        let text = "👍";
        let entities = [MessageEntity::CustomEmoji(MessageEntityCustomEmoji::new(
            0,
            2,
            "5368324170671202286",
        ))];

        let render = Renderer::new(text, &entities);

        assert_eq!(
            render.as_html(),
            "<tg-emoji emoji-id=\"5368324170671202286\">👍</tg-emoji>",
        );
        assert_eq!(
            render.as_markdown(),
            "[👍](tg://emoji?id=5368324170671202286)"
        );
    }

    #[test]
    fn render_date_time() {
        let text = "soon";
        let with_format = [MessageEntity::DateTime(
            MessageEntityDateTime::new(0, 4, 1).date_time_format("wDT"),
        )];
        let without_format = [MessageEntity::DateTime(MessageEntityDateTime::new(0, 4, 1))];

        assert_eq!(
            Renderer::new(text, &with_format).as_html(),
            "<tg-time unix=\"1\" format=\"wDT\">soon</tg-time>",
        );
        assert_eq!(
            Renderer::new(text, &without_format).as_html(),
            "<tg-time unix=\"1\">soon</tg-time>",
        );
        assert_eq!(
            Renderer::new(text, &with_format).as_markdown(),
            "![soon](tg://time?unix=1&format=wDT)",
        );
    }

    #[test]
    fn render_text_mention() {
        let text = "hi";
        let entities = [MessageEntity::TextMention(MessageEntityTextMention::new(
            0,
            2,
            User::new(123, false, "x"),
        ))];

        assert_eq!(
            Renderer::new(text, &entities).as_html(),
            "<a href=\"tg://user?id=123\">hi</a>",
        );
    }

    #[test]
    fn render_skips_auto_detected_entities() {
        // Mention/hashtag carry no markup, so the output is the (escaped) text only.
        let text = "@user #tag";
        let entities = [
            MessageEntity::Mention(MessageEntityMention::new(0, 5)),
            MessageEntity::Hashtag(MessageEntityHashtag::new(6, 4)),
        ];

        let render = Renderer::new(text, &entities);

        assert_eq!(render.as_html(), "@user #tag");
        assert_eq!(render.as_markdown(), "@user \\#tag");
    }

    #[test]
    fn render_complex() {
        let text = "Hi how are you?\nnested entities are cool\nIm in a Blockquote!\nIm in a \
                    multiline Blockquote!\n\nIm in a multiline Blockquote!\nIm in an expandable \
                    Blockquote!\nIm in an expandable multiline Blockquote!\n\nIm in an expandable \
                    multiline Blockquote!";
        let entities = [
            MessageEntity::Bold(MessageEntityBold::new(0, 2)),
            MessageEntity::Italic(MessageEntityItalic::new(3, 3)),
            MessageEntity::Underline(MessageEntityUnderline::new(7, 3)),
            MessageEntity::Strikethrough(MessageEntityStrikethrough::new(11, 3)),
            MessageEntity::Bold(MessageEntityBold::new(16, 1)),
            MessageEntity::Bold(MessageEntityBold::new(17, 5)),
            MessageEntity::Underline(MessageEntityUnderline::new(17, 4)),
            MessageEntity::Strikethrough(MessageEntityStrikethrough::new(17, 4)),
            MessageEntity::TextLink(MessageEntityTextLink::new(23, 8, "https://t.me/")),
            MessageEntity::TextLink(MessageEntityTextLink::new(32, 3, "tg://user?id=1234567")),
            MessageEntity::Code(MessageEntityCode::new(36, 4)),
            MessageEntity::Blockquote(crate::types::MessageEntityBlockquote::new(41, 19)),
            MessageEntity::Blockquote(crate::types::MessageEntityBlockquote::new(61, 60)),
            MessageEntity::ExpandableBlockquote(
                crate::types::MessageEntityExpandableBlockquote::new(122, 31),
            ),
            MessageEntity::ExpandableBlockquote(
                crate::types::MessageEntityExpandableBlockquote::new(154, 84),
            ),
        ];

        let render = Renderer::new(text, &entities);

        assert_eq!(
            render.as_html(),
            "<b>Hi</b> <i>how</i> <u>are</u> <s>you</s>?\n<b>n</b><b><u><s>este</s></u>d</b> \
            <a href=\"https://t.me/\">entities</a> <a href=\"tg://user?id=1234567\">are</a> <code>cool</code>\n\
            <blockquote>Im in a Blockquote!</blockquote>\n\
            <blockquote>Im in a multiline Blockquote!\n\nIm in a multiline Blockquote!</blockquote>\n\
            <blockquote expandable>Im in an expandable Blockquote!</blockquote>\n\
            <blockquote expandable>Im in an expandable multiline Blockquote!\n\nIm in an expandable multiline Blockquote!</blockquote>"
        );
        assert_eq!(
            render.as_markdown(),
            "*Hi* _\rhow_\r __\rare__\r ~you~?\n*n**__\r~este~__\rd* [entities](https://t.me/) \
             [are](tg://user?id=1234567) `cool`\n>Im in a Blockquote\\!\n>Im in a multiline \
             Blockquote\\!\n>\n>Im in a multiline Blockquote\\!\n**>Im in an expandable \
             Blockquote\\!||\n**>Im in an expandable multiline Blockquote\\!\n>\n>Im in an \
             expandable multiline Blockquote\\!||"
        );
    }

    #[test]
    fn render_blockquote_markers_distinguish_regular_from_expandable() {
        use crate::types::{MessageEntityBlockquote, MessageEntityExpandableBlockquote};

        let text = "a\nb";

        let regular = [MessageEntity::Blockquote(MessageEntityBlockquote::new(
            0, 3,
        ))];
        assert_eq!(Renderer::new(text, &regular).as_markdown(), ">a\n>b");
        assert_eq!(
            Renderer::new(text, &regular).as_html(),
            "<blockquote>a\nb</blockquote>"
        );

        let expandable = [MessageEntity::ExpandableBlockquote(
            MessageEntityExpandableBlockquote::new(0, 3),
        )];
        assert_eq!(Renderer::new(text, &expandable).as_markdown(), "**>a\n>b||");
        assert_eq!(
            Renderer::new(text, &expandable).as_html(),
            "<blockquote expandable>a\nb</blockquote>"
        );
    }

    #[test]
    fn render_blockquote_newline_with_emoji_before_quote() {
        use crate::types::MessageEntityExpandableBlockquote;

        // "😀 x\ny": the emoji is 2 UTF-16 units, so 'x' is at unit 3 and the '\n' at unit 4. The
        // quote covers "x\ny" (offset 3, length 3), so the `>` belongs at unit 5, before 'y'.
        let text = "😀 x\ny";
        let entities = [MessageEntity::ExpandableBlockquote(
            MessageEntityExpandableBlockquote::new(3, 3),
        )];

        // Counting chars instead would place the `>` at unit 4 — before the newline: "😀 **>x>\ny||".
        assert_eq!(
            Renderer::new(text, &entities).as_markdown(),
            "😀 **>x\n>y||"
        );
    }

    #[test]
    fn render_blockquote_newline_with_emoji_inside_quote() {
        use crate::types::MessageEntityExpandableBlockquote;

        // "😀\ny": the emoji is 2 UTF-16 units, so the '\n' is at unit 2 and the `>` belongs at
        // unit 3, before 'y'.
        let text = "😀\ny";
        let entities = [MessageEntity::ExpandableBlockquote(
            MessageEntityExpandableBlockquote::new(0, 4),
        )];

        // Counting chars instead would place the `>` at unit 2 — before the newline: "**>😀>\ny||".
        assert_eq!(Renderer::new(text, &entities).as_markdown(), "**>😀\n>y||");
    }

    #[test]
    fn render_blockquote_newline_ascii_is_unaffected() {
        use crate::types::MessageEntityExpandableBlockquote;

        // Control: for ASCII, chars and UTF-16 units coincide, so the `>` was already placed
        // correctly — which is why the two cases above went unnoticed.
        let text = "a\nb";
        let entities = [MessageEntity::ExpandableBlockquote(
            MessageEntityExpandableBlockquote::new(0, 3),
        )];

        assert_eq!(Renderer::new(text, &entities).as_markdown(), "**>a\n>b||");
    }
}