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

#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
enum Kind<T> {
    Link(T),
    Mention(user::Id),
}

/// Formats a link. Can be created with [`link`] or [`mention`].
///
/// [`link`]: ./fn.link.html
/// [`mention`]: ./fn.mention.html
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct Link<T, L = &'static str> {
    text: T,
    link: Kind<L>,
}

/// Creates a link.
pub fn link<T, L>(text: T, link: L) -> Link<T, L>
where
    T: Formattable,
    L: Deref<Target = str>,
{
    Link {
        text,
        link: Kind::Link(link),
    }
}

/// Creates a mention by ID.
pub fn mention<T: Formattable>(text: T, user: user::Id) -> Link<T> {
    Link {
        text,
        link: Kind::Mention(user),
    }
}

impl<T, L> markdown_v2::Formattable for Link<T, L>
where
    T: Formattable,
    L: Deref<Target = str>,
{
    fn format(
        &self,
        formatter: &mut Formatter,
        nesting: Nesting,
    ) -> fmt::Result {
        formatter.write_char('[')?;
        markdown_v2::Formattable::format(&self.text, formatter, nesting)?;
        formatter.write_str("](")?;

        match &self.link {
            Kind::Link(link) => link
                .deref()
                .chars()
                .map(|x| {
                    if markdown_v2::ESCAPED_LINK_CHARACTERS.contains(&x) {
                        formatter.write_char('\\')?;
                    }
                    formatter.write_char(x)
                })
                .collect::<Result<(), _>>()?,
            Kind::Mention(user::Id(id)) => {
                write!(formatter, "tg://user?id={}", id)?
            }
        }
        formatter.write_char(')')
    }
}

impl<T, L> html::Formattable for Link<T, L>
where
    T: Formattable,
    L: Deref<Target = str>,
{
    fn format(
        &self,
        formatter: &mut Formatter,
        nesting: Nesting,
    ) -> fmt::Result {
        formatter.write_str("<a href=\"")?;

        match &self.link {
            Kind::Link(link) => link
                .deref()
                .chars()
                .map(|x| {
                    if x == '"' {
                        formatter.write_char('\\')?;
                    }
                    formatter.write_char(x)
                })
                .collect::<Result<(), _>>()?,
            Kind::Mention(user::Id(id)) => {
                write!(formatter, "tg://user?id={}", id)?
            }
        }

        formatter.write_str("\">")?;
        html::Formattable::format(&self.text, formatter, nesting)?;
        formatter.write_str("</a>")
    }
}