chat-system 0.1.0

A multi-protocol async chat crate — single interface for IRC, Matrix, Discord, Telegram, Slack, Signal, WhatsApp, and more
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Rich text representation and format conversion.

use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};

/// A node in a rich text tree.
#[derive(Debug, Clone)]
pub enum RichTextNode {
    Plain(String),
    Bold(Vec<RichTextNode>),
    Italic(Vec<RichTextNode>),
    Strikethrough(Vec<RichTextNode>),
    Code(String),
    CodeBlock {
        language: Option<String>,
        code: String,
    },
    Link {
        url: String,
        text: Vec<RichTextNode>,
    },
    Mention {
        id: String,
        name: String,
    },
    Emoji(String),
    Paragraph(Vec<RichTextNode>),
    ListItem(Vec<RichTextNode>),
}

/// A rich text document as a sequence of nodes.
pub struct RichText(pub Vec<RichTextNode>);

impl RichTextNode {
    fn to_plain_text(&self) -> String {
        match self {
            RichTextNode::Plain(s) => s.clone(),
            RichTextNode::Bold(children)
            | RichTextNode::Italic(children)
            | RichTextNode::Strikethrough(children)
            | RichTextNode::Paragraph(children)
            | RichTextNode::ListItem(children) => {
                children.iter().map(|n| n.to_plain_text()).collect()
            }
            RichTextNode::Code(s) => s.clone(),
            RichTextNode::CodeBlock { code, .. } => code.clone(),
            RichTextNode::Link { text, .. } => text.iter().map(|n| n.to_plain_text()).collect(),
            RichTextNode::Mention { name, .. } => format!("@{}", name),
            RichTextNode::Emoji(e) => e.clone(),
        }
    }

    fn to_markdown(&self) -> String {
        match self {
            RichTextNode::Plain(s) => s.clone(),
            RichTextNode::Bold(children) => {
                format!(
                    "**{}**",
                    children.iter().map(|n| n.to_markdown()).collect::<String>()
                )
            }
            RichTextNode::Italic(children) => {
                format!(
                    "*{}*",
                    children.iter().map(|n| n.to_markdown()).collect::<String>()
                )
            }
            RichTextNode::Strikethrough(children) => {
                format!(
                    "~~{}~~",
                    children.iter().map(|n| n.to_markdown()).collect::<String>()
                )
            }
            RichTextNode::Code(s) => format!("`{}`", s),
            RichTextNode::CodeBlock { language, code } => {
                if let Some(lang) = language {
                    format!("```{}\n{}\n```", lang, code)
                } else {
                    format!("```\n{}\n```", code)
                }
            }
            RichTextNode::Link { url, text } => {
                format!(
                    "[{}]({})",
                    text.iter().map(|n| n.to_markdown()).collect::<String>(),
                    url
                )
            }
            RichTextNode::Mention { name, .. } => format!("@{}", name),
            RichTextNode::Emoji(e) => e.clone(),
            RichTextNode::Paragraph(children) | RichTextNode::ListItem(children) => {
                children.iter().map(|n| n.to_markdown()).collect()
            }
        }
    }

    fn to_matrix_html(&self) -> String {
        match self {
            RichTextNode::Plain(s) => html_escape(s),
            RichTextNode::Bold(children) => {
                format!(
                    "<b>{}</b>",
                    children
                        .iter()
                        .map(|n| n.to_matrix_html())
                        .collect::<String>()
                )
            }
            RichTextNode::Italic(children) => {
                format!(
                    "<i>{}</i>",
                    children
                        .iter()
                        .map(|n| n.to_matrix_html())
                        .collect::<String>()
                )
            }
            RichTextNode::Strikethrough(children) => {
                format!(
                    "<del>{}</del>",
                    children
                        .iter()
                        .map(|n| n.to_matrix_html())
                        .collect::<String>()
                )
            }
            RichTextNode::Code(s) => format!("<code>{}</code>", html_escape(s)),
            RichTextNode::CodeBlock { language, code } => {
                if let Some(lang) = language {
                    format!(
                        "<pre><code class=\"language-{}\">{}</code></pre>",
                        lang,
                        html_escape(code)
                    )
                } else {
                    format!("<pre>{}</pre>", html_escape(code))
                }
            }
            RichTextNode::Link { url, text } => {
                format!(
                    "<a href=\"{}\">{}</a>",
                    html_escape(url),
                    text.iter().map(|n| n.to_matrix_html()).collect::<String>()
                )
            }
            RichTextNode::Mention { name, .. } => format!("@{}", html_escape(name)),
            RichTextNode::Emoji(e) => html_escape(e),
            RichTextNode::Paragraph(children) | RichTextNode::ListItem(children) => {
                children.iter().map(|n| n.to_matrix_html()).collect()
            }
        }
    }

    fn to_irc_formatted(&self) -> String {
        match self {
            RichTextNode::Plain(s) => s.clone(),
            RichTextNode::Bold(children) => {
                format!(
                    "\x02{}\x02",
                    children
                        .iter()
                        .map(|n| n.to_irc_formatted())
                        .collect::<String>()
                )
            }
            RichTextNode::Italic(children) => {
                format!(
                    "\x1D{}\x1D",
                    children
                        .iter()
                        .map(|n| n.to_irc_formatted())
                        .collect::<String>()
                )
            }
            RichTextNode::Strikethrough(children) => {
                children.iter().map(|n| n.to_irc_formatted()).collect()
            }
            RichTextNode::Code(s) => format!("`{}`", s),
            RichTextNode::CodeBlock { code, .. } => code.clone(),
            RichTextNode::Link { url, text } => {
                format!(
                    "{} ({})",
                    text.iter()
                        .map(|n| n.to_irc_formatted())
                        .collect::<String>(),
                    url
                )
            }
            RichTextNode::Mention { name, .. } => format!("@{}", name),
            RichTextNode::Emoji(e) => e.clone(),
            RichTextNode::Paragraph(children) | RichTextNode::ListItem(children) => {
                children.iter().map(|n| n.to_irc_formatted()).collect()
            }
        }
    }

    fn to_whatsapp_formatted(&self) -> String {
        match self {
            RichTextNode::Plain(s) => s.clone(),
            RichTextNode::Bold(children) => {
                format!(
                    "*{}*",
                    children
                        .iter()
                        .map(|n| n.to_whatsapp_formatted())
                        .collect::<String>()
                )
            }
            RichTextNode::Italic(children) => {
                format!(
                    "_{}_",
                    children
                        .iter()
                        .map(|n| n.to_whatsapp_formatted())
                        .collect::<String>()
                )
            }
            RichTextNode::Strikethrough(children) => {
                format!(
                    "~{}~",
                    children
                        .iter()
                        .map(|n| n.to_whatsapp_formatted())
                        .collect::<String>()
                )
            }
            RichTextNode::Code(s) => format!("`{}`", s),
            RichTextNode::CodeBlock { code, .. } => format!("```{}```", code),
            RichTextNode::Link { url, text } => {
                format!(
                    "{} ({})",
                    text.iter()
                        .map(|n| n.to_whatsapp_formatted())
                        .collect::<String>(),
                    url
                )
            }
            RichTextNode::Mention { name, .. } => format!("@{}", name),
            RichTextNode::Emoji(e) => e.clone(),
            RichTextNode::Paragraph(children) | RichTextNode::ListItem(children) => {
                children.iter().map(|n| n.to_whatsapp_formatted()).collect()
            }
        }
    }
}

fn html_escape(s: impl AsRef<str>) -> String {
    let s = s.as_ref();
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            c => out.push(c),
        }
    }
    out
}

impl RichText {
    /// Convert to plain text (strips all formatting).
    pub fn to_plain_text(&self) -> String {
        self.0.iter().map(|n| n.to_plain_text()).collect()
    }

    /// Convert to standard CommonMark markdown.
    pub fn to_markdown(&self) -> String {
        self.0.iter().map(|n| n.to_markdown()).collect()
    }

    /// Convert to Discord markdown (same as CommonMark).
    pub fn to_discord_markdown(&self) -> String {
        self.to_markdown()
    }

    /// Convert to Telegram HTML.
    pub fn to_telegram_html(&self) -> String {
        crate::markdown::markdown_to_telegram_html(self.to_markdown())
    }

    /// Convert to Slack mrkdwn.
    pub fn to_slack_mrkdwn(&self) -> String {
        crate::markdown::markdown_to_slack(self.to_markdown())
    }

    /// Convert to Matrix HTML.
    pub fn to_matrix_html(&self) -> String {
        self.0.iter().map(|n| n.to_matrix_html()).collect()
    }

    /// Convert to IRC formatted text (bold=`\x02`, italic=`\x1D`).
    pub fn to_irc_formatted(&self) -> String {
        self.0.iter().map(|n| n.to_irc_formatted()).collect()
    }

    /// Convert to WhatsApp formatted text.
    pub fn to_whatsapp_formatted(&self) -> String {
        self.0.iter().map(|n| n.to_whatsapp_formatted()).collect()
    }

    /// Create from plain text.
    pub fn from_plain(text: impl AsRef<str>) -> Self {
        Self(vec![RichTextNode::Plain(text.as_ref().to_string())])
    }

    /// Parse from Markdown using pulldown-cmark.
    pub fn from_markdown(text: impl AsRef<str>) -> Self {
        let text = text.as_ref();
        let mut opts = Options::empty();
        opts.insert(Options::ENABLE_STRIKETHROUGH);
        let parser = Parser::new_ext(text, opts);

        let mut stack: Vec<Vec<RichTextNode>> = vec![vec![]];

        for event in parser {
            match event {
                Event::Start(Tag::Strong)
                | Event::Start(Tag::Emphasis)
                | Event::Start(Tag::Strikethrough) => {
                    stack.push(vec![]);
                }
                Event::Start(Tag::Link { dest_url, .. }) => {
                    stack.push(vec![RichTextNode::Plain(dest_url.to_string())]);
                    stack.push(vec![]);
                }
                Event::Start(Tag::CodeBlock(kind)) => {
                    let lang = match kind {
                        pulldown_cmark::CodeBlockKind::Fenced(lang) if !lang.is_empty() => {
                            Some(lang.to_string())
                        }
                        _ => None,
                    };
                    stack.push(vec![RichTextNode::Plain(lang.unwrap_or_default())]);
                    stack.push(vec![]);
                }
                Event::End(TagEnd::Strong) => {
                    let children = stack.pop().unwrap_or_default();
                    if let Some(top) = stack.last_mut() {
                        top.push(RichTextNode::Bold(children));
                    }
                }
                Event::End(TagEnd::Emphasis) => {
                    let children = stack.pop().unwrap_or_default();
                    if let Some(top) = stack.last_mut() {
                        top.push(RichTextNode::Italic(children));
                    }
                }
                Event::End(TagEnd::Strikethrough) => {
                    let children = stack.pop().unwrap_or_default();
                    if let Some(top) = stack.last_mut() {
                        top.push(RichTextNode::Strikethrough(children));
                    }
                }
                Event::End(TagEnd::Link) => {
                    let link_text = stack.pop().unwrap_or_default();
                    let url_node = stack.pop().unwrap_or_default();
                    let url = if let Some(RichTextNode::Plain(u)) = url_node.into_iter().next() {
                        u
                    } else {
                        String::new()
                    };
                    if let Some(top) = stack.last_mut() {
                        top.push(RichTextNode::Link {
                            url,
                            text: link_text,
                        });
                    }
                }
                Event::End(TagEnd::CodeBlock) => {
                    let code_nodes = stack.pop().unwrap_or_default();
                    let lang_node = stack.pop().unwrap_or_default();
                    let lang = if let Some(RichTextNode::Plain(l)) = lang_node.into_iter().next() {
                        if l.is_empty() {
                            None
                        } else {
                            Some(l)
                        }
                    } else {
                        None
                    };
                    let code: String = code_nodes.iter().map(|n| n.to_plain_text()).collect();
                    if let Some(top) = stack.last_mut() {
                        top.push(RichTextNode::CodeBlock {
                            language: lang,
                            code,
                        });
                    }
                }
                Event::Code(text) => {
                    if let Some(top) = stack.last_mut() {
                        top.push(RichTextNode::Code(text.to_string()));
                    }
                }
                Event::Text(text) => {
                    if let Some(top) = stack.last_mut() {
                        top.push(RichTextNode::Plain(text.to_string()));
                    }
                }
                Event::SoftBreak | Event::HardBreak => {
                    if let Some(top) = stack.last_mut() {
                        top.push(RichTextNode::Plain("\n".into()));
                    }
                }
                _ => {}
            }
        }

        Self(stack.into_iter().next().unwrap_or_default())
    }
}

impl std::fmt::Display for RichText {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.to_plain_text())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn plain_text_strips_formatting() {
        let rt = RichText(vec![
            RichTextNode::Bold(vec![RichTextNode::Plain("hello".into())]),
            RichTextNode::Plain(" world".into()),
        ]);
        assert_eq!(rt.to_plain_text(), "hello world");
    }

    #[test]
    fn discord_bold_renders_stars() {
        let rt = RichText(vec![RichTextNode::Bold(vec![RichTextNode::Plain(
            "hi".into(),
        )])]);
        assert!(rt.to_discord_markdown().contains("**hi**"));
    }

    #[test]
    fn matrix_bold_renders_b_tag() {
        let rt = RichText(vec![RichTextNode::Bold(vec![RichTextNode::Plain(
            "hi".into(),
        )])]);
        assert!(rt.to_matrix_html().contains("<b>hi</b>"));
    }

    #[test]
    fn irc_bold_uses_control_char() {
        let rt = RichText(vec![RichTextNode::Bold(vec![RichTextNode::Plain(
            "hi".into(),
        )])]);
        let s = rt.to_irc_formatted();
        assert!(s.contains('\x02'));
    }

    #[test]
    fn whatsapp_bold_uses_stars() {
        let rt = RichText(vec![RichTextNode::Bold(vec![RichTextNode::Plain(
            "hi".into(),
        )])]);
        assert!(rt.to_whatsapp_formatted().contains("*hi*"));
    }

    #[test]
    fn from_markdown_parses_bold() {
        let rt = RichText::from_markdown("**bold text**");
        assert!(rt.0.iter().any(|n| matches!(n, RichTextNode::Bold(_))));
    }

    #[test]
    fn display_gives_plain_text() {
        let rt = RichText(vec![RichTextNode::Plain("hello".into())]);
        assert_eq!(rt.to_string(), "hello");
    }

    #[test]
    fn code_block_roundtrip() {
        let rt = RichText(vec![RichTextNode::CodeBlock {
            language: Some("rust".into()),
            code: "let x = 1;".into(),
        }]);
        let md = rt.to_markdown();
        assert!(md.contains("```rust"));
        assert!(md.contains("let x = 1;"));
    }
}