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
use super::{html, markdown_v2, Nesting};
use std::{
    fmt::{self, Formatter},
    ops::Deref,
};

/// Represents a raw string for formatting. Can be created with [`raw`].
///
/// [`raw`]: ./fn.raw.html
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[must_use = "formatters need to be formatted with `markdown_v2` or `html`"]
pub struct Raw<T>(T);

/// Creates a raw string for formatting.
///
/// **Use this utility with extreme care**: it inserts the provided string into
/// the resulting string as-is. As a result, if it contains malformed
/// formatting, the resulting string won't be parsed by Telegram. Also,
/// unchecked user-provided input may insert its own formatting, which may be
/// undesrirable. Note that all other utilities automatically escape provided
/// strings as needed.
pub fn raw<I, T>(iterator: I) -> Raw<I>
where
    for<'a> &'a I: IntoIterator<Item = &'a T>,
    T: Deref<Target = str>,
{
    Raw(iterator)
}

impl<I, T> markdown_v2::Formattable for Raw<I>
where
    for<'a> &'a I: IntoIterator<Item = &'a T>,
    T: Deref<Target = str>,
{
    fn format(&self, formatter: &mut Formatter, _: Nesting) -> fmt::Result {
        (&self.0)
            .into_iter()
            .map(|x| formatter.write_str(&*x))
            .collect()
    }
}

impl<I, T> html::Formattable for Raw<I>
where
    for<'a> &'a I: IntoIterator<Item = &'a T>,
    T: Deref<Target = str>,
{
    fn format(
        &self,
        formatter: &mut Formatter,
        nesting: Nesting,
    ) -> fmt::Result {
        markdown_v2::Formattable::format(self, formatter, nesting)
    }
}